this Keyword

AP Computer Science A· difficulty 2/5

public class Pair {
    private int a, b;
    public Pair(int a, int b) {
        this.a = a;
        this.b = b;
    }
    public int sum() { return a + b; }
}
// ...
Pair p = new Pair(3, 4);
System.out.println(p.sum());

What is printed?

  • A

    7

    check_circle
  • B

    0

  • C

    12

  • D

    Compile-time error

Explanation

this.a and this.b are set to 3 and 4; sum returns 7.

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

Related questions