Welcome Guest | Sign in | Register

Home > Java Programming > File > Questions and Answers

01. When comparing java.io.BufferedWriter and java.io.FileWriter, which capability exist as a method in only one of two ?
A. closing the stream B. flushing the stream
C. writting to the stream D. writting a line separator to the stream

Answer and Explanation

Answer: writting a line separator to the stream

Explanation:
A newLine() method is provided in BufferedWriter which is not in FileWriter.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
02. 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");
}
}
A. create new actual file name as test.txt B. no actual file will be created.
C. Compile error. D. None of the above

Answer and Explanation

Answer: no actual file will be created.

Explanation:
creating a new instance of the class File, you're not yet making an actual file, you're
just creating a filename

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
03. 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
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());
FileWriter fw = new FileWriter(file);
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.
FileWriter fw = new FileWriter(file) do three things:
It created a FileWriter reference variable fw.
It created a FileWriter object, and assigned it to fw.
It created an actual empty file out on the disk.
So file.exists() return true .

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
05. What is the way to create a actual file ?
A. Invoke the createNewFile() method on a File object B. Create a Reader or a Writer or a Stream
C. Both are true D. None of the above

Answer and Explanation

Answer: Both are true

Explanation:
Whenever you create an instance of a FileReader, a FileWriter, a PrintWriter, a
FileInputStream, or a FileOutputStream
classes, you automatically create a file, unless one already exists.

Report Errors

Name:

Loading...

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

public class Test {
public static void main(String... args) throws Exception {
File myDir = new File("test");
// myDir.mkdir();
File myFile = new File(
myDir, "test.txt");
myFile.createNewFile();
}
}
A. create directory "test" and a file name as "test.txt" within the the directory. B. java.io.IOException: No such file or directory
C. Compile with error D. None of the above

Answer and Explanation

Answer: java.io.IOException: No such file or directory

Explanation:
// myDir.mkdir(); is commented so no directory, that’s why exception.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
07. public class A {}
public class B implements Serializable {
private transient 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: No Exception

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

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum
08. 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();
}
}
}
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: No Exception

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
09. Which statement is true ?
A. serialization applies only to OBJECTS. B. Static variables are NEVER saved as part of the object's state. So static variables are not Serializable.
C. Both are true D. None of the above

Answer and Explanation

Answer: Both are true

Explanation:
serialization applies only to OBJECTS. static variables are related to class not
OBJECT. so static variables are not Serializable.

Report Errors

Name:

Loading...

VView Answer | RReport | DDiscuss in Forum



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