Python Java C C++ HTML CSS JS

Java Practice Questions

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

Java Polymorphism Practice

Compile-time (overloading) and runtime (overriding) polymorphism.

1
Method Overloading - Area
Easy
Create a class Area with overloaded methods: double area(double r) returning 3.14*r*r, and double area(double l, double b) returning l*b. Read either one or two values...
Two lines: first an integer type (1 = circle, 2 = rectangle). Second line has radius for circle, or length breadth for rectangle.
A single integer: floor(area).
Sample Input 1
1
5
Sample Output 1
78
Sample Input 2
2
4 6
Sample Output 2
24
1
The compiler picks the overload by arguments.
2
Overloading by Parameter Count
Easy
Create a class Display with overloaded methods printVal(int a) printing the int, and printVal(String s) printing the string. Read an integer N and call printVal(N); no string is read.
A single integer N.
A single integer (printed by the int overload).
Sample Input 1
7
Sample Output 1
7
-10^6
Different parameter types select the overload.
1
Overloading with Different Types
Medium
Create a class Sum with methods int add(int a, int b) and double add(double a, double b). Read an integer X and a double Y. Print add(X, Y)...
Two tokens: an integer and a decimal number.
The double sum (add(X, Y)).
Sample Input 1
3 2.5
Sample Output 1
5.5
1
The double overload handles mixed args after widening.
2
Overriding toString
Medium
Create a class Student with name and marks, and override toString() to return "Student[name=, marks=]". Read a name and marks, create a Student, and print the object directly.
Two lines: name, then marks.
Student[name=, marks=]
Sample Input 1
Anil
92
Sample Output 1
Student[name=Anil, marks=92]
1
System.out.println(obj) calls toString().
3
Parent Reference Child Object
Medium
Create a class Animal with sound() printing "Animal sound" and a subclass Dog overriding it to print "Bark". In main: Animal a = new Dog(); a.sound();
No input.
Bark
Sample Input 1

                      
Sample Output 1
Bark
The object type (Dog) decides, not the reference.
4
equals() Override
Medium
Create a class Item with int id and override equals(Object o) to return true when ids match. Read two ids, create two Item objects, and print "Equal" or "Not Equal" using item1.equals(item2).
A single line with two integers id1 id2.
"Equal" or "Not Equal".
Sample Input 1
5 5
Sample Output 1
Equal
Sample Input 2
5 6
Sample Output 2
Not Equal
1
Compare the id fields, not references.
1
Dynamic Method Dispatch
Hard
Create a class Shape with double area() returning 0. Classes Circle (r, 3.14*r*r) and Rect (l,b, l*b) override it. Read 3 integers: type (1=circle, 2=rect) and its dimensions. Create the object as a Shape and print the integer part of area().
First line: type. Second line: dimensions.
A single integer: floor(area).
Sample Input 1
1
7
Sample Output 1
153
Sample Input 2
2
3 9
Sample Output 2
27
1
Dispatch happens at runtime.
2
Overriding with Super Call
Hard
Create class Base with a method void message() printing "Base message". Class Derived overrides message() to print "Derived message" then call super.message(). Call it on a Derived object.
No input.
Two lines: Derived message, then Base message.
Sample Input 1

                      
Sample Output 1
Derived message
Base message
super.message() invokes the parent version.
3
Covariant Return Type
Hard
Create a class Animal with Animal get() returning this. Subclass Dog overrides get() to return Dog. In main create a Dog, call get(), and print the class simple name using getClass().getSimpleName().
No input.
Dog
Sample Input 1

                      
Sample Output 1
Dog
Overriding may narrow the return type.
4
Type Casting (Up and Down)
Hard
Create class Animal and subclass Dog with an extra method void fetch() printing "Fetching". In main create a Dog, upcast it to Animal, then downcast back to Dog and call fetch().
No input.
Fetching
Sample Input 1

                      
Sample Output 1
Fetching
Downcast with (Dog) after an upcast.
Competitive MCQs — Java Polymorphism
Code snippets, output prediction, concepts & error spotting. Pick an answer to see instant feedback.
Score 0/ 33
Q1
What does polymorphism mean in Java?
Correct!
Wrong — correct answer is .
Polymorphism = many forms for one interface.
Q2
Which is compile-time polymorphism?
Correct!
Wrong — correct answer is .
Overloading is resolved at compile time by signatures.
Q3
Which is runtime polymorphism?
Correct!
Wrong — correct answer is .
Overriding is resolved at runtime by the object's actual type.
Q4
What is the output of this code?
java
1
2
3
4
5
6
7
8
9
10
11
12
class A {
    void f() {
        System.out.print("A");
    }
}
class B extends A {
    void f() {
        System.out.print("B");
    }
}
A x = new B();
x.f();
Correct!
Wrong — correct answer is .
The object is a B, so B's f() runs.
Q5
What is the output of this code?
class A {
void f() {
System.out.print("A");
}
}
class B extends A {
void f() {
System.out.print("B");
}
}
A x = new B();
x.f();
Correct!
Wrong — correct answer is .
The object is a B, so B's f() runs.
Q6
What is the output of this code?
java
1
2
3
4
5
6
7
8
9
class Calc {
    int add(int a, int b) {
        return a + b;
    }
    int add(int a, int b, int c) {
        return a + b + c;
    }
}
System.out.println(new Calc().add(1, 2, 3));
Correct!
Wrong — correct answer is .
The three-argument overload returns 1+2+3 = 6.
Q7
What is the output of this code?
class Calc {
int add(int a, int b) {
return a + b;
}
int add(int a, int b, int c) {
return a + b + c;
}
}
System.out.println(new Calc().add(1, 2, 3));
Correct!
Wrong — correct answer is .
The three-argument overload returns 1+2+3 = 6.
Q8
Which operator performs an explicit downcast?
Correct!
Wrong — correct answer is .
Downcasting uses a cast: (Dog) animal.
Q9
What is the output of this code?
java
1
2
3
4
5
6
7
8
9
10
11
12
class A {
    String who() {
        return "A";
    }
}
class B extends A {
    String who() {
        return "B";
    }
}
A[] arr = {new A(), new B()};
for (A a : arr) System.out.print(a.who());
Correct!
Wrong — correct answer is .
Each object's own who() runs → "AB".
Q10
What is the output of this code?
class A {
String who() {
return "A";
}
}
class B extends A {
String who() {
return "B";
}
}
A[] arr = {new A(), new B()};
for (A a : arr) System.out.print(a.who());
Correct!
Wrong — correct answer is .
Each object's own who() runs → "AB".
Q11
What is method overloading based on?
Correct!
Wrong — correct answer is .
Overloads differ in parameter lists; return type alone is not enough.
Q12
What is the output of this code?
java
1
2
3
Object o = "Java";
String s = (String) o;
System.out.println(s.length());
Correct!
Wrong — correct answer is .
The cast succeeds and length() returns 4.
Q13
What is the output of this code?
Object o = "Java";
String s = (String) o;
System.out.println(s.length());
Correct!
Wrong — correct answer is .
The cast succeeds and length() returns 4.
Q14
Polymorphism helps achieve:
Correct!
Wrong — correct answer is .
Polymorphic code handles objects by their common supertype.
Q15
What is the output of this code?
class A {
void f() {
System.out.print("A");
}
}
class B extends A {
void f() {
System.out.print("B");
}
}
A[] arr = {new A(), new B()};
for (A a : arr) a.f();
Correct!
Wrong — correct answer is .
Each element uses its runtime method: A then B.
Q16
What does overriding depend on?
Correct!
Wrong — correct answer is .
Overridden methods dispatch by the actual object type.
Q17
What is the output of this code?
class A {
int f() {
return 1;
}
}
class B extends A {
int f() {
return 2;
}
}
System.out.println(new A().f() + new B().f());
Correct!
Wrong — correct answer is .
A returns 1 and B returns 2, totaling 3.
Q18
What does upcasting mean?
Correct!
Wrong — correct answer is .
Upcasting narrows the visible interface to the parent type.
Q19
What is the output of this code?
class A {
void who() {
System.out.println("A");
}
}
class B extends A {
void who() {
System.out.println("B");
}
}
A a = new B();
a.who();
Correct!
Wrong — correct answer is .
Upcast references still dispatch to the runtime type B.
Q20
What is the output of this code?
class Calc {
int add(int a, int b) {
return a + b;
}
double add(double a, double b) {
return a + b;
}
}
System.out.println(new Calc().add(1.5, 2.5));
Correct!
Wrong — correct answer is .
The double overload matches, returning 4.0.
Q21
Which operator supports downcasting checks?
Correct!
Wrong — correct answer is .
instanceof verifies an object's type before downcasting.
Q22
What is the output of this code?
Object o = "hi";
if (o instanceof String s) {
System.out.println(s.toUpperCase());
}
Correct!
Wrong — correct answer is .
Pattern matching binds o to String s and uppercases it.
Q23
Which is a real-world example of polymorphism?
Correct!
Wrong — correct answer is .
One draw() call behaves differently per shape.
Q24
What is the output of this code?
class A {
String f() {
return "A";
}
}
class B extends A {
String f() {
return "B";
}
}
System.out.println(new B().f());
Correct!
Wrong — correct answer is .
The subclass override returns "B".
Q25
What is dynamic method dispatch?
Correct!
Wrong — correct answer is .
Overriding resolves dynamically in Java.
Q26
What is the output of this code?
class Animal {
String sound() {
return "generic";
}
}
class Cat extends Animal {
String sound() {
return "meow";
}
}
Animal a = new Cat();
System.out.println(a.sound());
Correct!
Wrong — correct answer is .
The runtime Cat type returns "meow".
Q27
Which is TRUE about overload resolution?
Correct!
Wrong — correct answer is .
The compiler selects the best matching overload.
Q28
What is the output of this code?
class A {
void f(int x) {
System.out.println("int");
}
void f(double x) {
System.out.println("double");
}
}
new A().f(5);
Correct!
Wrong — correct answer is .
The int argument matches the int overload exactly.
Q29
What is a polymorphic variable?
Correct!
Wrong — correct answer is .
Supertype references can hold subclass objects.
Q30
Which keyword is required for a subclass to call the overridden parent version?
Correct!
Wrong — correct answer is .
super.method() invokes the parent implementation.
Q31
What is the output of this code?
class A {
int f() {
return 10;
}
}
class B extends A {
int f() {
return super.f() + 5;
}
}
System.out.println(new B().f());
Correct!
Wrong — correct answer is .
super.f() gives 10, then B adds 5.
Q32
Which of these is NOT polymorphic behavior?
Correct!
Wrong — correct answer is .
Private methods are not dispatched polymorphically.
Q33
What is the output of this code?
interface Draw {
void draw();
}
class Circle implements Draw {
public void draw() {
System.out.println("circle");
}
}
Draw d = new Circle();
d.draw();
Correct!
Wrong — correct answer is .
Interface references dispatch to the implementing object.