0
0
Javaprogramming~15 mins

Method parameters in Java - Practice Problems & Coding Challenges

Choose your learning style8 modes available
trophyChallenge - 5 Problems
🎖️
Master of Method Parameters
Get all challenges correct to earn this badge!
Test your skills under time pressure!
💻 code output
intermediate
2:00remaining
Output 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);
    }
}
A5
B15
CCompilation error
DRuntime error
Attempts:
2 left
💻 code output
intermediate
2:00remaining
Output 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);
    }
}
A10
BRuntime error
CCompilation error
D5
Attempts:
2 left
💻 code output
advanced
2:00remaining
Output 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));
    }
}
ARuntime error
B123
CCompilation error
D6
Attempts:
2 left
💻 code output
advanced
2:00remaining
Output 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);
    }
}
ACompilation error
Bint: 5\nString: null
Cint: 5\nRuntime error
Dint: 5\nCompilation error
Attempts:
2 left
💻 code output
expert
2:00remaining
Output 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);
    }
}
ARuntime error
BCompilation error due to reassignment of final parameter
CPrints 5
DPrints 6
Attempts:
2 left