• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Wrong answer in Whizlabs Practice Exam 1, question 59

 
Ranch Hand
Posts: 241
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Drag and drop question. This is the answer provided:


However,

int in = Arrays.binarySearch(a, 12);

can also be replaced by The code will compile just the same and print 4. Since Object was given as one of the possible fragments, it is also correct.
 
Enthuware Software Support
Posts: 4810
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
binarySearch returns an int but due to automatic wrapping of primitives, it can be assigned to Object reference. So you are right.
 
Ranch Hand
Posts: 206
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, it's true that you could replace "int in = Arrays.binarySearch(a, 12);" with "Object in = Arrays.binarySearch(a, 12);" . Both are working, but "Object in = Arrays.binarySearch(a, 12); " require more work for the compiler or human.

Here is why:







In reference to K & B book page 614 on "Searching Arrays and Collections" topic, the rules apply for search are:

Searches are performed using the binarySearch() method.
■ Successful searches return the int index of the element being searched.
■ Unsuccessful searches return an int index that represents the insertion point.
The insertion point is the place in the collection/array where the element
would be inserted to keep the collection/array properly sorted. Because posi-
We’ve talked a lot about sorting by natural order and using Comparators
to sort. The last rule you’ll need to burn in is that, whenever you want to sort an array
or a collection, the elements inside must all be mutually comparable. In other words, if you
have an Object[] and you put Cat and Dog objects into it, you won’t be able to sort
it. In general, objects of different types should be considered NOT mutually comparable,
unless specifi cally stated otherwise.

Because positive return values and 0 indicate successful searches, the binarySearch()
method uses negative numbers to indicate insertion points. Since 0 is a valid
result for a successful search, the first available insertion point is -1. Therefore,
the actual insertion point is represented as (-(insertion point) -1). For
instance, if the insertion point of a search is at element 2, the actual insertion
point returned will be -3.



 
Dinner will be steamed monkey heads with a side of tiny ads.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic