| 4.8 | Which one of these is a valid method declaration? Select the one correct answer. -
void method1 { /* ... */ } -
void method2() { /* ... */ } -
void method3(void) { /* ... */ } -
method4() { /* ... */ } -
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. -
i = this.planets; -
i = this.suns; -
this = new ThisUsage(); -
this.i = 4; -
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. -
The first pair of methods will compile correctly and overload the method name fly . -
The second pair of methods will compile correctly and overload the method name fall . -
The third pair of methods will compile correctly and overload the method name glide . -
The second pair of methods will not compile correctly. -
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. -
Book(Book b) {} -
Book Book() {} -
private final Book() {} -
void Book() {} -
public static void Book(String[] args) {} -
abstract Book() {} |
| 4.12 | Which statements are true? Select the two correct answers. -
All classes must define a constructor. -
A constructor can be declared private . -
A constructor can return a value. -
A constructor must initialize all the fields of a class. -
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. -
A compilation error will occur at (1), since constructors cannot specify a return value. -
A compilation error will occur at (2), since the class does not have a default constructor. -
A compilation error will occur at (3), since the class does not have a constructor which takes one argument of type int . -
The program will compile correctly. |