Challenge - 5 Problems
Compile-time Polymorphism Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
β Predict Output
intermediate2: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"); } }
Attempts:
2 left
π‘ Hint
Look at the method signatures and which one matches the argument type.
β Incorrect
The method display is overloaded with one version accepting an int and another accepting a String. The calls match exactly one method each, so the output prints the respective labels and values.
β Predict Output
intermediate2: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); } }
Attempts:
2 left
π‘ Hint
Check which method matches the argument type exactly and which uses type promotion.
β Incorrect
The call with 100 matches the int version exactly. The call with 100L matches the long version exactly. So the output prints int: 100 and long: 100.
π§ Debug
advanced2: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); } }
Attempts:
2 left
π‘ Hint
Consider how Java chooses overloaded methods when both are applicable.
β Incorrect
The call s.process(5, 10) is ambiguous because both methods can accept two int arguments by type promotion (int to double). The compiler cannot decide which method to call, causing a compile-time error.
π Syntax
advanced2:00remaining
Which overloaded method signature is invalid?
Which of the following overloaded method declarations in Java is invalid?
Attempts:
2 left
π‘ Hint
Check the parameter names and whether they are unique within the method signature.
β Incorrect
Method parameters must have unique names. Option A declares two parameters with the same name 'a', which is a syntax error.
π Application
expert3: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); } }
Attempts:
2 left
π‘ Hint
Trace the constructor calls starting from the default constructor.
β Incorrect
The default constructor calls the one-arg constructor with 10. The one-arg constructor calls the two-arg constructor with (10, 20). The two-arg constructor sets the fields and prints first. Then control returns back printing one-arg and default constructor messages. Finally, the width and height are printed.