About Question enthuware.ocpjp.i.v11.2.3142 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
rowdyy
Posts: 3
Joined: Fri Jul 10, 2020 7:30 pm
Contact:

About Question enthuware.ocpjp.i.v11.2.3142 :

Post by rowdyy »

Test -1

83
-----------

Given: public static void main(String[] args){ try(FileInputStream fis = new FileInputStream("c:\\temp\\test.text")){ System.out.println(fis); } //line 1 }

Which of the following options can be inserted at //1 independent of each other to make the above code compile? Note that FileInputStream, try-with-resources, and multi-catch are NOT mentioned in the official exam objectives. However, we have seen questions in the exam that require knowledge on these topics. It is possible that these are questions are unscored i.e. their score doesn't count towards your final score. Since we have no way of knowing that, it is better to read up on these topics.

catch(IOException){ } ; This was specified as correct answer; but there is not identifier; like IOExceptio ioe;

This wont compile. Thats why i did not select this.


catch(IOException e){ } finally{  fis.close(); } -> This seems better option than the before one. But this option is wrong as per Enthuware.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.i.v11.2.3142 :

Post by admin »

You are right. It should have the parameter name. Fixed.

The last option invalid because the close() method on FileInputStream declares that it may throw IOException. So, the exception from the call to fis.close(); remains unhandled.
If you like our products and services, please help us by posting your review here.

kabanvau
Posts: 14
Joined: Thu Nov 21, 2019 5:48 am
Contact:

Re: About Question enthuware.ocpjp.i.v11.2.3142 :

Post by kabanvau »

The last option invalid because the close() method on FileInputStream declares that it may throw IOException. So, the exception from the call to fis.close(); remains unhandled.
I think it will not compile because the variable fis is not accessible from within the finally block. The same is with the first option.

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.i.v11.2.3142 :

Post by admin »

You are right. Fixed.
thank you for your feedback!
If you like our products and services, please help us by posting your review here.

rrdeleon04
Posts: 1
Joined: Fri Apr 08, 2022 10:34 am
Contact:

Re: About Question enthuware.ocpjp.i.v11.2.3142 :

Post by rrdeleon04 »

Question for the first option

Code: Select all

finally{  tempFis.close(); }
Explanation:
The IOException thrown from the try clause is not being caught and is not declared in the throws clause either. Further, since the close() method also throws IOException, that exception also remains unhandled. Therefore, this code won't compile.

I tried this code in intellij and it was saying the new FileInputStream() is throwing the FIleNotFoundException. I got confused at first because the explanation said that the IOException in the try clause is the one that's not declared. However, when I tried to add a catch for IOException, the error was gone. I understand FileNotFoundException is a subclass of IOException which is why it worked. I just want to ask if the explanation should've specifically mention both FileNotFoundException & IOException was not being caught?

Or is this dependent on the java version used(mine is 11) that's why only IOException was mentioned?

admin
Site Admin
Posts: 10036
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.i.v11.2.3142 :

Post by admin »

Your confusion is valid. The explanation should say FileNotFoundException instead of IOException because that is what FileInputStream constructor says in its throws clause. The explanation says IOException loosely because all file i/o related exceptions extends from IOException. So, even though catching IOException is ok (it gets rid of the error) it is imprecise and should have been avoided.

Further, the close method throws IOException and since it is a super class of FileNotFoundException, if you handle IOException, FileNotFoundException is automatically taken care of. But yes, the explanation should make this clear.

thank you for your feedback!
Paul.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 50 guests