Review Questions

graphics/rq_icon.gif

4.8

Which one of these is a valid method declaration?

Select the one correct answer.

  1. void method1 { /* ... */ }

  2. void method2() { /* ... */ }

  3. void method3(void) { /* ... */ }

  4. method4() { /* ... */ }

  5. method5(void) { /* ... */ }

4.9

Given the following code, which statements can be placed at the indicated position without causing compilation errors?

public class ThisUsage {
    int planets;
    static int suns;
    public void gaze() {
        int i;
        // ... insert statements here ...
    }
}

Select the three correct answers.

  1. i = this.planets;

  2. i = this.suns;

  3. this = new ThisUsage();

  4. this.i = 4;

  5. this.suns = planets;

4.10

Given the following pairs of method declarations, which statements are true?

void fly(int distance) {}
int  fly(int time, int speed) { return time*speed; }
void fall(int time) {}
int  fall(int distance) { return distance; }
void glide(int time) {}
void Glide(int time) {}

Select the two correct answers.

  1. The first pair of methods will compile correctly and overload the method name fly.

  2. The second pair of methods will compile correctly and overload the method name fall.

  3. The third pair of methods will compile correctly and overload the method name glide.

  4. The second pair of methods will not compile correctly.

  5. The third pair of methods will not compile correctly.

4.11

Given a class named Book, which one of these is a valid constructor declaration for the class?

Select the one correct answer.

  1. Book(Book b) {}

  2. Book Book() {}

  3. private final Book() {}

  4. void Book() {}

  5. public static void Book(String[] args) {}

  6. abstract Book() {}

4.12

Which statements are true?

Select the two correct answers.

  1. All classes must define a constructor.

  2. A constructor can be declared private.

  3. A constructor can return a value.

  4. A constructor must initialize all the fields of a class.

  5. A constructor can access the non-static members of a class.

4.13

What will be the result of attempting to compile the following program?

public class MyClass {
    long var;
    public void MyClass(long param) { var = param; }  // (1)
    public static void main(String[] args) {
        MyClass a, b;
        a = new MyClass();                            // (2)
        b = new MyClass(5);                           // (3)
    }
}

Select the one correct answer.

  1. A compilation error will occur at (1), since constructors cannot specify a return value.

  2. A compilation error will occur at (2), since the class does not have a default constructor.

  3. A compilation error will occur at (3), since the class does not have a constructor which takes one argument of type int.

  4. The program will compile correctly.