Scope and Access

AP Computer Science A· difficulty 4/5

public class S {
    int v = 10;
    public int update(int v) {
        v = v + 5;
        this.v = this.v + v;
        return v;
    }
}
public static int run() {
    S s = new S();
    int r = s.update(3);
    return r + s.v;
}
// Call: System.out.println(run());

What is printed?

  • A

    26

    check_circle
  • B

    21

  • C

    18

  • D

    23

Explanation

Local v=3, then v+5=8. this.v = 10 + 8 = 18. Returns 8. r + s.v = 8 + 18 = 26.

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

Related questions