Scope and Access

AP Computer Science A· difficulty 3/5

public class C {
    static int x = 5;
    public static int change() {
        int x = 10;
        x = x + 1;
        return x;
    }
    public static int run() {
        int a = change();
        return a + x;
    }
}
// Call: System.out.println(C.run());

What is printed?

  • A

    16

    check_circle
  • B

    21

  • C

    11

  • D

    22

Explanation

Local x in change() shadows static. change() returns 11. run() returns 11 + 5 (static x unchanged) = 16.

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

Related questions