Welcome Guest | Sign in | Register

Home > Java Programming > File > Questions and Answers

01. Can static variables are Serialized ?
A. yes B. No,static can't be Serialized.
C. may or may not Serialized D. None of the above

Answer and Explanation

Answer: No,static can't be Serialized.

Explanation:
No,static and transient can't be Serialized.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
02. Which statement is true?
A. Implementing the Serializable interface allows object serialization to save and restore the entire state of the object B. Implementing the Serializable interface allows object serialization to save state of the object and can't restore the entire state
C. Both are true D. None of the above

Answer and Explanation

Answer: Implementing the Serializable interface allows object serialization to save and restore the entire state of the object

Explanation:
Implementing the Serializable interface allows object serialization to save and
restore the entire state of the object

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
03. Which statement is true?
A. static and transient fields are NOT serialized B. static and transient fields are can be serialized
C. Both are true D. None of the above

Answer and Explanation

Answer: static and transient fields are NOT serialized

Explanation:
static and transient fields are NOT serialized

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
04. public class A {}
public class B implements Serializable {
A a = new A();
public static void main(String... args){
B b = new B();
try{
FileOutputStream fs = new FileOutputStream("b.ser");
ObjectOutputStream os = new ObjectOutputStream(fs);
os.writeObject(b);
os.close();
}catch(Exception e){
e.printStackTrace();
}
}
}

What is the output for the above code ?
A. Compilation Fail B. java.io.NotSerializableException: Because class A is not Serializable.
C. No Exception D. None of the above

Answer and Explanation

Answer: java.io.NotSerializableException: Because class A is not Serializable.

Explanation:
java.io.NotSerializableException:A Because class A is not Serializable.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
05. public class A {
public A() {
System.out.println("A");
}
}
public class B extends A implements Serializable {
public B() {
System.out.println("B");
}
}
public class Test {
public static void main(String... args) throws Exception {
B b = new B();
ObjectOutputStream save = new ObjectOutputStream(new FileOutputStream("datafile"));
save.writeObject(b);
save.flush();
ObjectInputStream restore = new ObjectInputStream(new FileInputStream("datafile"));
B z = (B) restore.readObject();
}
}
What is the output?
A. A B A B. A B A B
C.  B B D. B

Answer and Explanation

Answer: A B A

Explanation:
On the time of deserialization , the Serializable object not create new object. So
constructor of class B does not called.
A is not Serializable object so constructor is called.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
06.
public class A {
public A() {
System.out.println("A");
}
}
public class Test {
public static void main(String... args) throws Exception {
A a = new A();
ObjectOutputStream save = new ObjectOutputStream(new FileOutputStream("datafile"));
save.writeObject(a);
save.flush();
ObjectInputStream restore = new ObjectInputStream(new FileInputStream("datafile"));
A z = (A) restore.readObject();
}
}

What is the output?
A. A A B. A
C. java.io.NotSerializableException D. None of the above

Answer and Explanation

Answer: java.io.NotSerializableException

Explanation:
Explanations :Class A does not implements Serializable interface. So throws
NotSerializableException on trying to Serialize a non Serializable object.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
07. public class A implements Serializable{
public A() {
System.out.println("A");
}
}
public class Test {
public static void main(String... args) throws Exception {
A a = new A();
ObjectOutputStream save = new ObjectOutputStream(new FileOutputStream("datafile"));
save.writeObject(a);
save.flush();
ObjectInputStream restore = new ObjectInputStream(new FileInputStream("datafile"));
A z = (A) restore.readObject();
}
}

What is the output?
A. A A B. A
C. Runtime Exception D. Compile with error

Answer and Explanation

Answer: A

Explanation:
On the time of deserialization , the Serializable object not create new object. So
constructor of class A does not called.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
08.
public class A implements Serializable {
transient int a = 7;
static int b = 9;
}
public class B implements Serializable {
public static void main(String... args){
A a = new A();
try {
ObjectOutputStream os = new ObjectOutputStream(
new FileOutputStream("test.ser"));
os.writeObject(a);
os. close();
System.out.print( + + a.b + " ");
ObjectInputStream is = new ObjectInputStream(new FileInputStream("test.ser"));
A s2 = (A)is.readObject();
is.close();
System.out.println(s2.a + " " + s2.b);
} catch (Exception x){
x.printStackTrace();
}
}
}
What is the output?
A. 9 0 9 B. 9 7 9
C. Runtime Exception D. Compile with error

Answer and Explanation

Answer: 9 0 9

Explanation:
static and transient variables are not serialized when an object is serialized.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
09. Which is the correct way of Instantiate BufferedWriter object?
A. BufferedWriter b1 = new BufferedWriter(new File("file.txt")); B. BufferedWriter b1 = new BufferedWriter(new FileWriter("file.txt"));
C. Both are true D. None of the above

Answer and Explanation

Answer: BufferedWriter b1 = new BufferedWriter(new FileWriter("file.txt"));

Explanation:
Constructor of BufferedWriter is public BufferedWriter(Writer out) {
}
so BufferedWriter b1 = new BufferedWriter(new FileWriter("file.txt")); is correct one.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
10. What will be the result of compiling and run the following code:

public class Test {
public static void main(String... args) throws Exception {
Integer i = 34;
long l = 34l;
if(i.equals(l)){
System.out.println(true);
}else{
System.out.println(false);
}
}
}
A. true B. false
C. Compile error D. None of the above

Answer and Explanation

Answer: false

Explanation:
equals() method for the integer wrappers will only return true if the two primitive
types and the two values are equal.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum



Partner Sites
LucentBlackBoard.com                  SoftLucent.com                  LucentJobs.com
All rights reserved © 2012-2015 SoftLucent.