Welcome Guest | Sign in | Register

Home > Java Programming > File > Questions and Answers

01. try{
File f = new File("a.txt");
}catch(Exception e){
}catch(IOException io){
}

Is this code create new file name a.txt ?
A. True B. False
C. Compilation Error D. None

Answer and Explanation

Answer: Compilation Error

Explanation:
IOException is unreachable to compiler because all exception is going to catch by
Exception block.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
02. What is the output for the below code?
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();
}
}
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
03. What is the output for the below code?
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();
}
}
A. A A B. A
C. java.io.NotSerializableException D. None of the above

Answer and Explanation

Answer: java.io.NotSerializableException

Explanation:
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
04. What will be the result of compiling and run the following code:
public class Test {
public static void main(String... args) throws Exception {
File file = new File("test.txt");
System.out.println(file.exists());
file.createNewFile();
System.out.println(file.exists());
}
}
A. true true B. false true
C. false true D. None of the above

Answer and Explanation

Answer: false true

Explanation:
creating a new instance of the class File, you're not yet making an actual file, you're just
creating a filename. So file.exists() return false. createNewFile() method created an actual
file.so file.exists() return true.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
05. What is the output for the below code ?
public class A {}
public class B implements Serializable {
private static 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();
}
}
}
A. Compilation Fail B. java.io.NotSerializableException: Because class A is not Serializable
C. No Exception at Runtime D.

None of the above

Answer and Explanation

Answer: No Exception at Runtime

Explanation:
No java.io.NotSerializableException, Because class A variable is static. static variables are not Serializable.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
06. You have a class file name Test.class inside javaproject directory.
Test.java source code is :
import java.util.Properties;
class Test {
public static void main (String[] args){
Properties p = System.getProperties();
System.out.println(p.getProperty("key1"));
}
}
you have issued below commands from command prompt.
cd javaproject
java -D key1=value1 Test
What is the output ?
A. value1 B. null
C. Run successfully but no output D. Run fails - java.lang.NoClassDefFoundError: key1=value1

Answer and Explanation

Answer: Run fails - java.lang.NoClassDefFoundError: key1=value1

Explanation:
-D option , name=value pair must follow immediately, no spaces allowed. In this case
there is space between -D and key1=value1 So java.lang.NoClassDefFoundError:
key1=value1.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
07. Is the below statement true?

"Only objects that support the java.io.Serializable or java.io.Externalizable interface can be read from streams"

(Choose correct one from multiple below)
A. true B. false
C. can't say D. none of the above

Answer and Explanation

Answer: true

Explanation:
Only objects that support the java.io.Serializable or java.io.Externalizable interface
can be read from streams

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
08. 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
09. 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:
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
10.
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();
}
}
A. A B. A 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



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