Top 24 Polymorphism Interview Questions and Answers

In this post, I will be sharing the most frequently asked polymorphism interview questions and answers in Java for both fresher and experienced professionals.

Read Also: Polymorphism quiz (multiple choice questions and answers)

Polymorphism Interview Questions and Answers

Q1 What is Polymorphism in Java?

You can find the answer here.

Q2 What are the different types of Polymorphism in Java?

There are two types of Polymorphism in Java:

1. Static Polymorphism (Compile time Polymorphism)

2. Dynamic Polymorphism (Runtime Polymorphism)

Q3 What is the difference between Polymorphism and Inheritance in Java?

The major differences between Polymorphism and Inheritance in Java are listed below:

1. Inheritance shows the parent-child relationship between two classes whereas Polymorphism takes the advantage of that relationship to make the program more dynamic.

2. One of the advantages of Inheritance is code reusability in child class. The child class inherits behavior from the parent class whereas Polymorphism enables the child class to redefine already defined behavior inside the parent class.

Q4 What is static polymorphism (compile time polymorphism) in Java?

Static Polymorphism is a form of polymorphism that decides which method to execute at compile time.

Q5 How you can achieve static polymorphism in Java?

Static polymorphism in Java can be achieved through Method overloading, Constructor overloading, and Operator overloading.

Q6 What is Method overloading in Java?

You can find the method overloading in detail here.

Q7 What is dynamic polymorphism (runtime polymorphism) in Java?

Dynamic polymorphism or runtime polymorphism is a form of polymorphism where a call to an overridden method is resolved at runtime rather than compile-time. It is also known as Dynamic method dispatch.

Q8 How you can achieve dynamic polymorphism in Java?

Dynamic polymorphism in Java can be achieved through Method overriding.

Q9 Output of this program is :

 class PolymorphismDemoClass {
    void m1(String x){
        System.out.println("One");
    }
}
class PolymorphismDemoClassChild extends PolymorphismDemoClass {

}
public class Test{
    public static void main(String[] args){
        PolymorphismDemoClass parent = new PolymorphismDemoClassChild();
        parent.m1(new PolymorphismDemoClass());
    }
}

a. Compile time error

b. Runtime error

c. Prints One

d. Compiles properly and prints nothing

The correct answer is A.



Q10 Which statements are true

a. Private methods can be overridden

b. Static binding means overloading

c. Overriding can only be done in subclass

d. Main method can be overloaded

The correct answers are B, C, AND D.



Q11 Output of the program is
 public class Boxing1 {
    static void method(int x) {
        System.out.println("prints int");
    }
    static void method(Integer i) { 
        System.out.println("prints Integer");
    }

    public static void main(String args[]){
        short no=10;
        method(no);
    }
}

a. Compile Time Error

b. prints int then prints Integer

c. prints int

d. prints Integer

The correct answer is C.



Q12 Output of the program is
 public class PolymorphismDemoClass3 {
    static void overloadedMethod(Integer x) {
        System.out.println("Integer");
    }
    static void overloadedMethod(Integer... x) {
        System.out.println("Integer...");
    }

    public static void main(String args[]) {
        int x=251;
        overloadedMethod(x);
    }
}

a. Integer

b. Integer...

c. Compile Time Error

d. Integer then Integer....

The correct answer is A.



Q13 Output of the program is
 public class PolymorphismDemoClass4{
    static void overloadedMethod(Long l) {
        System.out.println("Long");
    }

    public static void main(String args[]){
        int a=30;
        overloadedMethod(a);
    }
}

a. Compile Time Error

b. prints Long

c. Runtime Exception

d. None

The correct answer is A.



Q14 Output of the program is
 public class Test {
    public static void print (int i) {
        System.out.println("int");
    }
    public static void print (long i) {
        System.out.println("long");
    }   
    public static void main(String a[]) {
        int i = 5;
        Test.print(i);
    }
}

a. Compile Time Error

b. long

c. int

d. Runtime Exception

The correct answer is C.




Q15 Output of the program is
 public class Test2 {
    public static void print (Integer i) {
        System.out.println("Integer");
    }
    public static void print (Long i) {
        System.out.println("Long");
    }
    public static void main(String a[]) {
        int i = 5;
        Test2.print(i);
    }
}

a. Compile Time Error

b. Long

c. Integer

d. Runtime Exception

The correct answer is C.



Q16 Output of the program is
 public class Test3 {
    public static void print (Double i){
        System.out.println("Double");
    }
    public static void print (Long i){
        System.out.println("Long");
    }
    public static void main(String a[]){
        int i = 5;
        Test3.print(i);
    }
}

a. Compile Time Error

b. prints Long

c. prints Double

d. Runtime Exception

The correct answer is A.



Q17 Output of the program is
 public class Test4 {
    public static void print (Double i) {
        System.out.println("Double");
    }
    public static void print (Long i) {
        System.out.println("Long");
    }
    public static void print (Number i) {
        System.out.println("Number");
    }
    public static void main(String a[]) {
        int i = 5;
        Test4.print(i);
    }
}

a. Compile Time Error

b. Long

c. Double

d. Number

The correct answer is D.



Q18 Output of the program is
 public class Test5 {
    public static void print (Double i) {
        System.out.println("Double");
    }
    public static void print (Long i) {
        System.out.println("Long");
    }
    public static void print (Integer i) {
        System.out.println("Integer");
    }
    public static void main(String a[]){
        byte i = 5;
        Test5.print(i);
    }
}

a. Compile Time Error

b. prints Long

c. prints Double

d. prints Integer

The correct answer is A.



Q19 Output of the program is
 public class Test6 {
    public static void print (byte... i) {
        System.out.println("byte");
    }
    public static void print (long i) {
        System.out.println("long");
    }
    public static void print (Integer i) {
        System.out.println("Integer");
    }
    public static void main(String a[]) {
        byte i = 5;
        Test6.print(i);
    }
}

a. Compile Time Error

b. long

c. byte

d. Integer

The correct answer is B.




Q20 Output of the program is
 public class Test7 {
    public static void print (int i) {
        System.out.println("int");
    }
    public static void main(String a[]) {
        byte i = 5;
        Test7.print(i);
    }
}

a. Compile Time Error

b. int

c. Runtime error

The correct answer is B.



Q21 Output of the program is
 public class Test8 {
    public static void main (String[] args)
    {
        int i = 5;
        overloadedMethod(i);
    }

    static void overloadedMethod (Integer x)
    {
        System.out.println("Integer");
    }

    static void overloadedMethod (long x)
    {
        System.out.println("long");
    }
}

a. Compile Time Error

b. long

c. long Integer

d. Integer

The correct answer is B.



Q22 Output of the program is
 public class Test9 {
    public static void main (String[] args)
    {
        byte b = 5;
        overloadedMethod(b);
    }

    static void overloadedMethod (Object b)
    {
        Byte obj = (Byte)b;
        System.out.println("byte: "+obj);
    }
}

a. Compile Time Error

b. byte : memory address of object obj

c. byte: 5

d. Runtime error

The correct answer is C.



Q23 Output of the program is

 public class Test10
{
    // Overloaded methods
    public void overloadedMethod(Integer i)
    {
        System.out.println("Integer ");
    }
    public void overloadedMethod(String name)
    {
        System.out.println("String ");
    }
    public void overloadedMethod(Double name)
    {
        System.out.println("Double ");
    }

    // Driver code
    public static void main(String [] args)
    {
        Test10 obj = new Test10();
        obj.overloadedMethod(null);
    }
}

a. Compile Time Error

b. prints Double

c. prints Integer

d. prints String

The correct answer is A.


Q24 Output of the program is
 public class Test11
{
    // Overloaded methods
    public void overloadedMethod(Integer x)
    {
        System.out.println("Integer");
    }
    public void overloadedMethod(String name)
    {
        System.out.println("String");
    }
    public void overloadedMethod(Double name)
    {
        System.out.println("Double ");
    }

    public static void main(String [] args)
    {
        Test11 obj = new Test11();
        Integer arg = null;
        obj.overloadedMethod(arg);
    }
}

a. Compile Time Error

b. Double

c. Integer

d. String

The correct answer is C.


That's all for today. Please mention in the comments if you have faced any other Polymorphism interview question that is not present in the above list.

About The Author

Subham Mittal has worked in Oracle for 3 years.
Enjoyed this post? Never miss out on future posts by subscribing JavaHungry