1Z0-811 Java Class Design 3 — Questions and Answers
Question 1: What is the effect of declaring a method `abstract` in a non-abstract class?
- The method has an empty body
- Compilation error — the class must also be abstract (Correct answer)
- The method is ignored at runtime
- The method becomes private
Correct answer: Compilation error — the class must also be abstract
A class containing an abstract method must itself be declared abstract, or a compile error results.
Question 2: Which statement correctly describes constructor chaining using `this()`?
- `this()` may appear anywhere in the constructor body
- `this()` calls the parent class constructor
- `this()` must be the first statement in the constructor (Correct answer)
- `this()` can be used in static methods
Correct answer: `this()` must be the first statement in the constructor
A `this()` constructor call must appear as the very first statement in the constructor body.
Question 3: Which of the following best describes method overloading?
- Same method name, same parameter list, different return type
- Same method name, different parameter lists in the same class (Correct answer)
- A subclass providing its own implementation of a parent method
- Using the `@Override` annotation on a method
Correct answer: Same method name, different parameter lists in the same class
Overloading means defining multiple methods with the same name but different parameter signatures in the same class.
Question 4: What does the `instanceof` operator return when the reference is null?
- Throws NullPointerException
- true
- false (Correct answer)
- Compilation error
Correct answer: false
`null instanceof AnyType` always evaluates to `false` without throwing an exception.
Question 5: In Java, a static nested class differs from an inner class because it:
- Cannot have any methods
- Does not hold an implicit reference to the outer class instance (Correct answer)
- Must be declared public
- Can only be instantiated inside the outer class
Correct answer: Does not hold an implicit reference to the outer class instance
A static nested class has no implicit reference to the enclosing instance, unlike a non-static inner class.
Question 6: Which of the following is a valid reason to use an abstract class instead of an interface?
- To allow multiple inheritance of type
- To provide shared instance state (fields) and partial implementation (Correct answer)
- To define a contract without any implementation
- To mark a class as non-instantiable only
Correct answer: To provide shared instance state (fields) and partial implementation
Abstract classes can hold instance fields and provide partial concrete implementation, which interfaces cannot do with state.
Question 7: What is the output of the following code? ```java class A { void greet() { System.out.print("A"); } } class B extends A { void greet() { super.greet(); System.out.print("B"); } } public class T { public static void main(String[] a) { new B().greet(); } }```
- A
- B
- AB (Correct answer)
- BA
Correct answer: AB
`super.greet()` prints "A" first, then the rest of B's greet prints "B", giving "AB".
What is the effect of declaring a method `abstract` in a non-abstract class?