Python Java C C++ HTML CSS JS

Java Practice Questions

Topic-based coding problems with sample test cases — build confidence step by step.

Java Inheritance Practice

Extending classes, super keyword, method overriding and polymorphism basics.

1
Simple Inheritance
Easy
Create a base class Animal with a method void sound() that prints "Animal makes a sound". Create a subclass Dog that extends Animal and overrides sound() to print "Dog barks". Read nothing — in main, create a Dog and call sound().
No input.
A single line printed by sound().
Sample Input 1

                      
Sample Output 1
Dog barks
Override the method in the subclass.
2
Extends and Super Fields
Easy
Create a class Vehicle with field brand (String) and a constructor that sets it. Create a subclass Car extends Vehicle with field model (String). In main, read brand and model, create a Car, and print " ".
Two lines: brand, then model.
A single line: brand and model.
Sample Input 1
Toyota
Innova
Sample Output 1
Toyota Innova
1
Use super(brand) in the Car constructor.
3
Inherited Method Call
Easy
Create a class Shape with method void info() that prints "This is a shape". Create a subclass Rectangle extends Shape (no override). In main create a Rectangle and call info().
No input.
The line printed by info().
Sample Input 1

                      
Sample Output 1
This is a shape
Subclass inherits the method.
1
Super Keyword (Method)
Medium
Create a class Parent with a method void display() that prints "Parent display". Create a subclass Child extends Parent that overrides display() but first calls super.display() and then prints "Child display". In main, call display() on a Child.
No input.
Two lines: Parent display, then Child display.
Sample Input 1

                      
Sample Output 1
Parent display
Child display
Use super.display() inside the override.
2
Multi-level Inheritance
Medium
Create classes GrandParent, Parent (extends GrandParent), Child (extends Parent). Each has a method void show() that prints its own name. Create a Child in main and call show().
No input.
The single line printed by the resolved show().
Sample Input 1

                      
Sample Output 1
Child
The most-derived override runs.
3
Protected Field Access
Medium
Create a base class Account with protected double balance. A subclass SavingsAccount extends Account with a method void deposit(double amt) that adds to balance. Read a starting balance and a deposit, create a SavingsAccount, deposit, and print the final balance as an integer.
Two lines: balance, then deposit.
A single integer: the final balance.
Sample Input 1
1000
500
Sample Output 1
1500
1
Subclass can access protected field directly.
4
Constructor Chaining
Medium
Create class A with constructor printing "A constructor", class B extends A printing "B constructor", and class C extends B printing "C constructor". In main create a C and observe the output order.
No input.
Three lines in construction order.
Sample Input 1

                      
Sample Output 1
A constructor
B constructor
C constructor
Superclass constructors run first automatically.
1
Method Overriding - Area
Hard
Create a base class Shape with a method double area() that returns 0. Create subclasses Circle (radius) with area = 3.14*r*r and Rectangle (l, b) with area = l*b, overriding area(). Read a shape type ("circle" or "rectangle") and its dimensions, create the right object as a Shape reference, and print the integer part of its area.
First line: type. Second line: the dimensions (radius for circle; length breadth for rectangle).
A single integer: floor(area).
Sample Input 1
circle
5
Sample Output 1
78
Sample Input 2
rectangle
4 6
Sample Output 2
24
1
Polymorphism: call area() through the base reference.
2
Runtime Polymorphism
Hard
Create a class Animal with method void speak() printing "Animal speaks". Subclasses Dog and Cat override speak() to print "Dog barks" / "Cat meows". Read a word ("dog" or "cat"), create the object as an Animal reference, and call speak().
A single word: dog or cat.
The correct speak() output.
Sample Input 1
dog
Sample Output 1
Dog barks
Sample Input 2
cat
Sample Output 2
Cat meows
The runtime object decides the method.
3
Final Class and Method
Hard
Create a base class Base with a final method void greet() that prints "Hello from Base". Attempt to override it is not allowed — simply create a subclass Sub extends Base (no override) and call greet() in main.
No input.
The greeting line.
Sample Input 1

                      
Sample Output 1
Hello from Base
Final methods cannot be overridden.
4
Inheritance with Arguments
Hard
Create a base class Employee with field name and constructor. A subclass Manager extends Employee with an extra field teamSize. Read name and teamSize, create a Manager, and print "Manager: leads people".
Two lines: name, then teamSize.
Manager: leads people
Sample Input 1
Priya
12
Sample Output 1
Manager: Priya leads 12 people
1
Pass name via super() and store teamSize.
5
IS-A Relationship Check
Hard
Create class Animal, subclass Dog extends Animal. In main, read nothing and print "true" if a Dog is an instanceof Animal, then "true" if Dog is an instanceof Object.
No input.
Two lines: true, then true.
Sample Input 1

                      
Sample Output 1
true
true
Use the instanceof operator.
Competitive MCQs — Java Inheritance
Code snippets, output prediction, concepts & error spotting. Pick an answer to see instant feedback.
Score 0/ 33
Q1
Which keyword makes a class inherit from another?
Correct!
Wrong — correct answer is .
extends declares a subclass; implements is for interfaces.
Q2
What is the output of this code?
java
1
2
3
4
5
6
7
class A {
    void show() {
        System.out.println("A");
    }
}
class B extends A { }
new B().show();
Correct!
Wrong — correct answer is .
B inherits show() from A.
Q3
What is the output of this code?
class A {
void show() {
System.out.println("A");
}
}
class B extends A { }
new B().show();
Correct!
Wrong — correct answer is .
B inherits show() from A.
Q4
What does the super keyword do?
Correct!
Wrong — correct answer is .
super accesses parent members and constructors.
Q5
What is the output of this code?
java
1
2
3
4
5
6
7
8
9
10
11
class A {
    void show() {
        System.out.println("A");
    }
}
class B extends A {
    void show() {
        System.out.println("B");
    }
}
new B().show();
Correct!
Wrong — correct answer is .
B overrides show(), so the child version runs.
Q6
What is the output of this code?
class A {
void show() {
System.out.println("A");
}
}
class B extends A {
void show() {
System.out.println("B");
}
}
new B().show();
Correct!
Wrong — correct answer is .
B overrides show(), so the child version runs.
Q7
Which Java keyword allows a class to implement multiple contracts?
Correct!
Wrong — correct answer is .
Java supports single class inheritance but multiple interfaces.
Q8
What is the output of this code?
java
1
2
3
4
5
6
7
8
9
10
11
12
class A {
    A() {
        System.out.print("A");
    }
}
class B extends A {
    B() {
        super();
        System.out.print("B");
    }
}
new B();
Correct!
Wrong — correct answer is .
The parent constructor runs first (explicit super()), then "B".
Q9
What is the output of this code?
class A {
A() {
System.out.print("A");
}
}
class B extends A {
B() {
super();
System.out.print("B");
}
}
new B();
Correct!
Wrong — correct answer is .
The parent constructor runs first (explicit super()), then "B".
Q10
What is method overriding?
Correct!
Wrong — correct answer is .
Overriding changes behaviour in the subclass.
Q11
Which keyword prevents a class from being extended?
Correct!
Wrong — correct answer is .
A final class cannot be subclassed.
Q12
What is the output of this code?
java
1
2
3
4
5
6
7
8
9
10
11
12
class Animal {
    void speak() {
        System.out.println("...");
    }
}
class Dog extends Animal {
    void speak() {
        System.out.println("Woof");
    }
}
Animal a = new Dog();
a.speak();
Correct!
Wrong — correct answer is .
Polymorphism: the runtime type Dog decides → "Woof".
Q13
What is the output of this code?
class Animal {
void speak() {
System.out.println("...");
}
}
class Dog extends Animal {
void speak() {
System.out.println("Woof");
}
}
Animal a = new Dog();
a.speak();
Correct!
Wrong — correct answer is .
Polymorphism: the runtime type Dog decides → "Woof".
Q14
Which built-in function checks inheritance at runtime?
Correct!
Wrong — correct answer is .
obj instanceof Class tests the object's type chain.
Q15
What is the output of this code?
class A {
int x = 1;
}
class B extends A {
int x = 2;
}
System.out.println(new B().x);
Correct!
Wrong — correct answer is .
The field in B shadows the inherited field, so x is 2.
Q16
What does a subclass inherit?
Correct!
Wrong — correct answer is .
Subclasses inherit accessible members; private ones are not directly visible.
Q17
What is the output of this code?
class A {
void show() {
System.out.println("A");
}
}
class B extends A {
void show() {
System.out.println("B");
}
}
A ref = new B();
ref.show();
Correct!
Wrong — correct answer is .
Method calls use the runtime type, so B's show() runs.
Q18
Which keyword prevents a class from being inherited?
Correct!
Wrong — correct answer is .
A final class cannot be extended.
Q19
What is the output of this code?
class A {
A() {
System.out.println("A");
}
}
class B extends A {
B() {
super();
System.out.println("B");
}
}
new B();
Correct!
Wrong — correct answer is .
super() runs the parent constructor first, then B's body.
Q20
Can a Java class extend two classes directly?
Correct!
Wrong — correct answer is .
Java allows only one direct superclass; use interfaces for more.
Q21
Which keyword allows implementing multiple contracts?
Correct!
Wrong — correct answer is .
A class implements any number of interfaces.
Q22
What is the output of this code?
class Animal {
void speak() {
System.out.println("...");
}
}
class Dog extends Animal {
void speak() {
System.out.println("Woof");
}
}
new Dog().speak();
Correct!
Wrong — correct answer is .
Dog overrides speak(), so its own version runs.
Q23
Which annotation marks an overriding method?
Correct!
Wrong — correct answer is .
@Override lets the compiler verify a real override.
Q24
What is the output of this code?
class A {
int add(int a, int b) {
return a + b;
}
}
class B extends A {
int add(int a, int b) {
return a + b + 100;
}
}
System.out.println(new B().add(1, 2));
Correct!
Wrong — correct answer is .
B overrides add(), adding 100 to the sum.
Q25
Which keyword calls a parent method from a subclass?
Correct!
Wrong — correct answer is .
super.method() invokes the parent implementation.
Q26
What is the output of this code?
class A {
void show() {
System.out.print("A");
}
}
class B extends A {
void show() {
super.show();
System.out.print("B");
}
}
new B().show();
Correct!
Wrong — correct answer is .
super.show() prints A, then the child prints B.
Q27
What is an "is-a" relationship?
Correct!
Wrong — correct answer is .
extends models the is-a relationship.
Q28
What is the output of this code?
class A {
void f() {
System.out.println("A");
}
}
class B extends A {
void f() {
System.out.println("B");
}
}
A a = new B();
((A) a).f();
Correct!
Wrong — correct answer is .
Casting to A does not change the runtime type, so B's f() runs.
Q29
Which of these is TRUE about constructors and inheritance?
Correct!
Wrong — correct answer is .
Java inserts super() to initialize the parent before the child.
Q30
What is the output of this code?
class A {
static void who() {
System.out.println("A");
}
}
class B extends A {
static void who() {
System.out.println("B");
}
}
A ref = new B();
ref.who();
Correct!
Wrong — correct answer is .
Static methods bind to the reference type A, not the runtime type.
Q31
Which of these prevents overriding?
Correct!
Wrong — correct answer is .
A final method cannot be overridden in subclasses.
Q32
What is the output of this code?
class A {
int x = 10;
}
class B extends A {
int getX() {
return x;
}
}
System.out.println(new B().getX());
Correct!
Wrong — correct answer is .
B inherits the field x, so getX() returns 10.
Q33
What is the hierarchy of all Java classes rooted at?
Correct!
Wrong — correct answer is .
Every class implicitly extends java.lang.Object.