About Question enthuware.ocajp.i.v7.2.925 :

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
Sweetpin2
Posts: 27
Joined: Thu Feb 07, 2013 9:46 pm
Contact:

About Question enthuware.ocajp.i.v7.2.925 :

Post by Sweetpin2 »

For this question the declaration

ArrayList<Double> al = new ArrayList<>();

is itself incorrect (Type mismatch: cannot convert from ArrayList<?> to ArrayList<Double>)

We need to declare like below

ArrayList<Double> al = new ArrayList<Double>();

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

Re: About Question enthuware.ocajp.i.v7.2.925 :

Post by admin »

Please make sure you are using JDK 7 for compilation.
If you like our products and services, please help us by posting your review here.

JackBo
Posts: 2
Joined: Wed Feb 05, 2014 3:57 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.925 :

Post by JackBo »

Shouldn't the explanation of the correct answer "System.out.println(al.indexOf(1.0));" be :
"ArrayList.indexOf takes Object as parameter"
instead of :
"1.0 is a double and so it can be boxed into a Double object."

?

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

Re: About Question enthuware.ocajp.i.v7.2.925 :

Post by admin »

No, in the given code, al is declared as ArrayList<Double>. So indexOf and add will require Double (not Object). That is why the explanation says that 1.0 will be boxed into a Double.
Explanation has now been enhanced to explain this point.

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

JackBo
Posts: 2
Joined: Wed Feb 05, 2014 3:57 pm
Contact:

Re: About Question enthuware.ocajp.i.v7.2.925 :

Post by JackBo »

But the following code runs and uses ArrayList.indexOf(Object) :

ArrayList<Double> list = new ArrayList<>();
System.out.println(list.indexOf("test"));

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

Re: About Question enthuware.ocajp.i.v7.2.925 :

Post by admin »

I see your point now. You are right. This has now been fixed.

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: No registered users and 134 guests