Challenge - 5 Problems
Master of Method Parameters
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of method with primitive parameter modification
What is the output of the following Java code?
Java
public class Test { public static void modify(int x) { x = x + 10; } public static void main(String[] args) { int a = 5; modify(a); System.out.println(a); } }
Attempts:
2 left
💡 Hint
Think about how Java passes primitive types to methods.
✗ Incorrect
In Java, primitive types are passed by value, so changes inside the method do not affect the original variable.
❓ Predict Output
intermediateOutput of method with object parameter modification
What is the output of this Java program?
Java
class Box { int value; Box(int v) { value = v; } } public class Test { public static void modify(Box b) { b.value = 10; } public static void main(String[] args) { Box box = new Box(5); modify(box); System.out.println(box.value); } }
Attempts:
2 left
💡 Hint
Consider how Java passes object references to methods.
✗ Incorrect
Java passes object references by value, so the method can modify the object's fields.
❓ Predict Output
advancedOutput of method with varargs parameter
What will be printed when this Java code runs?
Java
public class Test { public static int sum(int... numbers) { int total = 0; for (int n : numbers) { total += n; } return total; } public static void main(String[] args) { System.out.println(sum(1, 2, 3)); } }
Attempts:
2 left
💡 Hint
Varargs allow passing multiple arguments as an array.
✗ Incorrect
The method sums all the integers passed and returns the total.
❓ Predict Output
advancedOutput of method with overloaded parameters
What is the output of this 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(null); } }
Attempts:
2 left
💡 Hint
Consider how Java resolves overloaded methods with null argument.
✗ Incorrect
print(null) unambiguously calls print(String) because null can be assigned to a String reference but not to a primitive int.
❓ Predict Output
expertOutput of method with final parameter and reassignment attempt
What happens when this Java code runs?
Java
public class Test { public static void modify(final int x) { // x = x + 1; // Uncommenting this line causes what? System.out.println(x); } public static void main(String[] args) { modify(5); } }
Attempts:
2 left
💡 Hint
Final parameters cannot be reassigned inside the method.
✗ Incorrect
The code as is prints 5. Uncommenting the reassignment line causes a compilation error.
