aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on File Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on File Class" Watch "Question on File Class" New topic
Author

Question on File Class

Vivek Shinde
Greenhorn

Joined: Jan 09, 2001
Posts: 7
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
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


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
Susan Hoover
Ranch Hand

Joined: Jan 04, 2001
Posts: 64
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
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
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
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
Thanks Everybody, You were a great help
Vivek
nan sh
Ranch Hand

Joined: Jan 05, 2001
Posts: 167
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
Thanks Susan, Sachin ....
Looks like it will both compile and run fine on another OS ... you just won't find the file.

Jane
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on File Class