LearnFrenzy
Sign in Take a free test

OOPs · Topic 4 of 10

Polymorphism: questions with answers

Compile-time vs run-time polymorphism, overloading rules, overriding and dynamic dispatch, virtual functions, operator overloading, duck typing, and the traps: static types in overload resolution, default arguments, equals(Object).

Questions
45
Timed sets
3
Answers run or computed
37
Your accuracy
–

What this topic covers

15 subtopics
  • Overloading11
  • Operator overloading6
  • Overriding6
  • Virtual functions5
  • Basics4
  • Duck typing3
  • Dispatch2
  • Design1
  • Downcasting1
  • Interfaces1
  • Overriding rules1
  • Parametric polymorphism1
Show all 15 subtopics
  • Pattern matching1
  • Static polymorphism1
  • Upcasting1
  • Beginner 15
  • Intermediate 15
  • Advanced 15

About this topic

Polymorphism lets one interface work with many types. Compile-time polymorphism (overloading, templates, generics) is decided by the compiler from the static types of the arguments. Run-time polymorphism (overriding, virtual functions) is decided by the object's actual class. Most trick questions mix the two: an overloaded method chosen by the declared type, or a default argument taken from the base class while the override runs. Every program here was run to produce its answer.

Free preview

Sample questions

Try each one first, then open the answer for the explanation and why the other options are wrong.

Question 1 Overriding Beginner Practice Code executed

What does this print?

Java Main.java
public class Main {
    static class Animal { String sound() { return "..."; } }
    static class Dog extends Animal { @Override String sound() { return "Woof"; } }
    static class Cat extends Animal { @Override String sound() { return "Meow"; } }
    static class Fish extends Animal { }

    public static void main(String[] args) {
        Animal[] zoo = { new Dog(), new Cat(), new Fish() };
        StringBuilder out = new StringBuilder();
        for (Animal a : zoo) out.append(a.sound()).append(' ');
        System.out.println(out.toString().trim());
    }
}
Woof Meow Woof
Compile error
... ... ...
Woof Meow ...
Show answer and explanation
Correct answer D

Explanation

The loop only knows it has Animals, yet each call runs the method of the object's real class. Fish does not override, so it inherits Animal's version.

Why the other options are wrong

  • AFish inherits "...".
  • BEvery element is an Animal.
  • COverrides are dispatched by the real type.
Question 2 Overloading Intermediate Practice Code executed

The variable holds a String but is declared as Object. What does this print?

Java Main.java
public class Main {
    static String show(Object o) { return "object"; }
    static String show(String s) { return "string"; }

    public static void main(String[] args) {
        Object o = "hello";
        String s = "hello";
        System.out.println(show(o) + " " + show(s) + " " + show((String) o));
    }
}
string string string
object string string
object object string
object string object
Show answer and explanation
Correct answer B

Explanation

Overload selection uses the declared (static) type at compile time, not the object's run-time type. Only overriding is dynamic. A cast changes the static type.

Why the other options are wrong

  • Ao is declared as Object.
  • Cs is declared as String.
  • DThe cast makes it a String.
Question 3 Dispatch Advanced Practice Code executed

Overloading and overriding together. What does this print?

Java Main.java
public class Main {
    static class Animal { String name() { return "animal"; } }
    static class Dog extends Animal { @Override String name() { return "dog"; } }

    static String vet(Animal a) { return "vet(Animal:" + a.name() + ")"; }
    static String vet(Dog d)    { return "vet(Dog:" + d.name() + ")"; }

    public static void main(String[] args) {
        Animal a = new Dog();
        System.out.println(vet(a) + " " + vet(new Dog()));
    }
}
vet(Animal:animal) vet(Dog:dog)
vet(Dog:dog) vet(Dog:dog)
vet(Animal:dog) vet(Dog:dog)
vet(Dog:animal) vet(Dog:dog)
Show answer and explanation
Correct answer C

Explanation

Two decisions at two times: the compiler picks vet(Animal) from the declared type of a; at run time, a.name() is dispatched to Dog. Java is single-dispatch.

Why the other options are wrong

  • Aname() is overridden.
  • BThe overload is chosen from the static type.
  • DBoth parts are reversed.
42 more questions in the timed setsSame format as the samples, with a timer, a question palette and a full review at the end.
Start practising

OOPs

All 10 topics

Topics, levels and full mocks
  1. 01 Classes, Objects and Constructors45 questions · 3 levels
  2. 02 Encapsulation and Access Modifiers45 questions · 3 levels
  3. 03 Inheritance45 questions · 3 levels
  4. 04 Polymorphism45 questions · 3 levels
  5. 05 Abstract Classes and Interfaces45 questions · 3 levels
  6. 06 Object Lifecycle and Copying45 questions · 3 levels
  7. 07 static, final, this and super45 questions · 3 levels
  8. 08 Exception Handling45 questions · 3 levels
  9. 09 SOLID and Class Relationships45 questions · 3 levels
  10. 10 Design Patterns45 questions · 3 levels

Search LearnFrenzy

Esc

Quick links