Constructors

AP Computer Science A· difficulty 2/5

public class Point {
    private int x, y;
    public Point(int x, int y) {
        this.x = x;
        this.y = y;
    }
    public int getX() { return x; }
    public int getY() { return y; }
}
// ...
Point p = new Point(4, 7);
System.out.println(p.getX() + "," + p.getY());

What is printed?

  • A

    Compile-time error

  • B

    7,4

  • C

    0,0

  • D

    4,7

    check_circle

Explanation

The constructor uses this.x = x and this.y = y to assign instance variables, so getX returns 4 and getY returns 7.

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

Related questions