Review Questions

graphics/rq_icon.gif

4.20

Which statements are true about the use of modifiers?

Select the two correct answers.

  1. If no accessibility modifier (public, protected, and private) is specified for a member declaration, the member is only accessible for classes in the package of its class and subclasses of its class anywhere.

  2. You cannot specify accessibility of local variables. They are only accessible within the block in which they are declared.

  3. Subclasses of a class must reside in the same package as the class they extend.

  4. Local variables can be declared static.

  5. Objects themselves do not have any accessibility modifiers, only the object references do.

4.21

Given the following source code, which comment line can be uncommented without introducing errors?

abstract class MyClass {
    abstract void f();
    final    void g() {}
 // final    void h() {}                         // (1)

    protected static int i;
    private          int j;
}

final class MyOtherClass extends MyClass {
 // MyOtherClass(int n) { m = n; }               // (2)

    public static void main(String[] args) {
        MyClass mc = new MyOtherClass();
    }

    void f() {}
    void h() {}
 // void k() { i++; }                            // (3)
 // void l() { j++; }                            // (4)

    int m;
}

Select the one correct answer.

  1. final void h() {}                      // (1)
    
  2. MyOtherClass(int n) { m = n; }         // (2)
    
  3. void k() { i++; }                      // (3)
    
  4. void l() { j++; }                      // (4)
    
4.22

What would be the result of attempting to compile and run the following program?

class MyClass {
    static MyClass ref;
    String[] arguments;

    public static void main(String[] args) {
        ref = new MyClass();
        ref.func(args);
    }

    public void func(String[] args) {
        ref.arguments = args;
    }
}

Select the one correct answer.

  1. The program will fail to compile, since the static method main() cannot have a call to the non-static method func().

  2. The program will fail to compile, since the non-static method func() cannot access the static variable ref.

  3. The program will fail to compile, since the argument args passed to the static method main() cannot be passed on to the non-static method func().

  4. The program will fail to compile, since the method func() cannot assign the value of the static variable ref to the non-static variable arguments.

  5. The program will compile, but will throw an exception when run.

  6. The program will compile and run successfully.

4.23

Given the following member declarations, which statement is true?

int a;                             // (1)
static int a;                      // (2)
int f() { return a; }              // (3)
static int f() { return a; }       // (4)

Select the one correct answer.

  1. Declarations (1) and (3) cannot occur in the same class definition.

  2. Declarations (2) and (4) cannot occur in the same class definition.

  3. Declarations (1) and (4) cannot occur in the same class definition.

  4. Declarations (2) and (3) cannot occur in the same class definition.

4.24

Which statement is true?

Select the one correct answer.

  1. A static method can call other non-static methods in the same class by using the this keyword.

  2. A class may contain both static and non-static variables and both static and non-static methods.

  3. Each object of a class has its own instance of each static variable.

  4. Instance methods may access local variables of static methods.

  5. All methods in a class are implicitly passed a this parameter when called.

4.25

What, if anything, is wrong with the following code?

abstract class MyClass {
    transient int j;
    synchronized int k;

    final void MyClass() {}

    static void f() {}
}

Select the one correct answer.

  1. The class MyClass cannot be declared abstract.

  2. The field j cannot be declared transient.

  3. The field k cannot be declared synchronized.

  4. The method MyClass() cannot be declared final.

  5. The method f() cannot be declared static.

  6. Nothing is wrong with the code; it will compile without errors.

4.26

Which one of these is not a legal member declaration within a class?

Select the one correct answer.

  1. static int a;

  2. final Object[] fudge = { null };

  3. abstract int t;

  4. native void sneeze();

  5. final static private double PI = 3.14159265358979323846;

4.27

Which statements are true about modifiers?

Select the two correct answers.

  1. Abstract classes can contain final methods.

  2. Fields can be declared native.

  3. Non-abstract methods can be declared in abstract classes.

  4. Classes can be declared native.

  5. Abstract classes can be declared final.

4.28

Which statement is true?

Select the one correct answer.

  1. Transient fields will not be saved during serialization.

  2. Constructors can be declared abstract.

  3. The initial state of an array object constructed with the statement int[] a = new int[10] will depend on whether the array variable a is a local variable or a field.

  4. A subclass of a class with an abstract method must provide an implementation for the abstract method.

  5. Only static methods can access static members.