This week's book giveaway is in the Artificial Intelligence forum.
We're giving away four copies of Pragmatic AI and have Noah Gift on-line!
See this thread for details.
Win a copy of Pragmatic AI this week in the Artificial Intelligence forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Enthuware Toughest questions on Collections  RSS feed

 
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Toughest Questions for Oracle Java Programmer Certification
Enthunware


Q. No. 11







What will it print when it is compiled and run?



(a) It will compile with a warning and run without throwing an exception.
(b) It will compile without a warning and will throw an exception when run.
(c) It will compile and run without warning and exception.
(d) It will print 10 and -1.
(e) It will print 10 and 0.
(f) It will print 10.


answer are b,f

shouldn't be answer be 10 ,null
as get returns null if there is no value corresponding to given key


please help


 
Ranch Hand
Posts: 2066
Clojure IntelliJ IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Don't you execute this code? What is the answer?
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have removed
b.setIsbn("111");

from the code and then ran the program

.The output is same as given in the answer 10 followed by a Exception

help
 
Ranch Hand
Posts: 68
Chrome Java MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
HashMap add the key,value pair by using hashcode and equals method but book class does not implement the hash code and equals. so,every new object is different from the previous one and you can't search through the hashmap unless you have the reference of object when you are adding in the hashMap.


Exception occurs because when you execute the method
bs.getNumberOfCopies(b);

this method starts finding the b in HashMap and it does not find any b object because each object is different(because we have not implemented the hashcode). and hashmap returns null.

int getNumberOfCopies(Book b){

return map.get(b); //now here exception occurs because jvm can't convert null to zero..

}
 
Sumit Khurana
Ranch Hand
Posts: 68
Chrome Java MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Sumit Khurana wrote:
int getNumberOfCopies(Book b){

return map.get(b); //now here exception occurs because jvm can't unbox null to zero..

}

 
Ranch Hand
Posts: 432
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi sumit,see this question
 
Sumit Khurana
Ranch Hand
Posts: 68
Chrome Java MyEclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in this question...what is the problem??
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!