Calling a Void Method with Parameters

AP Computer Science A· difficulty 3/5

public class Helper {
    public static void inc(int n) { n++; }
}
// ...
int x = 5;
Helper.inc(x);
System.out.println(x);

What is printed?

  • A

    5

    check_circle
  • B

    6

  • C

    0

  • D

    Compile-time error

Explanation

Java passes primitives by value; modifying the parameter n inside inc does not affect x.

Want 10 more like this — adaptive to your weak spots?

Related questions