Inheritance Practice Questions

AP (Advanced Placement) · AP Computer Science A · 150 free MCQs with instant results and detailed explanations.

150
Total
45
Easy
75
Medium
30
Hard

Start Practicing Inheritance

Take a timed quiz or customize your practice session

Quick Quiz (10 Qs) → Mock Test (25 Qs) ⚙ Customize

Sample Questions from Inheritance

Here are 10 sample questions. Start a quiz to get randomized questions with scoring.

Q1
Easy
What is inheritance in object-oriented programming?
A. A mechanism to create new classes using existing classes.
B. A method to hide class details from users.
C. A way to execute code from multiple classes at the same time.
D. A process of making a class static.
Show Answer & Explanation
Correct Answer: A
Inheritance allows a new class to inherit properties and methods from an existing class, promoting code reuse.
Q2
Easy
Which of the following correctly demonstrates single inheritance in Java?
A. class Child extends Parent { }
B. class Child implements Parent { }
C. class Parent extends Child { }
D. class Parent { } class Child { }
Show Answer & Explanation
Correct Answer: A
In Java, single inheritance is achieved using the 'extends' keyword, where one class (Child) inherits from another (Parent).
Q3
Easy
Considering the following classes: class A { int x; } class B extends A { int y; } What is the result of the code 'B b = new B(); b.x = 5;'?
A. It will compile successfully and set b.x to 5.
B. It will cause a compilation error since x is private.
C. It will compile but set b.x to 0.
D. It will throw a runtime exception.
Show Answer & Explanation
Correct Answer: A
Class B inherits from class A, therefore it can access public and protected members of A. The code will compile and set b.x to 5 without issues.
Q4
Medium
Which of the following statements about inheritance in Java is true?
A. A subclass can override methods of its superclass.
B. A subclass cannot have more than one superclass.
C. All methods in a superclass must be marked as 'public' to be inherited.
D. Inheritance allows a class to inherit attributes but not methods.
Show Answer & Explanation
Correct Answer: A
A subclass can override methods of its superclass, allowing for dynamic method dispatch. This is a fundamental feature of inheritance in Java.
Q5
Medium
What will be the output of the following code snippet? class Animal { void sound() { System.out.println("Animal sound"); } } class Dog extends Animal { void sound() { System.out.println("Bark"); } } public class Test { public static void main(String[] args) { Animal myDog = new Dog(); myDog.sound(); } }
A. Animal sound
B. Bark
C. Compilation error
D. No output
Show Answer & Explanation
Correct Answer: B
The output will be 'Bark' because the method 'sound()' in class Dog overrides the method 'sound()' in class Animal, demonstrating polymorphism.
Q6
Medium
Which type of inheritance is NOT supported in Java?
A. Single Inheritance
B. Multiple Inheritance
C. Hierarchical Inheritance
D. Multilevel Inheritance
Show Answer & Explanation
Correct Answer: B
Java does not support multiple inheritance directly to avoid ambiguity and complexity, particularly the 'diamond problem'. This is why interfaces are used for similar purposes.
Q7
Medium
Consider the following classes. If class B extends class A and class C extends class B, which of the following can be used to create an object of class C?
A. C obj = new B();
B. A obj = new C();
C. B obj = new A();
D. C obj = new C();
Show Answer & Explanation
Correct Answer: D
The correct syntax to create an object of class C is 'C obj = new C();'. This creates an instance of C, which is valid since C is a class.
Q8
Hard
Consider the following class hierarchy: `class Animal { void sound() { System.out.println("Animal sound"); } } class Dog extends Animal { void sound() { System.out.println("Bark"); } }` What will be the output of the following code? `Animal myDog = new Dog(); myDog.sound();`
A. Bark
B. Animal sound
C. Compile-time error
D. Runtime error
Show Answer & Explanation
Correct Answer: A
The output will be 'Bark' because the method `sound()` is overridden in the `Dog` class, and the reference type `Animal` points to a `Dog` object, invoking the overridden method.
Q9
Hard
If you have a class `Vehicle` that has a method `fuelEfficiency()`, and a subclass `Car` that overrides `fuelEfficiency()` to return a specific fuel consumption value, what will happen if you try to call `fuelEfficiency()` on a `Vehicle` reference that actually points to a `Car` object?
A. It will call the `Vehicle` version of `fuelEfficiency()`.
B. It will call the `Car` version of `fuelEfficiency()`.
C. It will throw a NullPointerException.
D. It will result in a compilation error.
Show Answer & Explanation
Correct Answer: B
It will call the `Car` version of `fuelEfficiency()` because of method overriding, which allows the subclass's implementation to be executed even when referenced by the superclass.
Q10
Hard
Consider the following class hierarchy: class A has a method display() that prints 'Class A', and class B extends class A, overriding the display() method to print 'Class B'. If an object of type B is created but referenced by a variable of type A, what will be printed when display() is called?
A. Class A
B. Class B
C. Error: display() cannot be called
D. Class A and Class B
Show Answer & Explanation
Correct Answer: B
In Java, method overriding allows a subclass (B) to provide a specific implementation of a method that is already defined in its superclass (A). When display() is called on a B object, even when referenced as type A, the overridden method in B is executed, resulting in 'Class B' being printed.

Showing 10 of 150 questions. Start a quiz to practice all questions with scoring and timer.

Practice All 150 Questions →

Inheritance โ€” AP (Advanced Placement) AP Computer Science A Practice Questions Online

This page contains 150 practice MCQs for the chapter Inheritance in AP (Advanced Placement) AP Computer Science A. The questions are organized by difficulty โ€” 45 easy, 75 medium, 30 hard โ€” so you can choose the right level for your preparation.

Every question includes a detailed explanation to help you understand the concept, not just memorize answers. Take a timed quiz to simulate exam conditions, or practice at your own pace with no time limit.