613). 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
Show Answer
Correct Answer: Deserialization
Notes:
Answer: d
Explanation: Deserialization is a process by which the data written in the stream can be extracted out from the stream.
614). Which of these process occur automatically by java run time system?
[A]Serialization
[B]Memory allocation
[C]Deserialization
[D]All of the mentioned
Show Answer
Correct Answer: All of the mentioned
Notes:
Answer: d
Explanation: Serialization, deserialization and Memory allocation occur automatically by java run time system.
615). Which of these interface extends DataInput interface?
[A]Serializable
[B]Externalization
[C]ObjectOutput
[D]ObjectInput
Show Answer
Correct Answer: ObjectInput
Notes:
Answer: d
Explanation: ObjectInput interface extends the DataInput interface and supports object serialization.
616). 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()
Show Answer
Correct Answer: Object readObject()
Notes:
Answer: c
Explanation: None.
617). Which of these class extend InputStream class?
[A]ObjectStream
[B]ObjectInputStream
[C]ObjectOutput
[D]ObjectInput
Show Answer
Correct Answer: ObjectInputStream
Notes:
Answer: b
Explanation: ObjectInputStream class extends the InputStream class and implements the ObjectInput interface.
618). What will be the output of the following Java code?
[A]5
[B]void
[C]serialization
[D]deserialization
Show Answer
Correct Answer: 5
Notes:
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
619). What will be the output of the following Java code?
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;
this.i = i;
this.s = s;
}
}
[A]-7
[B]Hello
[C]2.1E10
[D]deserialization
Show Answer
Correct Answer: deserialization
Notes:
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
620). What will be the output of the following Java program?
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);
}
}
}
Show Answer
Correct Answer: 0
Notes:
Answer: d
Explanation: New input stream is linked to steal ‘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
621). What will be the output of the following Java program?
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);
}
}
}
Show Answer
Correct Answer: 4
Notes:
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
622). What will be the output of the following Java program?
import java.io.FileOutputStream;
public class FileOutputStreamExample
{
public static void main(String args[])
{
try
{
FileOutputStream fout=new FileOutputStream("D:\\sanfoundry.txt");
String s="Welcome to Sanfoundry.";
byte b[]=s.getBytes();//converting string into byte array
fout.write(b);
fout.close();
System.out.println("Success");
} catch(Exception e){System.out.println(e);}
}
}
[A]“Success” to the output and “Welcome to Sanfoundry” to the file
[B]only “Welcome to Sanfoundry” to the file
[C]compile time error
[D]No Output
Show Answer
Correct Answer: “Success” to the output and “Welcome to Sanfoundry” to the file
Notes:
Answer: a
Explanation: First, it will print “Success” and besides that it will write “Welcome to Sanfoundry” to the file sanfoundry.txt.