Polymorphism Questions with Answers

34 practice questions on Polymorphism (OOP Concepts) with answers and step-by-step solutions. Try 10 samples below, then practise all 34 free in the AptiNest app.

Sample questions

Question 1Easy

Which of the following can distinguish two overloaded methods in Java?

  1. Only the access modifier
  2. The number or types of parameters
  3. Only the thrown exceptions
  4. Only the return type

Answer: B. The number or types of parameters

Explanation

Overloads must differ in parameter list. A difference only in return type, modifiers or throws clause is not enough.

Step-by-step solution

  1. Overloaded methods share a name but have different signatures.
  2. Java tells overloads apart by the number or types of their parameters.
  3. So the correct answer is option B.

Why the other options are wrong

  • A. Access modifiers do not change the method signature.
  • C. Thrown exceptions are not part of the overload signature.
  • D. The return type alone cannot distinguish overloads.

Tip: Overloads differ in parameters, never by return type alone.

Question 2Easy

What does this Python code print?

class Account:
    def fee(self):
        return 10

class Premium(Account):
    def fee(self):
        return super().fee() * 2 - 5

accs = [Account(), Premium(), Premium()]
print(sum(a.fee() for a in accs))
  1. 30
  2. 60
  3. 50
  4. 40

Answer: D. 40

Explanation

Account gives 10 and each Premium gives 10 * 2 - 5 = 15. Total 10 + 15 + 15 = 40.

Step-by-step solution

  1. Account.fee() returns 10.
  2. Premium.fee() returns super().fee() × 2 − 5 = 10 × 2 − 5 = 15, for each of the two Premium objects.
  3. The total is 10 + 15 + 15 = 40; index 3 is correct.

Why the other options are wrong

  • A. 30 would mean each Premium gives 10; the formula gives 15.
  • B. 60 means each Premium gives 25, which adds 5 instead of subtracting it.
  • C. 50 means each Premium gives 20, which skips the − 5.

Tip: When summing method results, work out each object's own override first, then add.

Question 3Easy

Method overloading is an example of:

  1. Compile-time (static) polymorphism
  2. Encapsulation
  3. Runtime (dynamic) polymorphism
  4. Multiple inheritance

Answer: A. Compile-time (static) polymorphism

Explanation

The compiler chooses among overloads by their parameter lists. Overriding is resolved at runtime.

Step-by-step solution

  1. Overloading means the same method name with different parameter lists.
  2. The compiler picks the version from the argument types before the program runs.
  3. That is compile-time, or static, polymorphism, so the answer is compile-time (static) polymorphism.

Why the other options are wrong

  • B. Encapsulation is hiding data behind methods; overloading is not about hiding data.
  • C. Runtime polymorphism is overriding, where the object's real class picks the method when it runs.
  • D. Multiple inheritance means inheriting from several classes; overloading has nothing to do with it.

Tip: Overloading is decided at compile time; overriding is decided at runtime.

Question 4Easy

What is the output of this C++ program?

#include <iostream>
struct Vec {
    int x, y;
    Vec operator+(const Vec& o) const { return {x + o.x, y + o.y}; }
    Vec operator*(int k) const { return {x * k, y * k}; }
};
int main() {
    Vec a{1, 2}, b{3, 4};
    Vec c = a + b * 2;
    std::cout << c.x << "," << c.y << "\n";
}
  1. 14,20
  2. 8,12
  3. 4,6
  4. 7,10

Answer: D. 7,10

Explanation

Overloaded operators keep their normal precedence, so b * 2 = (6, 8) is computed first, then a + (6, 8) = (7, 10).

Step-by-step solution

  1. b * 2 is (3 × 2, 4 × 2) = (6, 8); multiplication comes before addition.
  2. a + (6, 8) is (1 + 6, 2 + 8) = (7, 10).
  3. The output is "7,10"; index 3 is correct.

Why the other options are wrong

  • A. 14,20 doubles the sum again; the × 2 applies only to b, not to the total.
  • B. 8,12 is (a + b) × 2, which ignores the precedence of multiplication.
  • C. 4,6 is a + b with no multiplication at all.

Tip: Multiplication binds tighter than addition, as in arithmetic: work out b * 2 first.

Question 5Medium

What is the output of this Java program?

class Money {
    int rupees;
    Money(int r) { rupees = r; }
    public String toString() { return "₹" + rupees; }
}
public class Main {
    public static void main(String[] args) {
        Money m = new Money(50);
        System.out.println("Total: " + m + 1);
    }
}
  1. Total: ₹50 1
  2. Total: ₹51
  3. Total: 51
  4. Total: ₹501

Answer: D. Total: ₹501

Explanation

Concatenation goes left to right: "Total: " + m calls the overridden toString() giving "Total: ₹50", and then 1 is appended as text.

Step-by-step solution

  1. m.toString() returns "₹" + rupees, which is ₹50.
  2. "Total: " + m joins the text, giving "Total: ₹50".
  3. Then + 1 adds the text 1, not a number, giving "Total: ₹501".
  4. Option D is "Total: ₹501", so the answer is D.

Why the other options are wrong

  • A. The 1 is joined to the text, so there is no space and no separate 1; the output has 501 together.
  • B. The 1 is joined to the text, not added to 50, so 51 is wrong.
  • C. The output keeps the text 'Total:' and the rupee amount, so 51 alone is wrong.

Tip: In Java, string + object + number joins from the left, so the number becomes text.

Question 6Medium

What is a covariant return type?

  1. An overriding method returning a subtype of the overridden method's return type
  2. A generic method with a wildcard return
  3. A method that returns different, unrelated types depending on the values of its input arguments
  4. An overload that differs only in return type

Answer: A. An overriding method returning a subtype of the overridden method's return type

Explanation

Java and C++ allow an override to return a more specific type, for example Animal clone() overridden by Dog clone().

Step-by-step solution

  1. Covariant return type lets subclass methods return subtype of superclass method's return type.
  2. This allows more specific types in overriding, like Dog instead of Animal.
  3. Example: Animal clone() overridden by Dog clone() shows covariant return type.
  4. It improves type safety and code clarity by matching return types with actual objects.
  5. Correct answer is option A as it matches this definition exactly.

Why the other options are wrong

  • B. A wildcard return is a generics feature, not a covariant return.
  • C. Unrelated return types chosen by input values do not fit covariance, which needs a subtype.
  • D. Overloads cannot differ only by return type in Java; covariance is about overriding a method.

Tip: Remember: covariant return types make overriding more precise by returning subtypes.

Question 7Medium

Python's 'duck typing' means:

  1. Methods are bound only once at import time
  2. An object's suitability is decided by the methods it supports, not its declared type
  3. All objects must inherit from a common Duck class
  4. Every type is checked strictly at compile time, so an object must declare which interfaces it implements

Answer: B. An object's suitability is decided by the methods it supports, not its declared type

Explanation

If an object has the needed methods ('walks and quacks like a duck'), it can be used, regardless of its class hierarchy.

Step-by-step solution

  1. Duck typing checks if an object has required methods.
  2. It does not care about the object's declared type.
  3. Object's suitability depends on methods it supports.
  4. So, an object's suitability is decided by the methods it supports, not its declared type.
  5. Final answer: An object's suitability is decided by the methods it supports, not its declared type.

Why the other options are wrong

  • A. Method binding happens at call time in Python, not once at import.
  • C. Python classes need not share a base; duck typing does not require a common Duck class.
  • D. Duck typing is a runtime check of methods; Python does not check declared interfaces at compile time.

Tip: Check if an object can perform needed actions, not its exact type.

Question 8Medium

Python does not support classic method overloading by signature. What happens when a class defines two methods named f with different parameters?

  1. A SyntaxError is raised
  2. The first definition is kept
  3. The later definition replaces the earlier one
  4. Both are kept and chosen by argument count

Answer: C. The later definition replaces the earlier one

Explanation

A def statement simply binds a name in the class namespace, so a second def f rebinds it; only the last definition survives.

Step-by-step solution

  1. First method f is defined and bound to the class.
  2. Second method f redefines the same name, replacing the first one.
  3. Python allows redefining functions in a class, last definition wins.
  4. So the later definition replaces the earlier one.
  5. Final answer: The later definition replaces the earlier one.

Why the other options are wrong

  • A. Python does not raise a SyntaxError for this; redefining a name is allowed.
  • B. The first definition is not kept; the name is bound again to the later function.
  • D. Both are not kept; Python has no overloading by argument count, so only one remains.

Tip: In Python, later method definitions in a class override earlier ones.

Question 9Hard

What is the output of this Java program?

public class Main {
    static void f(int x)    { System.out.print("int "); }
    static void f(long x)   { System.out.print("long "); }
    static void f(double x) { System.out.print("double "); }
    public static void main(String[] args) {
        byte b = 1;
        f(b);
        f(5L);
        f(2.0f);
    }
}
  1. int int double
  2. Compilation error: no f(byte)
  3. long long double
  4. int long double

Answer: D. int long double

Explanation

Overload resolution picks the most specific widening conversion: byte widens to int first, 5L is a long, and float widens to double.

Step-by-step solution

  1. Byte b = 1; calls f(int) because byte widens to int.
  2. f(5L); calls f(long) because 5L is a long literal.
  3. f(2.0f); calls f(double) because float widens to double.
  4. Final output: int long double.

Why the other options are wrong

  • A. f(5L) matches f(long) exactly, so the second word is long, and f(b) with a byte picks int, not a repeated int.
  • B. There is no f(byte), but byte widens to int, so the call compiles and picks f(int) as the most specific match.
  • C. f(b) with a byte widens to int and picks f(int), so the first word is int, not long.

Tip: Remember widening conversions for primitive types in method overloads.

Question 10Hard

How does a typical C++ implementation perform a virtual function call?

  1. By recompiling the call at runtime
  2. By searching the method name in a per-class hash table of strings on every single call
  3. By inlining every possible override
  4. Through a per-object pointer to the class's vtable, indexing the function's slot

Answer: D. Through a per-object pointer to the class's vtable, indexing the function's slot

Explanation

Each polymorphic object holds a vptr to its class's table of function pointers; a virtual call loads the vptr and jumps to the entry for that function.

Step-by-step solution

  1. A C++ object has a vptr pointing to its vtable.
  2. The vtable contains pointers to all virtual functions of the class.
  3. To call a virtual function, the program uses the vptr to locate the correct function in the vtable.
  4. It then jumps to the function's address from the vtable.
  5. Final answer: Through a per-object pointer to the class's vtable, indexing the function's slot.

Why the other options are wrong

  • A. No recompiling happens at runtime; the call goes through the vtable pointer.
  • B. Methods are not looked up by name in a hash table on each call in typical C++.
  • C. Inlining every override is impossible when the type is only known at runtime.

Tip: Remember: vtable and vptr are key to virtual function calls in C++.

Practise all 34 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all OOP Concepts topics · All topics