• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

I/O problem from Amit poddar's test

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
this question is from Amit poddar's moch test. The answer is" b".
but my question is why not Ans: a, becoz according to RHE(PAge no.
379)
"The File must describe a directory not a file." so this would be a
compile time error as File has argument "12345.msg" that is a file not
a dir.
please explain me.
**************************
Q40) What will be the output if
Consider a directory structure like this (NT or 95)
C:\JAVA\12345.msg --FILE
\dir1\IO.class -- IO.class is under dir1
import java.io.*;
public class IO {
public static void main(String args[]) {
File f = new File("12345.msg");
String arr[] = f.list();
System.out.println(arr.length);
}
}
a) Compiler error as 12345.msg is a file not a directory
b) java.lang.NullPointerException at run time
c) No error , but nothing will be printed on screen
***********************


------------------
Praveen Kumar
Mumbai ,India
email: bades@vsnl.com
 
Ranch Hand
Posts: 347
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Praveen,
This is from the Java 2 specifications from Sun:
public String[] list()
Returns an array of strings naming the files and directories in the directory denoted by this abstract pathname.
If this abstract pathname does not denote a directory, then this method returns null. Otherwise an array of strings is returned, one for each file or directory in the directory.
I think the reason the answer is given as b is because, like you said, 12345.msg is NOT a directory. Therefore,

should return null.
In the next line, they try to access arr.length with a null, hence the exception.
Hope this helps.
Stephanie
 
Praveen Kumar
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephanie, Thanx for replying.

------------------
Praveen Kumar
Mumbai ,India
email: bades@vsnl.com
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Could you please e-mail Amit poddar's mock exam to me:
han@fsiinc.com
thank you very much!
Hanmeng
 
I will suppress my every urge. But not this shameless plug:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic