Bird
Raised Fist0
Javaprogramming~20 mins

Compile-time polymorphism in Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Challenge - 5 Problems
πŸŽ–οΈ
Compile-time Polymorphism Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of overloaded method calls
What is the output of the following Java program that demonstrates method overloading?
Java
public class Test {
    void display(int a) {
        System.out.println("Integer: " + a);
    }
    void display(String a) {
        System.out.println("String: " + a);
    }
    public static void main(String[] args) {
        Test obj = new Test();
        obj.display(10);
        obj.display("Hello");
    }
}
AInteger: 10\nString: Hello
BInteger: 10\nInteger: Hello
CString: 10\nString: Hello
DCompilation error due to ambiguous method calls
Attempts:
2 left
πŸ’‘ Hint
Look at the method signatures and which one matches the argument type.
❓ Predict Output
intermediate
2:00remaining
Overloaded method with type promotion
What will be the output when the following Java code runs?
Java
public class Demo {
    void show(int a) {
        System.out.println("int: " + a);
    }
    void show(long a) {
        System.out.println("long: " + a);
    }
    public static void main(String[] args) {
        Demo d = new Demo();
        d.show(100);
        d.show(100L);
    }
}
Along: 100\nlong: 100
Bint: 100\nlong: 100
Cint: 100\nint: 100
DCompilation error due to ambiguous method call
Attempts:
2 left
πŸ’‘ Hint
Check which method matches the argument type exactly and which uses type promotion.
πŸ”§ Debug
advanced
2:00remaining
Identify the compile-time error in overloaded methods
Why does the following Java code cause a compile-time error?
Java
public class Sample {
    void process(int a, double b) {
        System.out.println("int and double");
    }
    void process(double a, int b) {
        System.out.println("double and int");
    }
    public static void main(String[] args) {
        Sample s = new Sample();
        s.process(5, 10);
    }
}
ANo error; output is 'int and double'
BCompilation error due to duplicate method signatures
CCompilation error due to ambiguous method call s.process(5, 10)
DNo error; output is 'double and int'
Attempts:
2 left
πŸ’‘ Hint
Consider how Java chooses overloaded methods when both are applicable.
πŸ“ Syntax
advanced
2:00remaining
Which overloaded method signature is invalid?
Which of the following overloaded method declarations in Java is invalid?
Avoid calculate(int a, int a) {}
Bint calculate(int a, int b) {}
Cvoid calculate(int a, int b) {}
Dvoid calculate(double a, int b) {}
Attempts:
2 left
πŸ’‘ Hint
Check the parameter names and whether they are unique within the method signature.
πŸš€ Application
expert
3:00remaining
Determine the output of overloaded constructors
What is the output when the following Java program runs?
Java
public class Box {
    int width, height;
    Box() {
        this(10);
        System.out.println("Default constructor");
    }
    Box(int w) {
        this(w, 20);
        System.out.println("One-arg constructor");
    }
    Box(int w, int h) {
        width = w;
        height = h;
        System.out.println("Two-arg constructor");
    }
    public static void main(String[] args) {
        Box b = new Box();
        System.out.println("Width: " + b.width + ", Height: " + b.height);
    }
}
AOne-arg constructor\nTwo-arg constructor\nDefault constructor\nWidth: 10, Height: 20
BDefault constructor\nOne-arg constructor\nTwo-arg constructor\nWidth: 10, Height: 20
CCompilation error due to recursive constructor calls
DTwo-arg constructor\nOne-arg constructor\nDefault constructor\nWidth: 10, Height: 20
Attempts:
2 left
πŸ’‘ Hint
Trace the constructor calls starting from the default constructor.

Practice

(1/5)
1.

What is compile-time polymorphism in Java?

easy
A. Using different method names for different tasks
B. Changing the method behavior at runtime based on object type
C. Using the same method name with different parameters in the same class
D. Creating multiple classes with the same name

Solution

  1. Step 1: Understand method overloading

    Compile-time polymorphism is also called method overloading, where methods share the same name but differ in parameters.
  2. Step 2: Differentiate from runtime polymorphism

    Runtime polymorphism uses method overriding, changing behavior based on object type at runtime, not compile-time.
  3. Final Answer:

    Using the same method name with different parameters in the same class -> Option C
  4. Quick Check:

    Compile-time polymorphism = method overloading [OK]
Hint: Same method name, different parameters means compile-time polymorphism [OK]
Common Mistakes:
  • Confusing compile-time with runtime polymorphism
  • Thinking method overriding is compile-time polymorphism
  • Believing different method names are polymorphism
2.

Which of the following is the correct syntax for method overloading in Java?

public class Calculator {
    public int add(int a, int b) { return a + b; }
    public int add(int a, int b, int c) { ? }
}
easy
A. return a + b + c;
B. return a + b;
C. return a * b * c;
D. return a - b - c;

Solution

  1. Step 1: Check method parameters

    The second add method has three parameters, so it should add all three values.
  2. Step 2: Write correct return statement

    Return the sum of a, b, and c to correctly overload the add method.
  3. Final Answer:

    return a + b + c; -> Option A
  4. Quick Check:

    Overloaded method sums all parameters [OK]
Hint: Overloaded methods must handle all their parameters correctly [OK]
Common Mistakes:
  • Returning sum of only two parameters in three-parameter method
  • Using wrong operators like multiplication or subtraction
  • Syntax errors like missing semicolon
3.

What will be the output of the following Java code?

class Demo {
    void show(int a) { System.out.println("Int: " + a); }
    void show(String a) { System.out.println("String: " + a); }
}
public class Test {
    public static void main(String[] args) {
        Demo d = new Demo();
        d.show(5);
        d.show("Hello");
    }
}
medium
A. Int: 5\nString: Hello
B. String: 5\nInt: Hello
C. Int: 5\nInt: Hello
D. Compilation error

Solution

  1. Step 1: Identify overloaded methods

    There are two show methods: one takes int, the other takes String.
  2. Step 2: Match method calls to parameters

    d.show(5) calls show(int), printing "Int: 5"; d.show("Hello") calls show(String), printing "String: Hello".
  3. Final Answer:

    Int: 5\nString: Hello -> Option A
  4. Quick Check:

    Method overloading calls correct method by parameter type [OK]
Hint: Method chosen by parameter type at compile time [OK]
Common Mistakes:
  • Confusing parameter types and outputs
  • Expecting runtime polymorphism behavior
  • Thinking it causes compilation error
4.

Find the error in this code snippet related to compile-time polymorphism:

class Test {
    void display(int a) { System.out.println(a); }
    void display(int a, int b) { System.out.println(a + b); }
    void display(int a) { System.out.println(a * 2); }
}
medium
A. Incorrect method parameter types
B. Duplicate method display(int a) causes compilation error
C. Missing return type in one method
D. No error, code compiles fine

Solution

  1. Step 1: Check method signatures

    Two methods have the exact same name and parameter list: display(int a).
  2. Step 2: Understand method overloading rules

    Method overloading requires different parameter lists; duplicate signatures cause compilation error.
  3. Final Answer:

    Duplicate method display(int a) causes compilation error -> Option B
  4. Quick Check:

    Duplicate method signatures cause compile error [OK]
Hint: Overloaded methods must differ in parameter list [OK]
Common Mistakes:
  • Thinking method bodies affect overloading
  • Ignoring duplicate parameter lists
  • Assuming code compiles without error
5.

Consider this class:

class Printer {
    void print(int a) { System.out.println("Number: " + a); }
    void print(String a) { System.out.println("Text: " + a); }
    void print(int a, String b) { System.out.println(a + " and " + b); }
}

Which call will cause a compile-time error?

hard
A. print("Test")
B. print(10, "Hello")
C. print(5)
D. print("Hello", 10)

Solution

  1. Step 1: Check method signatures

    Methods accept (int), (String), and (int, String) parameters.
  2. Step 2: Match call parameters

    print("Hello", 10) tries to call (String, int), which does not exist, causing compile error.
  3. Final Answer:

    print("Hello", 10) -> Option D
  4. Quick Check:

    No matching method for (String, int) call [OK]
Hint: Check parameter order and types carefully for overloaded methods [OK]
Common Mistakes:
  • Assuming parameter order doesn't matter
  • Thinking all combinations are allowed
  • Ignoring method signature mismatch