Question

In: Computer Science

1. Consider the following code: public class Widget implements Serializable { private int x; public void...

1. Consider the following code:

public class Widget implements Serializable
{
private int x;
public void setX( int d ) { x = d; }
public int getX() { return x; }
writeObject( Object o ) { o.writeInt(x); }
}

Which of the following statements is true?

I. The Widget class is not serializable because no constructor is defined.

II. The Widget class is not serializable because the implementation of writeObject() is not needed.

III. The code will not compile because the implementation of writeObject is incorrect.

Solutions

Expert Solution

Answer- III

Explanation-

I. The Widget class is not serializable because no constructor is defined:

Yes it is true that in order for a class to be serializable, the class requires a no argument constructor, but since the class Widget is not extending any parent class, which means that it isn't a child class. But in this case, it is not necessary because, since the class "widget" 's super class, which is "Object" class has a no-argument constructor. Hence it suffices all the needs and therefore this is a Wrong Option. (False)

II. The Widget class is not serializable because the implementation of writeObject() is not needed:

First of all, the writeObject() method is used for writing the byte stream, created by the process of Serialization in a physical location. Yes it is not needed, since we are not using to retrieve the data (bytestream) from any physical location. But it doesn't mean that the class will not become serializable due to this. Hence the class is Serializable. Therefore this is a Wrong Option. (False)

III. The code will not compile because the implementation of writeObject is incorrect:

Yes the code will not compile because the implementation of the writeObject() method is incorrect. There is no return type provided before the method "writeObject()" , generally this should be "void". Therefore this comes under invalid method declaration and hence the code cannot be compiled due to this reason. (True)

  


Related Solutions

#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int,...
#include<iostream> using namespace std; class point{ private: int x; int y; public: void print()const; void setf(int, int); }; class line{ private: point ps; point pe; public: void print()const; void setf(int, int, int, int); }; class rectangle{ private: line length[2]; line breadth[2]; public: void print()const; void setf(int, int, int, int, int, int, int, int); }; int main(){ rectangle r1; r1.setf(3,4,5,6, 7, 8, 9, 10); r1.print(); system("pause"); return 0; } a. Write function implementation of rectangle, line and point. b. What is...
Consider the following code: public class Bay extends Lake { public void method1() { System.out.println("Bay 1");...
Consider the following code: public class Bay extends Lake { public void method1() { System.out.println("Bay 1"); super.method2(); } public void method2() { System.out.println("Bay 2"); } } //********************************* class Pond { public void method2() { System.out.println("Pond 2"); } } //************************** class Ocean extends Bay { public void method2() { System.out.println("Ocean 2"); } } //********************************* class Lake extends Pond { public void method3() { System.out.println("Lake 3"); method2(); } } //**************************** class Driver { public static void main(String[] args) { Object var4 =...
With the code that is being tested is: import java.util.Random; public class GVdate { private int...
With the code that is being tested is: import java.util.Random; public class GVdate { private int month; private int day; private int year; private final int MONTH = 1; private final int DAY = 9; private static Random rand = new Random(); /** * Constructor for objects of class GVDate */ public GVdate() { this.month = rand.nextInt ( MONTH) + 1; this.day = rand.nextInt ( DAY );    } public int getMonth() {return this.month; } public int getDay() {return this.day;...
public class Problem1 {    public static void partition(int[] A)    {        /*Rearrange the...
public class Problem1 {    public static void partition(int[] A)    {        /*Rearrange the array to have the following property:        Suppose the first element in the original array has the value x.        In the new array, suppose that x is in position i, that is data[i] = x.        Then, data[j] <= x for all j < I and data[j] > x for all j > i.        Thus, informally, all the...
1. Convert the following code shown below to C++ code: public class HighwayBillboard { public int...
1. Convert the following code shown below to C++ code: public class HighwayBillboard { public int maxRevenue(int[] billboard, int[] revenue, int distance, int milesRes) { int[] MR = new int[distance + 1]; //Next billboard which can be used will start from index 0 in billboard[] int nextBillBoard = 0; //example if milesRes = 5 miles then any 2 bill boards has to be more than //5 miles away so actually we can put at 6th mile so we can add...
public class SinglyLikedList {    private class Node{        public int item;        public...
public class SinglyLikedList {    private class Node{        public int item;        public Node next;        public Node(int item, Node next) {            this.item = item;            this.next = next;        }    }       private Node first;    public void addFirst(int a) {        first = new Node(a, first);    } } 1. Write the method add(int item, int position), which takes an item and a position, and...
Consider the following code: public class Example { public static void doOp(Op op) { boolean result...
Consider the following code: public class Example { public static void doOp(Op op) { boolean result = op.operation(true, false); System.out.println(result); } public static void main(String[] args) { doOp(new AndOperation()); doOp(new OrOperation()); } } main's output: false true Define any interfaces and/or classes necessary to make this output happen. Multiple answers are possible. You may not modify any of the code in Example.
import java.util.NoSuchElementException; public class ArrayBasedDeque<AnyType> implements deque<AnyType> {       private static int MAX_SIZE = 5;...
import java.util.NoSuchElementException; public class ArrayBasedDeque<AnyType> implements deque<AnyType> {       private static int MAX_SIZE = 5; // initial array size    //DO NOT CHANGE this, it is set to 5 to make sure your code    //will pass all the tests and works with no issue.       // add all data fields which are required    /**    * ArrayBasedDeque() constructs an empty deque.    */    public ArrayBasedDeque(){        //complete    }       /**    *...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y;...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y; public MyPoint() { this(0, 0); } public MyPoint(double x, double y) { this.x = x; this.y = y; } // Returns the distance between this MyPoint and other public double distance(MyPoint other) { return Math.sqrt(Math.pow(other.x - x, 2) + Math.pow(other.y - y, 2)); } // Determines if this MyPoint is equivalent to another MyPoint public boolean equals(MyPoint other) { return this.x == other.x &&...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y;...
Consider the following class: import java.util.Scanner; public class MyPoint { private double x; private double y; public MyPoint() { this(0, 0); } public MyPoint(double x, double y) { this.x = x; this.y = y; } // Returns the distance between this MyPoint and other public double distance(MyPoint other) { return Math.sqrt(Math.pow(other.x - x, 2) + Math.pow(other.y - y, 2)); } // Determines if this MyPoint is equivalent to another MyPoint public boolean equals(MyPoint other) { return this.x == other.x &&...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT