Java OCA OCP Practice Question 2760

Question

Consider the following program:

public class Point2D {
        private int x, y;
        public Point2D(int x, int y) {
                x = x;/*from   w  w w. j  a v  a2 s. c o  m*/
        }

        public String toString() {
                return "[" + x + ", " + y + "]";
        }
        public static void main(String []args) {
                 Point2D point = new Point2D(10, 20);
                 System.out.println(point);
        }
}

Which one of the following options provides the output of this program when executed?

  • a) point
  • b) Point
  • c) [0, 0]
  • d) [10, 0]
  • e) [10, 20]


c)

Note

The assignment x = x; inside the construct reassigns the passed parameter; it does not assign the member x in Point2D.

The correct way to perform the assignment is this.x = x;.

Field y is not assigned, so its value remains 0.




PreviousNext

Related