Java Inheritance
Test your understanding of Java inheritance, superclasses, subclasses, method overriding, and the super keyword.
Questions in this quiz
20 questions on Java Inheritance. Work through them here, then start the quiz to answer them and see your score.
1.What keyword does a class use to inherit from another class in Java?
- Asuper
- Bextends
- Cimplements
- Dinherits
2.In Java, a class can directly extend how many classes?
- AUnlimited
- B2
- C1
- D3
3.What does the super keyword do when called in a constructor?
- AOverrides the parent method
- BCalls the parent class static method
- CCreates a new parent object
- DCalls the parent class constructor
4.Which of the following is true about method overriding?
- AThe overriding method must be static
- BThe overriding method must have a different method name
- CThe overriding method must have the same signature as the parent method
- DThe overriding method must have a different return type
5.What does this code print?
class Animal { String sound() { return "..."; } } class Dog extends Animal { String sound() { return "Woof"; } } public class Main { public static void main(String[] args) { Animal a = new Dog(); System.out.println(a.sound()); } }- AWoof
- B...
- CCompilation error
- Dnull
6.Which annotation should you use when overriding a method to catch mistakes early?
- A@Replace
- B@Override
- C@Super
- D@Overwrite
7.What is the root class of all Java classes?
- AClass
- BObject
- CBase
- DRoot
8.Can a child class access private members of its parent class directly?
- ANo, never directly
- BYes, always
- CYes, using the super keyword
- DYes, but only in the constructor
9.What does this code print?
class Vehicle { int speed = 60; } class Car extends Vehicle { int speed = 100; void show() { System.out.println(super.speed + " " + speed); } } public class Main { public static void main(String[] args) { new Car().show(); } }- A100 100
- B100 60
- C60 60
- D60 100
10.Which access modifier allows a member to be accessed by child classes but not outside the package?
- Adefault
- Bprivate
- Cprotected
- Dpublic
11.What happens if a parent class has no no-arg constructor and the child does not call super() explicitly?
- AIt compiles fine and runs with null
- BCompilation error
- CRuntime NullPointerException
- DThe child uses a default Object constructor
12.What is constructor chaining?
- AUsing static initialiser blocks
- BCalling the same constructor multiple times
- COverloading constructors with the same parameters
- DLinking constructors using this() or super()
13.What does this code print?
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 "); } } public class Main { public static void main(String[] args) { new C(); } }- AA B C
- BA
- CC B A
- DC
14.Can a final class be extended in Java?
- AYes, but only within the same package
- BYes, any class can be extended
- CNo, final classes cannot be extended
- DOnly abstract classes can extend final classes
15.What is the difference between method overloading and method overriding?
- AOverloading is compile-time, overriding is runtime
- BBoth are resolved at runtime
- COverriding is compile-time, overloading is runtime
- DBoth are resolved at compile time
16.What does this code print?
class Parent { void greet() { System.out.println("Hello from Parent"); } } class Child extends Parent { @Override void greet() { super.greet(); System.out.println("Hello from Child"); } } public class Main { public static void main(String[] args) { new Child().greet(); } }- AHello from Child
- BHello from Child Hello from Parent
- CHello from Parent
- DHello from Parent Hello from Child
17.Which of the following CANNOT be overridden?
- Astatic methods
- Bprotected methods
- Cpublic methods
- Dabstract methods
18.What is an "IS-A" relationship?
- AA class that contains another class as a field
- BA static relationship between classes
- CThe relationship created by inheritance or interface implementation
- DTwo classes in the same package
19.What is the output of this code?
class Shape { static String type = "Shape"; } class Circle extends Shape { static String type = "Circle"; } public class Main { public static void main(String[] args) { Shape s = new Circle(); System.out.println(s.type); } }- ACompilation error
- BCircle
- Cnull
- DShape
20.Which statement about the instanceof operator is correct?
- AIt can only be used with primitive types
- BIt returns true if the object is an instance of the class or any of its parent classes
- CIt returns true only if the types match exactly
- DIt throws an exception if the object is null