Welcome Guest | Sign in | Register
File - Discussion Page For Q.2724 | Java Programming Questions and Answers | Java Programming Free Online-Tests | LucentBlackBoard | LucentBlackBoard.com

Home > Java Programming > File > Questions and Answers

Q: 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 .

Comment on this Question

Name:

Loading...




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