Variables and Data Types

AP Computer Science A· difficulty 4/5

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

What is printed and why?

  • A

    Compile error

  • B

    6; n is incremented in place

  • C

    0; n is reset

  • D

    5; primitives are passed by value, so the caller's n is unchanged

    check_circle

Explanation

Java passes primitives by value. inc receives a copy; modifying x does not affect n in the caller.

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

Related questions