OOP & Inheritance Questions with Answers

23 practice questions on OOP & Inheritance (Core Java) with answers and step-by-step solutions. Try 10 samples below, then practise all 23 free in the AptiNest app.

Sample questions

Question 1Easy

What happens when this code is compiled?

class A {
    A(int x) { }
}

class B extends A {
    B() { }
}

public class Main {
    public static void main(String[] args) {
        new B();
    }
}
  1. Runtime error: NoSuchMethodError
  2. It compiles and runs with no output
  3. It compiles; x defaults to 0
  4. Compilation error: A has no no-argument constructor

Answer: D. Compilation error: A has no no-argument constructor

Explanation

B() implicitly calls super(), but A only declares A(int). The compiler reports an error because no matching constructor exists.

Step-by-step solution

  1. B() has no explicit super call, so Java inserts super() on its own.
  2. Class A has only A(int x) and no no-argument constructor.
  3. The compiler cannot find a matching A() constructor and reports an error.
  4. So the code does not compile; option D is the correct answer.

Why the other options are wrong

  • A. This is a compile-time error, not a runtime NoSuchMethodError.
  • B. The code does not compile, so nothing runs.
  • C. The code does not compile; x is only a constructor parameter in A, not a field set to 0.

Tip: If a superclass has only constructors with parameters, every subclass constructor must call super(...) with matching arguments.

Question 2Easy

Which access modifier makes a member visible within its own package and to subclasses in other packages?

  1. private
  2. public only through getters
  3. protected
  4. default (no modifier)

Answer: C. protected

Explanation

protected = package access plus access from subclasses anywhere. Default (package-private) access does not extend to subclasses in other packages.

Step-by-step solution

  1. Private: visible only inside the same class.
  2. default (no modifier): visible only inside the same package, so subclasses in other packages cannot see it.
  3. Protected: visible inside the same package and to subclasses anywhere.
  4. So the answer is protected, option C.

Why the other options are wrong

  • A. Private hides the member from the same package and from subclasses too.
  • B. Getters are a design pattern, not an access modifier, so they do not make a member visible to subclasses by themselves.
  • D. Default access stops at the package boundary, so subclasses in other packages cannot see the member.

Tip: Public means everywhere, protected means package plus subclasses, default means package only, private means the class only.

Question 3Easy

What is the output?

class Point {
    int x, y;
    Point(int x, int y) { this.x = x; this.y = y; }
}

public class Main {
    public static void main(String[] args) {
        Point a = new Point(1, 2);
        Point b = new Point(1, 2);
        System.out.println((a == b) + " " + a.equals(b));
    }
}
  1. true true
  2. false true
  3. true false
  4. false false

Answer: D. false false

Explanation

a and b are different objects, so == is false. Point does not override equals, so Object.equals also compares references.

Step-by-step solution

  1. A and b are two separate Point objects, each created with new.
  2. a == b compares references, and the two references differ, so it is false.
  3. Point does not override equals, so a.equals(b) uses Object.equals, which also compares references and gives false.
  4. The output is false false, option D.

Why the other options are wrong

  • A. Two separate objects are never == each other, so the first value is not true.
  • B. Equals is not overridden, so it compares references and gives false, not true.
  • C. The == check is false because the objects differ, so the first value cannot be true.

Tip: Two new objects are never == each other. To compare contents, override equals and hashCode.

Question 4Easy

Which statement about method overloading in Java is correct?

  1. Overloaded methods must differ in return type only
  2. Overloading is resolved at runtime using the object type
  3. Overloaded methods must be in different classes
  4. Overloaded methods must differ in their parameter lists

Answer: D. Overloaded methods must differ in their parameter lists

Explanation

Overloads share a name but need different parameter types, count or order. They are resolved at compile time; a different return type alone is not enough.

Step-by-step solution

  1. Overloads share the name.
  2. They must differ in parameter list.
  3. They are resolved at compile time.

Why the other options are wrong

  • A. Return type alone is not enough.
  • B. Overloading is compile-time; overriding is runtime.
  • C. Overloads can be in the same class.

Tip: Parameters decide the overload.

Question 5Medium

What is the output?

class Parent { }

class Child extends Parent { }

public class Main {
    public static void main(String[] args) {
        Parent p = new Parent();
        Child c = (Child) p;
        System.out.println("done");
    }
}
  1. done
  2. null
  3. ClassCastException at runtime
  4. Compilation error

Answer: C. ClassCastException at runtime

Explanation

A downcast compiles because Child is a subtype of Parent, but the object is a plain Parent, so the cast fails at runtime.

Step-by-step solution

  1. new Parent() creates a plain Parent object.
  2. Casting to Child compiles since Child is a subtype.
  3. At runtime the object is not a Child, so the cast fails.
  4. ClassCastException.

Why the other options are wrong

  • A. The cast fails before 'done' is reached.
  • B. No null is printed.
  • D. Downcasts between related classes compile.

Tip: Compile-time looks at the reference, runtime at the object.

Question 6Medium

What is the output?

class Parent { }

class Child extends Parent { }

public class Main {
    public static void main(String[] args) {
        Object o = new Child();
        System.out.println((o instanceof Parent) + " " + (o instanceof Runnable));
    }
}
  1. false false
  2. true true
  3. Compilation error
  4. true false

Answer: D. true false

Explanation

A Child is-a Parent, so the first test is true. Child does not implement Runnable, so the second is false; the check compiles because o is of type Object.

Step-by-step solution

  1. Object o is assigned a Child instance, which is a Parent subclass.
  2. O instanceof Parent checks if o is a Parent or subclass, which it is. So result is true.
  3. O instanceof Runnable checks if o implements Runnable, but Child does not. So result is false.
  4. The code compiles because Object can be checked against any interface.
  5. Final output is 'true false'.

Why the other options are wrong

  • A. The first check is true, since a Child object is a Parent, so the result cannot be false false.
  • B. Child does not implement Runnable, so the second check is false and the output is not true true.
  • C. instanceof with an interface on an Object reference compiles, so the code does not fail to compile.

Tip: Remember: instanceof works for classes and interfaces. If not implemented, it returns false.

Question 7Medium

What is the output?

class Parent {
    String name = "P";
    String getName() { return name; }
}

class Child extends Parent {
    String name = "C";
    String getName() { return name; }
}

public class Main {
    public static void main(String[] args) {
        Parent p = new Child();
        System.out.println(p.name + " " + p.getName());
    }
}
  1. P P
  2. C P
  3. C C
  4. P C

Answer: D. P C

Explanation

Fields are not polymorphic: p.name uses the declared type Parent. getName() is overridden, so Child.getName() returns Child's field "C".

Step-by-step solution

  1. Fields are not overridden: p.name uses the declared type Parent, so it is P.
  2. getName() is overridden in Child, so p.getName() runs Child's version.
  3. Child's getName() returns Child's own name field, which is C.
  4. The output is P C, which is option D, the correct answer.

Why the other options are wrong

  • A. p.name is P, but getName() returns C from Child.
  • B. p.name uses the declared type Parent, so it is P, not C.
  • C. p.name uses the declared type Parent, so it is P, not C.

Tip: Fields follow the declared type at compile time, but overridden methods follow the actual object at run time.

Question 8Medium

What is the output?

class Counter {
    private int count;
    Counter increment() { count++; return this; }
    int get() { return count; }
}

public class Main {
    public static void main(String[] args) {
        Counter c = new Counter();
        System.out.println(c.increment().increment().increment().get());
    }
}
  1. 3
  2. Compilation error
  3. 1
  4. 0

Answer: A. 3

Explanation

increment() returns this, so the calls are chained on the same object and count is incremented three times.

Step-by-step solution

  1. c.increment() increments count to 1.
  2. c.increment().increment() increments count to 2.
  3. c.increment().increment().increment() increments count to 3.
  4. get() returns count which is 3.
  5. Final answer: 3.

Why the other options are wrong

  • B. The code compiles, since each method returns this and the chained calls are valid.
  • C. 1 would need only one increment, but three increment calls run in the chain.
  • D. 0 would be right only if no increment ran, but each call adds one to count.

Tip: Use method chaining for concise code when methods return the object itself.

Question 9Hard

What is the output?

class A {
    void m(int x) { System.out.print("A.int "); }
    void m(long x) { System.out.print("A.long "); }
}

class B extends A {
    void m(long x) { System.out.print("B.long "); }
}

public class Main {
    public static void main(String[] args) {
        A a = new B();
        a.m(1);
    }
}
  1. B.long
  2. Compilation error
  3. A.long
  4. A.int

Answer: D. A.int

Explanation

At compile time m(int) is the best overload for argument 1. B only overrides m(long), so at runtime the inherited A.m(int) runs.

Step-by-step solution

  1. At compile time a has type A, so the compiler looks only at A's methods.
  2. The argument 1 is an int, so m(int) is the exact match and is chosen.
  3. B overrides only m(long) and does not override m(int), so A's m(int) is inherited unchanged.
  4. At runtime the inherited A.m(int) runs and prints A.int, option D.

Why the other options are wrong

  • A. B's m(long) is never called, because the compiler chose m(int).
  • B. The code compiles: a.m(1) matches A's m(int), and B inherits it.
  • C. A's m(long) would only be picked for a long argument; the int argument picks m(int).

Tip: The compile-time type chooses the overload. The runtime object only changes which body runs for that exact signature.

Question 10Hard

What is the output?

public class Main {
    static void f(Object o) { System.out.print("Object "); }
    static void f(String s) { System.out.print("String "); }

    public static void main(String[] args) {
        f(null);
        Object x = "hi";
        f(x);
    }
}
  1. String Object
  2. Object Object
  3. String String
  4. Compilation error

Answer: A. String Object

Explanation

For null, the most specific applicable overload (String) wins. For x, overloads are chosen by the declared type Object, not the runtime type.

Step-by-step solution

  1. f(null): both f(Object) and f(String) accept null, and String is more specific, so f(String) runs and prints String.
  2. Object x = "hi": overloads are chosen by the declared type Object, not by the runtime value.
  3. f(x) therefore picks f(Object), which prints Object.
  4. The output is String Object, option A.

Why the other options are wrong

  • B. The null call goes to f(String) because it is more specific, so the first word is not Object.
  • C. The second call uses the declared type Object, so it cannot pick f(String).
  • D. The code compiles, because null matches both overloads and String is more specific, so there is no ambiguity.

Tip: For null, the most specific overload wins. For a variable, Java uses its compile-time type.

Practise all 23 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Core Java topics · All topics