Question 95
Given: 
10. class MakeFile { 
11. public static void main(String[] args) { 
12. try { 
13. File directory = new File(“d”)
14. File file = new File(directory,”f”)
15. if(!file.exists()) { 
System.out.println("file.exists() is true");//自添的测试代码,题目没有
16. file.createNewFile(); 
17. } 
18. } catch (IOException e) { 
System.out.println("file.exists() is false");//自添的测试代码,题目没有
19. e.printStackTrace 
20. } 
21. } 
22. } 
The current directory does NOT contain a directory named “d.”
Which three are true? (Choose three.) A. Line 16 is never executed. 
B. An exception is thrown at runtime. 
C. Line 13 creates a File object named “d.!
D. Line 14 creates a File object named “f.!
E. Line 13 creates a directory named “d” in the file system
F. Line 16 creates a directory named “d” and a file “f” within it in 
file system. 
G. Line 14 creates a file named “f” inside of the directory named “d”
the file system. 
Answer: BCD
我在eclipse里测试了一下,怎么输出是:
file.exists() is true
file.exists() is false
java.io.IOException: 系统找不到指定的路径。
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createNewFile(Unknown Source)
at t.main(t.java:9)
问题一:有true又有false,如何是这样???
问题二:谁能清楚地解释一下各个选项的对错啊?谢谢请路过的神仙哥哥姐姐赐教,小弟感激流涕

解决方案 »

  1.   

    不存在该文件的时候打印file.exists() is true ;(你加这句话有点??)
    试图创建时又出错(因为d不存在,所以不能创建d/f)就打印了file.exists() is false 
      

  2.   

    应该是指定的文件不存在吧。
    还有 e.printStackTrace,后面少个() 了。
      

  3.   

    LZ是不是想要考SCJP啊
    最近这些题提的很频繁哦!
      

  4.   

    directory 是什么啊,目录啊!所以C不行!D行E对
    有true是因为在 file.createNewFile(); 
    之前没发生异常,所以打印了file.exists() is true!
    有false是因为file.createNewFile(); 
    发生了异常,所以走到了catch块所以就打印了
    file.exists() is false!你因该把
    System.out.println("file.exists() is true");//
    放在file.createNewFile(); 下面啊!
    答案不应该是BCD啊,我觉得是BDE!
      

  5.   

    file.createNewFile();
    File中没有上面那个方法,在Try中运行到这儿发生异常,所以会发生上面的结果。