Challenge - 5 Problems
Method Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of a method with parameters
What is the output of the following Java program?
Java
public class Test { public static int multiply(int a, int b) { return a * b; } public static void main(String[] args) { System.out.println(multiply(3, 4)); } }
Attempts:
2 left
💡 Hint
Look at what the multiply method returns when given 3 and 4.
✗ Incorrect
The multiply method returns the product of a and b. 3 times 4 is 12, so the output is 12.
❓ Predict Output
intermediateReturn type and output
What will be printed when this Java code runs?
Java
public class Demo { public static String greet() { return "Hello!"; } public static void main(String[] args) { System.out.println(greet()); } }
Attempts:
2 left
💡 Hint
The greet method returns a string. What does it return exactly?
✗ Incorrect
The greet method returns the string "Hello!" which is printed by main.
❓ Predict Output
advancedMethod overloading output
What is the output of this Java program with overloaded methods?
Java
public class Overload { public static int add(int a, int b) { return a + b; } public static double add(double a, double b) { return a + b; } public static void main(String[] args) { System.out.println(add(2, 3)); System.out.println(add(2.0, 3.0)); } }
Attempts:
2 left
💡 Hint
Check which add method is called for int and double arguments.
✗ Incorrect
The first call uses int parameters and calls add(int, int), returning 5. The second call uses double parameters and calls add(double, double), returning 5.0.
❓ Predict Output
advancedVoid method and output
What will be printed when this Java program runs?
Java
public class VoidTest { public static void printMessage() { System.out.println("Hi there!"); } public static void main(String[] args) { printMessage(); System.out.println("Done"); } }
Attempts:
2 left
💡 Hint
Void methods do not return a value but can print inside.
✗ Incorrect
printMessage prints "Hi there!" then main prints "Done" on the next line.
❓ Predict Output
expertMethod recursion output
What is the output of this Java program using recursion?
Java
public class Recursion { public static int factorial(int n) { if (n <= 1) return 1; else return n * factorial(n - 1); } public static void main(String[] args) { System.out.println(factorial(4)); } }
Attempts:
2 left
💡 Hint
Factorial of 4 is 4 * 3 * 2 * 1.
✗ Incorrect
factorial(4) = 4 * factorial(3) = 4 * 3 * factorial(2) = 4 * 3 * 2 * factorial(1) = 4 * 3 * 2 * 1 = 24.
