Serialization & Deserialization

advertisement
Serialization & Deserialization
This section of our 1000+ Java MCQs focuses on serialization & deserialization of Java
Programming Language.
1. Which of these is a process of extracting/removing the state of an object from a stream?
a) Serialization
b) Externalization
c) File Filtering
d) Deserialization
View Answer
2. Which of these process occur automatically by java run time system?
a) Serialization
b) Memory allocation
c) Deserialization
d) All of the mentioned
View Answer
Answer: d
Explanation: Serialization, deserialization and Memory allocation occur automatically by java
run time system.
3. Which of these is an interface for control over serialization and deserialization?
a) Serializable
b) Externalization
c) FileFilter
d) ObjectInput
View Answer
Answer: b
Explanation: None.
4. Which of these interface extends DataInput interface?
a) Serializable
b) Externalization
c) ObjectOutput
d) ObjectInput
View Answer
Answer: d
Explanation: ObjectInput interface extends the DataInput interface and supports object
serialization.
5. Which of these is a method of ObjectInput interface used to deserialize an object from a
stream?
a) int read()
b) void close()
c) Object readObject()
d) Object WriteObject()
View Answer
Answer: c
Explanation: None.
6. Which of these class extend InputStream class?
a) ObjectStream
b) ObjectInputStream
c) ObjectOutput
d) ObjectInput
View Answer
Answer: b
Explanation: ObjectInputStream class extends the InputStream class and implements the
ObjectInput interface.
7. What is the output of this program?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
import java.io.*;
class streams {
public static void main(String[] args) {
try {
FileOutputStream fos = new FileOutputStream("serial");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeInt(5);
oos.flush();
oos.close();
}
catch(Exception e) {
System.out.println("Serialization" + e);
System.exit(0);
}
try {
int z;
FileInputStream fis = new FileInputStream("serial");
ObjectInputStream ois = new ObjectInputStream(fis);
z = ois.readInt();
ois.close();
System.out.println(x);
}
catch (Exception e) {
System.out.print("deserialization");
System.exit(0);
}
}
28.
}
a) 5
b) void
c) serialization
d) deserialization
View Answer
Answer: a
Explanation: oos.writeInt(5); writes integer 5 in the Output stream which is extracted by z =
ois.readInt(); and stored in z hence z contains 5.
Output:
$ javac streams.java
$ java streams
5
8. What is the output of this program?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
27.
28.
29.
30.
31.
32.
33.
34.
35.
import java.io.*;
class serialization {
public static void main(String[] args) {
try {
Myclass object1 = new Myclass("Hello", -7, 2.1e10);
FileOutputStream fos = new FileOutputStream("serial");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(object1);
oos.flush();
oos.close();
}
catch(Exception e) {
System.out.println("Serialization" + e);
System.exit(0);
}
try {
int x;
FileInputStream fis = new FileInputStream("serial");
ObjectInputStream ois = new ObjectInputStream(fis);
x = ois.readInt();
ois.close();
System.out.println(x);
}
catch (Exception e) {
System.out.print("deserialization");
System.exit(0);
}
}
}
class Myclass implements Serializable {
String s;
int i;
double d;
Myclass(String s, int i, double d){
this.d = d;
36.
37.
38.
39.
this.i = i;
this.s = s;
}
}
a) -7
b) Hello
c) 2.1E10
d) deserialization
View Answer
Answer: d
Explanation: x = ois.readInt(); will try to read an integer value from the stream ‘serial’ created
before, since stream contains an object of Myclass hence error will occur and it will be catched
by catch printing deserialization.
Output:
$ javac serialization.java
$ java serialization
deserialization
9. What is the output of this program?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
26.
a) 1
b) 2
c) 3
import java.io.*;
class streams {
public static void main(String[] args) {
try {
FileOutputStream fos = new FileOutputStream("serial");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeFloat(3.5);
oos.flush();
oos.close();
}
catch(Exception e) {
System.out.println("Serialization" + e);
System.exit(0);
}
try {
FileInputStream fis = new FileInputStream("serial");
ObjectInputStream ois = new ObjectInputStream(fis);
ois.close();
System.out.println(ois.available());
}
catch (Exception e) {
System.out.print("deserialization");
System.exit(0);
}
}
}
d) 4
View Answer
Answer: d
Explanation: New input stream is linked to streal ‘serials’, an object ‘ois’ of ObjectInputStream
is used to access this newly created stream, ois.close(); closes the stream hence we can’t access
the stream and ois.available() returns 0.
Output:
$ javac streams.java
$ java streams
0
10. What is the output of this program?
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.
20.
21.
22.
23.
24.
25.
import java.io.*;
class streams {
public static void main(String[] args) {
try {
FileOutputStream fos = new FileOutputStream("serial");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeFloat(3.5);
oos.flush();
oos.close();
}
catch(Exception e) {
System.out.println("Serialization" + e);
System.exit(0);
}
try {
FileInputStream fis = new FileInputStream("serial");
ObjectInputStream ois = new ObjectInputStream(fis);
System.out.println(ois.available());
}
catch (Exception e) {
System.out.print("deserialization");
System.exit(0);
}
}
}
a) 1
b) 2
c) 3
d) 4
View Answer
Answer: d
Explanation: oos.writeFloat(3.5); writes 3.5 in output stream. A new input stream is linked to
stream ‘serials’, an object ‘ois’ of ObjectInputStream is used to access this newly created stream,
ois.available() gives the total number of byte in the input stream since a float was written in the
stream thus the stream contains 4 byte, hence 4 is returned and printed.
Output:
$ javac streams.java
$ java streams
4
Download