Challenge - 5 Problems
Master of Method Calling
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of method call with recursion
What is the output of this Java program when the
printNumbers method is called with argument 3?Java
public class Test { public static void printNumbers(int n) { if (n == 0) return; System.out.print(n + " "); printNumbers(n - 1); } public static void main(String[] args) { printNumbers(3); } }
Attempts:
2 left
💡 Hint
Think about how the method prints before the recursive call and when it stops.
✗ Incorrect
The method prints the current number before calling itself with n-1. It stops when n is 0, so it prints 3, then 2, then 1, each followed by a space.
❓ Predict Output
intermediateOutput of method call with overloaded methods
What is the output of this Java program?
Java
public class OverloadTest { public static void display(int a) { System.out.println("int: " + a); } public static void display(String a) { System.out.println("String: " + a); } public static void main(String[] args) { display(5); display("hello"); } }
Attempts:
2 left
💡 Hint
Look at the method signatures and which one matches the argument type.
✗ Incorrect
The method display(int) is called with 5, so it prints "int: 5". The method display(String) is called with "hello", so it prints "String: hello".
🔧 Debug
advancedIdentify the error in method call
What error does this Java code produce when compiled?
Java
public class ErrorTest { public static void greet() { System.out.println("Hello"); } public static void main(String[] args) { greet("World"); } }
Attempts:
2 left
💡 Hint
Check the method signature and how it is called.
✗ Incorrect
The method greet() takes no arguments, but it is called with one String argument "World". This causes a compilation error.
❓ Predict Output
advancedOutput of method call with static and instance methods
What is the output of this Java program?
Java
public class StaticInstance { public static void staticMethod() { System.out.println("Static method called"); } public void instanceMethod() { System.out.println("Instance method called"); } public static void main(String[] args) { StaticInstance.staticMethod(); StaticInstance obj = new StaticInstance(); obj.instanceMethod(); } }
Attempts:
2 left
💡 Hint
Static methods are called on the class, instance methods on objects.
✗ Incorrect
The static method is called first and prints "Static method called". Then the instance method is called on the object and prints "Instance method called".
❓ Predict Output
expertOutput of method call with method overriding and polymorphism
What is the output of this Java program?
Java
class Parent { void show() { System.out.println("Parent show"); } } class Child extends Parent { @Override void show() { System.out.println("Child show"); } } public class TestPolymorphism { public static void main(String[] args) { Parent obj = new Child(); obj.show(); } }
Attempts:
2 left
💡 Hint
Look at which method is called when a Parent reference points to a Child object.
✗ Incorrect
The method show() is overridden in Child. Even though the reference is Parent, the actual object is Child, so Child's show() is called.
