Bird
Raised Fist0
Javaprogramming~20 mins

Method overloading in Java - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Method Overloading Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict 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
💡 Hint
Look at the parameter types used in each method call.
❓ Predict 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
💡 Hint
Check which method matches each call.
❓ Predict 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
💡 Hint
Consider how Java chooses between widening and boxing conversions.
❓ Predict 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
💡 Hint
Exact matches are preferred over varargs.
❓ Predict 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
💡 Hint
When null is passed, Java chooses the most specific method.

Practice

(1/5)
1.

What is method overloading in Java?

easy
A. Changing the return type of a method only
B. Having multiple classes with the same name
C. Using inheritance to override methods
D. Having multiple methods with the same name but different parameters

Solution

  1. Step 1: Understand method naming

    Method overloading means methods share the same name.
  2. Step 2: Check parameter differences

    They differ by number or type of parameters, not just return type.
  3. Final Answer:

    Having multiple methods with the same name but different parameters -> Option D
  4. Quick Check:

    Same name + different parameters = method overloading [OK]
Hint: Same method name, different parameters means overloading [OK]
Common Mistakes:
  • Confusing overloading with overriding
  • Thinking return type alone overloads method
  • Mixing class names with method names
2.

Which of the following is a correct method overloading example in Java?

class Test {
  void show(int a) {}
  void show(int a, int b) {}
  void show(int a) {}
}
easy
A. This code compiles without errors
B. Duplicate method show(int a) causes a compile error
C. Method names must be different to overload
D. Return types must differ for overloading

Solution

  1. Step 1: Check method signatures

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

    Overloading requires different parameter lists; duplicate signatures cause errors.
  3. Final Answer:

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

    Same parameters = error, not overloading [OK]
Hint: Parameters must differ; identical signatures cause errors [OK]
Common Mistakes:
  • Assuming return type difference overloads method
  • Ignoring duplicate parameter lists
  • Thinking method names must differ
3.

What is the output of this Java code?

class Demo {
  void print(int a) {
    System.out.println("int: " + a);
  }
  void print(String s) {
    System.out.println("String: " + s);
  }
}
public class Main {
  public static void main(String[] args) {
    Demo d = new Demo();
    d.print(5);
    d.print("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 method calls and parameters

    First call passes int 5, second call passes String "hello".
  2. Step 2: Match calls to overloaded methods

    print(int) prints "int: 5", print(String) prints "String: hello".
  3. Final Answer:

    int: 5 String: hello -> Option A
  4. Quick Check:

    Correct method chosen by parameter type [OK]
Hint: Method chosen by argument type matches parameter [OK]
Common Mistakes:
  • Mixing output order
  • Assuming wrong method called
  • Expecting compile error
4.

Find the error in this code snippet:

class Calculator {
  int add(int a, int b) { return a + b; }
  double add(double a, double b) { return a + b; }
  int add(int a, int b, int c) { return a + b + c; }
  int add(int a, int b) { return a - b; }
}
medium
A. Duplicate method add(int a, int b) with different body causes error
B. Overloading requires different method names
C. Return type difference causes error
D. No error, code compiles fine

Solution

  1. Step 1: Check method signatures

    Two methods have signature add(int a, int b) but different bodies.
  2. Step 2: Understand overloading rules

    Overloading requires unique parameter lists; duplicate signatures cause errors regardless of body.
  3. Final Answer:

    Duplicate method add(int a, int b) with different body causes error -> Option A
  4. Quick Check:

    Same parameters = duplicate method error [OK]
Hint: Same parameters, different body = error, not overloading [OK]
Common Mistakes:
  • Thinking different return types fix duplicates
  • Ignoring duplicate parameter lists
  • Confusing overloading with overriding
5.

Consider this class:

class Converter {
  int convert(String s) { return Integer.parseInt(s); }
  int convert(int i) { return i * 2; }
  int convert(String s, int base) { return Integer.parseInt(s, base); }
}
public class Main {
  public static void main(String[] args) {
    Converter c = new Converter();
    System.out.println(c.convert("10"));
    System.out.println(c.convert(10));
    System.out.println(c.convert("10", 16));
  }
}

What is the output when this code runs?

hard
A. Runtime error parsing string
B. 10\n20\n10
C. 10\n20\n16
D. Compilation error due to ambiguous methods

Solution

  1. Step 1: Analyze each method call

    convert("10") parses string "10" as decimal 10; convert(10) doubles 10 to 20; convert("10", 16) parses hex "10" as decimal 16.
  2. Step 2: Determine output values

    Outputs are 10, 20, and 16 respectively.
  3. Final Answer:

    10 20 16 -> Option C
  4. Quick Check:

    Overloaded methods handle inputs correctly [OK]
Hint: Match method by parameters; parseInt with base converts hex [OK]
Common Mistakes:
  • Confusing hex parsing result
  • Expecting compile or runtime errors
  • Mixing method outputs
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard