About Question enthuware.ocpjp.ii.v11.2.1353 :

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

Moderator: admin

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

About Question enthuware.ocpjp.ii.v11.2.1353 :

Post by kabanvau »

Hi,
This code will thow an Exception at runtime:

Code: Select all

System.out.println(rs.getString(0));
Columns are numbered from 1.

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

Re: About Question enthuware.ocpjp.ii.v11.2.1353 :

Post by admin »

Did you read the explanation? To throw an exception, the code has to compile first!
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.ii.v11.2.1353 :

Post by kabanvau »

Yes, it does not compile. But it is wrong to number columns from 0.

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

Re: About Question enthuware.ocpjp.ii.v11.2.1353 :

Post by admin »

It is true that it is wrong to number columns from zero. But neither the question nor the explanation says that it is ok to number columns from 0. It just that in this case, the given code does not compile, so the question of throwing exception does not arise.
If you like our products and services, please help us by posting your review here.

teodorj
Posts: 33
Joined: Tue Jun 19, 2018 10:27 am
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.1353 :

Post by teodorj »

In the explanation of this option...
It will not compile.
Observe that the declaration of try-with-resources statement is wrong. The type of the resource must be specified in the try itself. i.e. it should be: try(Statement stmt = c.createStatement())
Meaning that the resource used in try with resources in this example "stmt" must be final or effectively final or declare like in the explanation.
Am I correct?

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

Re: About Question enthuware.ocpjp.ii.v11.2.1353 :

Post by admin »

Yes, the try-with-resources resource must either be a variable declaration or an expression denoting a reference to a final or effectively final variable.
Enhanced the explanation to make it clearer.
thank you for your feedback!
If you like our products and services, please help us by posting your review here.

supafly
Posts: 13
Joined: Mon Apr 18, 2016 2:05 pm
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.1353 :

Post by supafly »

at the bottom of the question: (Assume that items not specified such as import statements and try/catch block are all valid.)

the second part is a bit confuesing since the try with resource stmt is invalid.

hsnclk
Posts: 16
Joined: Sat Dec 16, 2023 7:22 am
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.1353 :

Post by hsnclk »

Even if we declare the Statement properly in the try-resource, we have an unhandled Exception problem caused by DriverManager.getConnection.

Image
Attachments
Screenshot 2023-12-16 at 15.28.31.png
Screenshot 2023-12-16 at 15.28.31.png (161.93 KiB) Viewed 498 times

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

Re: About Question enthuware.ocpjp.ii.v11.2.1353 :

Post by admin »

The unhandled exception problem that you are getting is because you are assuming that the code fragment is in a method without any throws clause. The note at the bottom is now updated as:
(Assume that the method in which this code appears has appropriate throws clause.)
thank you for your feedback!
If you like our products and services, please help us by posting your review here.

hsnclk
Posts: 16
Joined: Sat Dec 16, 2023 7:22 am
Contact:

Re: About Question enthuware.ocpjp.ii.v11.2.1353 :

Post by hsnclk »

My pleasure :) I thank you!! It was really eye-opening question.

Post Reply

Who is online

Users browsing this forum: No registered users and 31 guests