0
0
Javaprogramming~20 mins

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

Choose your learning style9 modes available
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.