Working with Inheritance practice questions

From OCA Java SE 7 (1Z0-803) · 25 questions on this topic

Working with Inheritance practice questions from OCA Java SE 7 (1Z0-803). This pack has 25 questions tagged Working with Inheritance, drawn from its timed mock exams. 8 of them are worked through in full below — the question, every option, why each is right or wrong, and the explanation.

Worked examples for Working with Inheritance

  1. Question 1

    Which of the following statements about Java 7 interfaces are true?

    1. A. Interface methods are implicitly public and abstractCorrect answer

      Correct: in Java 7 every interface method is implicitly public and abstract.

    2. B. Interface fields are implicitly public, static, and finalCorrect answer

      Correct: interface fields are implicitly public, static, and final constants that must be initialized at declaration.

    3. C. An interface may declare static methods with bodies

      Wrong: static (and default) methods with bodies in interfaces arrived in Java 8; a Java 7 interface cannot declare them.

    4. D. A class may implement multiple interfacesCorrect answer

      Correct: a class may implement any number of interfaces, which is a core purpose of interfaces.

    5. E. An interface field may be reassigned by an implementing class

      Wrong: interface fields are implicitly final constants, so an implementing class can never reassign them.

    Explanation

    In Java 7, every interface method is public abstract (`Interface methods are implicitly public and abstract`) and every field is a public static final constant that must be initialized at declaration (`Interface fields are implicitly public, static, and final`) — hence `An interface field may be reassigned by an implementing class` is impossible. Multiple interface implementation is the whole point of interfaces (`A class may implement multiple interfaces`). Static and default methods with bodies arrived in Java 8, so `An interface may declare static methods with bodies` is false for this exam.

  2. Question 2

    What is the output of the following program? ```java public class Main { public static void main(String[] args) { new C(); System.out.println(); } } class A { A() { System.out.print("A"); } } class B extends A { B() { System.out.print("B"); } } class C extends B { C() { System.out.print("C"); } } ```

    1. A. CBA

      Wrong: reverses the order. The implicit super() runs each superclass constructor body before the subclass body, so the topmost A prints first, not last.

    2. B. C

      Wrong: assumes only C's constructor executes. Each constructor's implicit super() chains all the way up, so A's and B's bodies also print.

    3. C. Compilation fails because the constructors never call super()

      Wrong: the compiler automatically inserts an implicit super() into any constructor that lacks an explicit this()/super(), so the missing calls are not an error.

    4. D. ABCCorrect answer

      Correct: implicit super() chains to the top before any body runs, executing A, then B, then C for output "ABC".

    Explanation

    Every constructor that doesn't start with an explicit this() or super() gets an implicit super() inserted by the compiler (so `Compilation fails because the constructors never call super()` is wrong). new C() therefore chains to the top before any body runs: A's body first, then B's, then C's — "ABC".

  3. Question 3

    What is the output of the following program? ```java public class Main extends P { int v = val(); static int val() { System.out.print("V"); return 1; } Main() { System.out.print("M"); } public static void main(String[] args) { new Main(); System.out.println(); } } class P { P() { System.out.print("P"); } } ```

    1. A. PVMCorrect answer

      super() runs the parent constructor first (P), then the subclass field initializer (V), then the subclass constructor body (M).

    2. B. VPM

      The parent constructor runs before the subclass's field initializers, so P precedes V.

    3. C. MVP

      The constructor body runs last, not first, and the parent constructor runs first, so P comes before M.

    4. D. PV

      This omits the subclass constructor body M, which runs after the field initializer.

    Explanation

    Instance creation order: the implicit super() runs the parent constructor first (P), then the subclass's field initializers (V), then its constructor body (M): "PVM". Field initializers always run between super() and the constructor body.

  4. Question 4

    A Car class declares a field of type Engine and delegates work to it. Which relationship does this design demonstrate?

    1. A. IS-A (inheritance)

      IS-A would mean Car extends Engine, but a car is not a kind of engine; holding a field is composition.

    2. B. HAS-A (composition)Correct answer

      Holding a reference to another type is composition: Car HAS-an Engine.

    3. C. Method overriding

      Overriding is about redefining inherited methods, not about one class holding another as a field.

    4. D. Polymorphism

      Polymorphism is runtime dispatch on subtype, not the HAS-a relationship of holding a field.

    Explanation

    Holding a reference to another type is composition — Car HAS-an Engine. IS-A would mean `class Car extends Engine`, which is semantically wrong: a car is not a kind of engine. The exam tests choosing composition vs inheritance by meaning.

  5. Question 5

    What is the result of compiling the following program? ```java public class Main { public static void main(String[] args) { Tool t = new Drill(); System.out.println(t.bit()); } } class Tool { String name() { return "tool"; } } class Drill extends Tool { String name() { return "drill"; } String bit() { return "bit"; } } ```

    1. A. bit

      Wrong: bit() is not visible through the Tool reference type at compile time, so this value is never produced.

    2. B. drill

      Wrong: the call is t.bit(), not name(), and it does not compile because Tool has no bit() method.

    3. C. An exception is thrown at runtime

      Wrong: the error is a compile-time "cannot find symbol", not a runtime exception.

    4. D. Compilation failsCorrect answer

      Correct: the reference type Tool has no bit() method, so the call cannot be resolved and compilation fails, even though the Drill object defines it.

    Explanation

    The REFERENCE type decides which members are visible at compile time. t is declared as Tool, and Tool has no bit() method — "cannot find symbol" — even though the actual object is a Drill that does. Either declare t as Drill or cast: ((Drill) t).bit().

  6. Question 6

    What is the output of the following program? ```java public class Main extends Base { public static void main(String[] args) { System.out.println(new Main().code); } } class Base { protected int code = 7; } ```

    1. A. 0

      The inherited code field keeps its initialized value 7, not the default 0.

    2. B. Compilation fails because code is protected

      protected members are accessible to subclasses, so Main can read the inherited field.

    3. C. 7Correct answer

      Main inherits Base's protected code field, already initialized to 7.

    4. D. Compilation fails because Main does not declare code

      Main inherits code from Base, so it does not need to declare it itself.

    Explanation

    protected members are inherited and fully accessible to subclasses (`Compilation fails because code is protected`). Main IS-a Base, so every Main instance carries the inherited code field, already initialized to 7 (`Compilation fails because Main does not declare code`).

  7. Question 7

    What is the output of the following program? ```java public class Main { public static void main(String[] args) { Object o = new java.util.ArrayList<String>(); System.out.println((o instanceof java.util.List) + " " + (o instanceof String)); } } ```

    1. A. true true

      The object is an ArrayList, not a String, so the second instanceof is false.

    2. B. true falseCorrect answer

      The ArrayList IS-a List (true) and is not a String (false).

    3. C. false false

      An ArrayList is a List, so the first instanceof is true, not false.

    4. D. Compilation fails because o cannot be tested against String

      instanceof against String compiles because Object relates to String; it errors only for provably unrelated types.

    Explanation

    The object is an ArrayList, which IS-a List (true) and is not a String (false). Testing an Object reference against String compiles fine (`Compilation fails because o cannot be tested...`) — instanceof is only a compile error when the types are provably unrelated, and Object relates to everything.

  8. Question 8

    What is the result of compiling the following program? ```java public class Main { public static void main(String[] args) { new C().go(); } } class P { final void go() { System.out.println("p"); } } class C extends P { void go() { System.out.println("c"); } } ```

    1. A. Compilation failsCorrect answer

      A final method cannot be overridden, so C's go() is rejected by the compiler.

    2. B. c

      C's go() cannot override the final parent method, so the code does not compile to print c.

    3. C. p

      The override attempt is a compile error, so the program never runs to print p.

    4. D. An exception is thrown at runtime

      The final-method violation is caught at compile time, not at runtime.

    Explanation

    A final method can never be overridden — C's go() is rejected ("overridden method is final"). final on a method freezes its implementation for the whole hierarchy; final on a class forbids subclassing entirely.

Practise all 25 Working with Inheritance questions

OCA Java SE 7 has the full set, inside timed mock exams that mirror real exam conditions — every question with a worked explanation.

Open OCA Java SE 7

Other topics in this pack