Java OCA OCP Practice Question 2620

Question

Consider the following program:

import java.io.*;

class Point2D implements Externalizable {
        private int x, y;
        public Point2D(int x, int y) {
                x = x;/*w  w w  .  ja v a 2s  . c  o  m*/
        }
        public String toString() {
                return "[" + x + ", " + y + "]";
        }
        public void writeExternal(ObjectOutput out) throws IOException {
                System.out.println("Point " + x + ":" + y);
        }
        public void readExternal(ObjectInput in) throws IOException,
        ClassNotFoundException {
                /* empty */
        }
        public static void main(String []args) {
                Point2D point = new Point2D(10, 20);
                System.out.println(point);
        }
 }

When executed, this program prints the following:

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


d)

Note

In the constructor of Point2D, the statement x = x; reassigns the passed parameter and does not assign the member x in Point2D.

Field y is not assigned, so the value is 0.

Note that you implement the Externalizable interface to support serialization; this program uses the toString() method, which has nothing to do with object serialization/persistence.




PreviousNext

Related