• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

Mock Question Doubt...EQ+

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Question ID : com.enthuware.ets.scjp.v5.2.132

Consider the following code:



Select the correct statements about the above program.

PLEASE SELECT ONE OPTION

A.// 1 will throw an exception at runtime if a file named "x" does not exist.

B.//2 and //3 will compile without any error.

C.//4 will compile without any error.

D.The complete program will compile without any error

E.None of these.

Its says the correct answer is B..But whats wrong with A..??
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

A.// 1 will throw an exception at runtime if a file named "x" does not exist.



FileNotFoundException. Choose A as well.
 
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Line 1 won't ever cause an Exception, whether a file named "x" exist or not. The following code would create a file named "x":

Line 2 would cause the FileNotFoundException, because when creating a FileReader-Object with a File-Parameter, the JVM tries to open the file, specified by the File-Object.
 
Deepak Bala
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sasha Ruehmkorf:
Line 1 won't ever cause an Exception, whether a file named "x" exist or not. The following code would create a file named "x":

Line 2 would cause the FileNotFoundException, because when creating a FileReader-Object with a File-Parameter, the JVM tries to open the file, specified by the File-Object.



Ah it seems I missed a minor detail. That is correct, the other line is the one that actually searches for the file.
 
Ranch Hand
Posts: 185
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The correct Answere is 'B'

Option A: New File("X") will create a file named "X" in the current directory.It will Not throw any exception.
Compile without error

public static void main(String[] args) {
File f = new File("x"); //1
//BufferedReader bfr1 = new BufferedReader(new FileReader(f)); //2
//BufferedReader bfr2 = new BufferedReader( bfr1 ); //3
//PrintWriter pw = new PrintWriter(new FileReader(f)); //4
}
I have removed the throws clause from the main method and still it compiles that means it will not throw any exception!!! if you uncomment the second line ie, //2, it will not compile as that line may throw FileNotFoundException.

Hope you got the point.

Thanks&Regards
Krishna
SCJP1.4
SCWCD1.4
 
Sasha Ruehmkorf
Ranch Hand
Posts: 115
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Option A: New File("X") will create a file named "X" in the current directory.


That's not true. The first line is not creating a real file on the hard drive, it creates an abstract File-Object, nothing more happens.
 
Get out of my mind! Look! A tiny ad!
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic