• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

double array list

 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys, I'm not sure I understtod the answers to this question (
Glenn, Mitchell. OCAJP Oracle Certified Associate Java SE 8 Programmer Practice Exams (Kindle Locations 18713-18715). Enthuware. Kindle Edition.):
Given:

What can be inserted in the above code so that it can compile without any error?
Choose two option:
A.
B.
C.
D.


I got that wrong, and the answers are B C
Now, given the code above answer B is looking for 1.0 inside the array lists, it doesn't find it and it returns -1 correct? Answer C, I really havve no idea. I mean, how is that correct? Doesn't it return a boolean value?
 
Henry Wong
author
Marshal
Pie
Posts: 22124
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:
Now, given the code above answer B is looking for 1.0 inside the array lists, it doesn't find it and it returns -1 correct? Answer C, I really havve no idea. I mean, how is that correct? Doesn't it return a boolean value?


First, the question is referring to "compile without any error", meaning compiling correctly. There is no mention of running without exceptions, or whether even running the resultant code even produces results that makes sense.

For B, the indexOf() method takes an Object. With autoboxing, the "-1.0" is boxed to a Double (which IS-A Object). The result is an int, and the PrintStream println() method can take an int argument.

For C, the contains() method takes an Object. A String IS-A Object. The result is a boolean, and the PrintStream println() method can take an boolean argument.

So, why shouldn't B and C compile?

Henry
 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for that.
First, the question is referring to "compile without any error", meaning compiling correctly.
OK understood.
For B, the indexOf() method takes an Object. With autoboxing, the "-1.0"
Sorry I don't understand why it returns -1.0. shouldn't it return -1, an int instead as I thought that indexOf returns the index f the searched element or the negative of that index -1?
 
Henry Wong
author
Marshal
Pie
Posts: 22124
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:
For B, the indexOf() method takes an Object. With autoboxing, the "-1.0"
Sorry I don't understand why it returns -1.0. shouldn't it return -1, an int instead as I thought that indexOf returns the index f the searched element or the negative of that index -1?

I didn't say that the method returns a Double object. Autoboxing that is mentioned here, is for the parameter. The -1.0 primitive value is boxed to a Double object prior to the method call. This is mentioned because this is what is needed to allow the method call to resolved.

As for the return value, you cut off my quote regarding that...
Henry Wong wrote:The result is an int, and the PrintStream println() method can take an int argument.

For the return value, I didn't even mention the value at all. The only mention was the type, in that it was an int. The reason is because this is what is needed to allow for the println() method call to resolve.

Remember, this question is about whether or not it compiles. It doesn't matter what the value is, as long as it can be printed -- as supported by the println() method.

Henry
 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, thanks, I don't mean to be thick but then, where is that -1.0 coming from then if it's so a positive double and not a negative one?
 
Henry Wong
author
Marshal
Pie
Posts: 22124
88
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jason Attin wrote:Right, thanks, I don't mean to be thick but then, where is that -1.0 coming from then if it's so a positive double and not a negative one?


Oops, I seemed to have misread the questions. Please change my response to...


For B, the indexOf() method takes an Object. With autoboxing, the double is boxed to a Double (which IS-A Object). The result is an int, and the PrintStream println() method can take an int argument.


It kinda serves me right for not remaining consistent. Just like with the return value, the actual value for the autoboxing is not relevant to this question.

Sorry,
Henry
 
Jason Attin
Ranch Hand
Posts: 232
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not a problem, don't worry, it's clear now :-)!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic