Review Questions
3.1 Which one of these declarations is a valid method declaration?
Select the one correct answer.
void method1 { /* ... */ }
void method2() { /* ... */ }
void method3(void) { /* ... */ }
method4() { /* ... */ }
method5(void) { /* ... */ }
3.2 Which statements, when inserted at (1), will not result in compile-time errors?
public class ThisUsage { int planets; static int suns; public void gaze() { int i; // (1) INSERT STATEMENT HERE } }
Select the three correct answers.
i = this.planets;
i = this.suns;
this = new ThisUsage();
this.i = 4;
this.suns = planets;
3.3 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.
The first pair of methods will compile, and overload the method name fly.
The second pair of methods will compile, and overload the method name fall.
The third pair of methods will compile, and overload the method name glide.
The first pair of methods will not compile.
The second pair of methods will not compile.
The third pair of methods will not compile.
3.4 Given a class named Book, which one of these constructor declarations is valid for the class Book?
Select the one correct answer.
Book(Book b) {}
Book Book() {}
private final Book() {}
void Book() {}
public static void Book(String[] args) {}
abstract Book() {}
3.5 Which statements are true?
Select the two correct answers.
A class must define a constructor.
A constructor can be declared private.
A constructor can return a value.
A constructor must initialize all fields when a class is instantiated.
A constructor can access the non-static members of a class.
3.6 What will be the result of compiling 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.
A compile-time error will occur at (1).
A compile-time error will occur at (2).
A compile-time error will occur at (3).
The program will compile without errors.