SCJP : File Mock Exam Practice Questions
Questions no -1
What is the output for the below 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());
}
}
options
A)true true
B)false false
C)false true
D)true false
Correct answer is : C
Explanations :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.
Questions no -2
When comparing java.io.BufferedWriter and java.io.FileWriter, which capability exist as a method in only one of two ?
options
A)closing the stream
B)flushing the stream
C)writting to the stream
D)writting a line separator to the stream
Correct answer is : D
Explanations :A newLine() method is provided in BufferedWriter which is not in FileWriter.
|