Hi folks ! A question from JExam Select the correct statement regarding the following piece of code. File f = new File("C:\\Large.txt") a) On execution a file called "Large.txt" will be created on the local harddisk b)The code fails to compile on a Unix machine because the directory Seperator is not correct C)A file is not created when the code is executed d) an excepton is trown at runtime if the file "large.txt" already exits e) The code fails to compile since this is not a valid constructor for the File class. I thought the correct answers were b and c . But the answer given is only c. Isn't the file separator '\\' unique to windows ? I do not have unix on my machine to try it out. Can any one help out ? Regards vivek
Jane Griscti
Ranch Hand
Joined: Aug 30, 2000
Posts: 3141
posted
0
Hi Vivek, You're right, the File class uses the filename conventions of the host system; which means the code should cause an error on a Unix system. If you want the code to run on different systems you need to separate the 'path' and 'filename' components and use the 'pathSeparatorChar' which is a field in the File class. Think it's safe to assume that if a question uses a Window pathname construct the underlying system is also Windows; at least for the purpose of the example. Hope that helps. Jane
Vivek and Jane, Yes, the code will most likely cause an error at runtime on Unix, but it will compile just fine. Answer b states that the code fails to compile, and is therefore incorrect.
sachin patel
Ranch Hand
Joined: Nov 28, 2000
Posts: 75
posted
0
import java.io.*; class Demo { public static void main(String a[]) { String tmp=""; File f = new File("C:\\Large.txt"); System.out.println(f.exists()); } }
OUTPUT false
Hello i tried this on unix machine and it compiled fine and when i run the program its output was "false"..... ------------------ Sachin, **************************************************** Learn from others mistakes. Life is too short to make all yourself. ****************************************************
Sachin,<P>****************************************************<BR>Learn from others mistakes. Life is too short to make all yourself.<BR>****************************************************
Maitham H
Greenhorn
Joined: Jan 07, 2001
Posts: 29
posted
0
I think, if we take a close look at what the exam Objectives are. we can easily eliminate answer b. Sun is not interested in testing your OS knowledge, so they won't possibly ask questions or want answers to questions that will involve specific Operating Systems architectures.
A Software life cycle can be greater than its Developer's
Susan Hoover
Ranch Hand
Joined: Jan 04, 2001
Posts: 64
posted
0
Whoops, I just reread my previous reply and realized it might be unclear. When I said
the code will most likely cause an error at runtime on Unix
I did not mean to imply that it would cause an exception to be thrown, just that the file named "C:\\Large.txt" would not be found, but that would still be some sort of problem occurring at runtime and not at compile time. My second sentence above still stands.
Vivek Shinde
Greenhorn
Joined: Jan 09, 2001
Posts: 7
posted
0
Thanks Everybody, You were a great help Vivek
nan sh
Ranch Hand
Joined: Jan 05, 2001
Posts: 167
posted
0
Hi, my answer of this question: C is correct. explaination for a) is wrong when u instance a File class nothing will be created in harddisck, b) is wrong whatever directory seperator for the platform is correct or not no compiler error, no run time exception, it just sinks. but u can use isexist() to check. c) is correct, if u instance a object of File class with a file name not exist, only problem u will have ,when u try to read(or write) with the stream which created by this File object, u will get a IOException. d) is wrong, but on contrary is right(just c option) e) is wrong see c) explaintantion.
Have you tried this Mock Exam Testing Engine yet?<br /><a href="http://www.mycgiserver.com/~nan111/index.html" target="_blank" rel="nofollow">www.mycgiserver.com/~nan111/index.html</a>
Jane Griscti
Ranch Hand
Joined: Aug 30, 2000
Posts: 3141
posted
0
Thanks Susan, Sachin .... Looks like it will both compile and run fine on another OS ... you just won't find the file.