0
0
Javaprogramming~15 mins

Method overloading in Java - Practice Problems & Coding Challenges

Choose your learning style8 modes available
trophyChallenge - 5 Problems
🎖️
Method Overloading Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 code output
intermediate
2:00remaining
Output of overloaded methods with different parameter types
What is the output of the following Java program?
Java
public class Test {
    public static void print(int x) {
        System.out.println("int: " + x);
    }
    public static void print(String x) {
        System.out.println("String: " + x);
    }
    public static void main(String[] args) {
        print(5);
        print("5");
    }
}
A
int: 5
String: 5
B
String: 5
int: 5
C
int: 5
int: 5
D
String: 5
String: 5
Attempts:
2 left
💻 code output
intermediate
2:00remaining
Overloading with different number of parameters
What will be printed when the following Java code runs?
Java
public class Demo {
    static void show() {
        System.out.println("No parameters");
    }
    static void show(int a) {
        System.out.println("One parameter: " + a);
    }
    public static void main(String[] args) {
        show();
        show(10);
    }
}
A
No parameters
One parameter: 10
B
One parameter: 10
No parameters
C
No parameters
No parameters
D
One parameter: 10
One parameter: 10
Attempts:
2 left
💻 code output
advanced
2:00remaining
Overloading with widening and boxing
What is the output of this Java program?
Java
public class OverloadTest {
    static void test(long x) {
        System.out.println("long: " + x);
    }
    static void test(Integer x) {
        System.out.println("Integer: " + x);
    }
    public static void main(String[] args) {
        test(5);
    }
}
ARuntime error
BInteger: 5
CCompilation error
Dlong: 5
Attempts:
2 left
💻 code output
advanced
2:00remaining
Overloading with varargs and exact match
What will be printed when this Java code runs?
Java
public class VarargsTest {
    static void display(int a, int b) {
        System.out.println("Two ints: " + a + ", " + b);
    }
    static void display(int... nums) {
        System.out.println("Varargs: " + nums.length);
    }
    public static void main(String[] args) {
        display(1, 2);
    }
}
ACompilation error
BVarargs: 2
CTwo ints: 1, 2
DRuntime error
Attempts:
2 left
💻 code output
expert
2:00remaining
Overloading with inheritance and null argument
What is the output of this Java program?
Java
public class InheritanceOverload {
    static void call(Object o) {
        System.out.println("Object version");
    }
    static void call(String s) {
        System.out.println("String version");
    }
    public static void main(String[] args) {
        call(null);
    }
}
AObject version
BString version
CRuntime error
DCompilation error
Attempts:
2 left