Shared Flashcard Set

Details

SCJP 6 Chapter 1
Flashcards for the Sierra/Bates SCJP 6 exam prep book. Chapter 1 - Declarations and Access Control
70
Software
Professional
02/08/2011

Additional Software Flashcards

 


 

Cards

Term
Which of the following are NON-valid characters for the first character of a legal Java identifier?


a. number
b. letter
c. underscore
d. percent character (%)
e. currency character ($)
Definition
a, d
(p. 5)
Term
Which of the following are NON-valid characters for the characters following the first character of a legal Java identifier?


a. number
b. letter
c. underscore
d. percent character (%)
e. currency character ($)
Definition
d
(p. 5)
Term
T/F: A Java keyword can be used as an identifier.
Definition
F
(p. 5)
Term
T/F: Java identifiers are case sensitive.
Definition
T
(p. 5)
Term
T/F: The following is a legal identifier:
int $c;
Definition
T
(p. 5)
Term
When can a class access a protected variable?
Definition
a) When it is defined in the same class
b) When it is defined in a super-class
c) When it is in the same package
Term
When can a class access a protected member variable in a class in another package?
Definition
Only by inheritance (pg. 36)
Term
When can a class access a default member variable in a class in another package?
Definition
It can't (p. 36)
Term
When can access modifiers be applied to local variables?
Definition
Never (p. 36)
Term
Name some Nonaccess Member Modifiers (there are at least 7)
Definition
final, abstract, transient, synchronized, native, strictfp, and static (p. 39)
Term
Which is an access member modifier: protected or static?
Definition
protected
Term
Private members may be accessed by what type of classes other than the declaring class?
Definition
None.  Private members are only visible to the class in which they were declared.
Term
What is the difference between the access controls protected and default?
Definition
A default member of one class may only be accessed by classes within the same package.  Protected members, may additionally be accessed by classes extending the member's declaring class, regardless of the package.
Term
Which of the following are legal declarations?

1) Int[5] someArray;
2) Int[] someArray[];
3) Int[][] someArray[];
4) Int someArray[][];
5) Int someArray[5][5];
Definition
2,3,4 (pg. 55)
Term
What is the result?

public class Test {
public static void main (String args[]){
int[] x = new int[5];
System.out.println(x[1]);
Test [] test = new Test[5];
System.out.println(test[1]);
}
}
Definition
0
null
(pg. 58)
Term
public class Test {
private int x = 5;
Test(){};
Test(int x){
this.x = x;
}
public static void main (String args[]){
final Test test = new Test();
test.setX(6);
System.out.println(test.getX());
test.changeX(test);
System.out.println(test.getX());
}
public void setX(int x) {
this.x = x;
}
public int getX() {
return x;
}
public void changeX(Test test){
test = new Test(7);
}
}
Definition
6
6
(pg. 57)
Term
Define the effects of the final modifier on

A Class?
A method?
A variable?
Definition
1) Class cannot be subclassed.
2) Method cannot be overridden.
3) A final variable cannot reassigned once assigned.

(pg. 59)

(pg.
Term
What is the result?
public class Test {
public static int x;
public static final int y;
public static void main (String args[]){
final Test test = new Test();
test.x=(6);
test.y=(7);
System.out.println(x + " " + test.y);
}
}
Definition
Compilation error. A final variable must be initialized before the constructor completes.

(pg. 57)
Term
Which of the following can be marked Static:

1) Constructors?
2) Classes?
3) Interfaces?
4) Methods?
5) Variables?
6) Inner Classes?
7) Methods and Instance Variables of inner Classes?
8) Method Local inner Classes?
9) Local Variables?
10) Initialization Blocks?
Definition
4, 5, 6, 10
(pg. 59 and 60)
Term
// Given the following how would you assign a value to drink?
// insert code where #1 goes.
enum CoffeeSize { BIG}
class Coffee {
CoffeeSize size;
}
public class CoffeeTest1 {
public static void main(String[] args){
Coffee drink = new Coffee();
#1
}
}
Definition
drink.size = CoffeeSize.BIG;
(pg. 61)
Term
enum CoffeeSize { Big(8),Huge(10);
CoffeeSize (int ounces){this.ounces = ounces;}
private int ounces;
public int getOunces(){ return this.ounces;}
}
class Coffee {
CoffeeSize size = new CoffeeSize(20);
static public void main (String args[]){
Coffee drink1 = new Coffee();
System.out.println(drink1.size);
}
}

What is the result?
Definition
Compiler error, you cannot call an Enums constructor directly..
(pg. 64)
Term
Methods marked "abstract" end in a semicolon rather than curly braces.

Y/N?
Definition
Yes.

(p.17)
Term
If a single method is "abstract", the whole class must be declared "abstract".

Y/N?
Definition
Yes.

(p.18)
Term
T/F: The following identifier is legal:
int -d;
Definition
False - Identifier does not start with a letter, currency char ($) or underscore.
(Ref p. 5)
Term
T/F: The following identifier is legal:
int e#;
Definition
False - Identifier contains an illegal character (#).
(Ref: p. 5)
Term
T/F: The following identifier is legal:
int .f;
Definition
False - Identifier does not start with a letter, currency char ($) or underscore.
(Ref: p. 5)
Term
T/F: The following identifier is legal:
int 7g;
Definition
False - Identifier does not start with a letter, currency char ($) or underscore.
(Ref: p. 5)
Term
Which of the following is not a keyword? (Choose one)
a. while
b. next
c. native
d. strictfp
e. goto
Definition
b. next (p. 6)
Term
What are the JavaBeans naming conventions for a boolean?
Definition
Getter method prefix of 'get' or 'is'.
Setter method prefix of 'set'.
(p. 9)
Term
What are the JavaBean naming rules for non-boolean properties?
Definition
Getter method prefix is 'get'.
Setter method prefix is 'set'.
(p. 9)
Term
T/F: JavaBeans naming standards allows for non-public method signatures.
Definition
False - getter and setter method signatures must be marked public.
(p. 9)
Term
What are the JavaBeans naming standards for Listeners?
Definition
Adding a listener uses a method prefix of 'add'.
Removing a listener uses a method prefix of 'remove'
The method signature must end with 'Listener'
(p. 9)
Term
T/F: An method signature may be legal (and compile) even if it violates JavaBeans standards.
Definition
T (p. 10).
Term
Which of the following modifiers can not be applied to an instance variable?

a.) static
b.) transient
c.) abstract
d.) native
e.) volatile
f.) synchronized
Definition
a,c,d,f
pg52
Term
What is the result?

Class Test {
public static void main (String args[]){
int x;
int y;

setX(x);
y = 2;

System.out.println(x + y);
}

private static void setX(int x) {
x = 5;
}
}
Definition
Wont compile. Local varialbes must be initialized and x is never initialized before use.

pg54
Term
To which of these can the synchronized and native modifier be applied?
a.) variables
b.) classes
c.) methods
Definition
c

pg45,46
Term
Which of the following are invalid JavaBean method signatures (Choose two).
a. void setCustomerName(String s)
b. public boolean isMyStatus()
c. public void addMyListener(MyListener m)
d. public void removeMyListener(MyListener m)
e. public int getMyValue()
f. public void addXListener(MyListener m)
Definition
a, f
(p. 10)
Term
What does it mean when a method or variable member is declared public?
Definition
It means that all other classes regardless of the package they belong to, can access the member.
Term
When a member is marked private, can other classes access the code in the private class?
Definition
NO, no other class can use it other than the class in which the private member was declared.
Term
Which are valid variable argument list (var-args) declarations?

a.)void doStuff(... x) { }
b.)void doMoreStuff(MyClass ... myClass) { }
c.)void doLotsOfStuff(MyClass myClass, int x, char... y);
d.)void doLoadsOfStuff(int... x, int... y);
e.)void doTonsOfStuff(MyClass... myClass, int x);
Definition
b,c are valid

a - must declare parameter type
d - can only have one var-arg parameter
e - var-arg must come last

pg 47
Term
Can a subclass inherit a private member from it's superclass?
Definition
No, when a member is declared private, a subclass can't inherit it.
Term
Can you create a method in a subclass that has the same name as a private method in a superclass?
Definition
Yes, if the superclass method is private then the subclass method can have the same name without compiler error.
Term
Can a private method be overridden by a subclass?
Definition
No, since the subclass cannot inherit from the superclass, it therefore cannot override the method.
Term
Which of the following are invalid constructors and why?
a.) void Foo() { }
b.) private Foo() { }
c.) final Foo() { }
d.) Foo() { }
Definition
a - constructors dont have return types
c - constructors cant be final

pg48
Term
Put the following in order from smallest to largest in terms of bit size and how many bits for each?
long, byte, int, short
Definition
byte(8), short(16), int(32), long(64)

pg50
Term
True or False. Signed primitives have more possible negative values than positive values?
Definition
True

pg 50
Term

public class Dog extends Animal {
  public void speak() {
    System.out.println("Bark.");
  }

  public static void main(String[] args) {
    Dog dog = new Dog();
    dog.speak();
  }
}

abstract class Animal implements AnimalInterface {
  public void doAnimalStuff() { }
  public final void speak() {
    System.out.println("I am talking.");
  }
}

 

What is the result?

Definition

Compilation error because class Dog cannot override the final method "speak()" declared in class Animal. (pg 40)

Term

public static void f(final int var) {
  System.out.println(var++);
}

T/F - This method can be successfully called.

Definition

False.  Final argument "var" cannot be modified in the method. (pg 41)

Term

public class Animal implements AnimalInterface{
  public abstract void speak();
  public void doAnimalStuff() { }
 
}

interface AnimalInterface {
  void doAnimalStuff();
}

Will this compile?

Definition

No - It is illegal to have an abstract method in a class that is not explicitly declared abstract. (pg 42)

Term

Which of the following modifiers cannot be combined together?  (Choose all that applies)

1) protected and final
2) abstract and static
3) final and abstract
4) public and static
5) abstract and private

Definition

2, 3, and 5

A method can never be marked as both abstract and final, abstract and static, or abstract and private. (pg 45)

Term

public abstract class A {
  abstract void foo();
}

class B extends A {
  void foo(int I) { }
}

 

T/F - This will compile.

Definition

False - It is required for class B to implement method foo().  Class B won't compile because of this.  (pg 44)

Term

T/F - The first concrete subclass of an abstract class must implement all abstract methods of the superclass.

Definition

True - (pg 42)

Term

public abstract class SomeClass{
  void someMethod() {
    // Code
  }
}


Will this code compile?

Definition

Yes - You can have an abstract class with no abstract methods. (pg 42)

Term

If a subclass is abstract, is it required to implement all of its superclass abstract methods?

Definition

No, it is allowed to implement any or all of the superclass abstract methods. (pg 44)

Term

public abstract class A {
  private String type;
  public abstract void doAStuff();
  public String getType() {
    return type;
  }
}

public abstract class B extends A {
  public abstract void doAStuff();
  public void doBStuff() { }
}

public class C extends B {
  public void doAStuff() { }
}

 

How many methods does class C have?

Definition

class C has 3 methods - Class C implements doAStuff() and inherits both getType() and doBStuff(). (pg 43)

Term

interface AInterface {
  void doAThings();
}

public abstract class A implements AInterface {
  public abstract void doMoreThings();
  public String doSomething() { }
}

public abstract class B extends A {
  public void doAThings() { }
}

 


What extra methods is class B required to implement?

Definition

class B is required to implement doMoreThings().

Term
All Interface Methods are implicitly public and abstract.
Definition
True.
"You do not need to actually type public or abstract, but the methods always are"
Term
List the 3 types of variables allowed to be declared in an interface.
Definition
public, static, final
Term
Interfaces methods can be static. (T/F)
Definition
False
Term
An Interface can implement another interface. (T/F)
Definition
False
Term
Which of these interface methods will compile? (Select all that are true)

A.
final void bounce();

B.
static void bounce();

C.
private void bounce();

D.
protected void bounce();

E. None of the above
Definition
None of the above.

final void bounce(); // final and abstract can never be used
// together, and abstract is implied
static void bounce(); // interfaces define instance methods
private void bounce(); // interface methods are always public
protected void bounce(); // (same as above)
Term
T/F: There can be multiple public classes per source code file.
Definition
F
(p. 11)
Term
If a source code file contains a package statement, where must the package statement appear?
Definition
It must be the first line in the source code file, before any import statements.
(p. 11)
Term
T/F: A file can have more than one non-public class.
Definition
T
(p. 11)
Term
If a source code file contains import statements, where must they appear?
Definition
Import statements must go between the package statement (if there is one) and the class declaration.
(p. 11)
Term
T/F: Files with no public classes must have a name that matches one of the classes.
Definition
F
(p. 12)
Term
T/F: A class can be declared with only public or default access
Definition
T
(p. 12)
Term
What are default access with respect to a class?
Definition
The class will have no access modifier preceding the class definition, and it can only be seen by classes within the same package.
(p. 13)
Term
What are the non-access modifiers that are applicable to a class?
Definition
final, abstract, and strictfp
(p. 15)
Term
T/F: The following is a legal class declaration:
public abstract final Ferrari { }
Definition
F - cannot use abstract and final together
(p. 15)
Supporting users have an ad free experience!