Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Enthuware Toughest questions on Collections

 
Mohit G Gupta
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


 
Abimaran Kugathasan
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
 
Sumit Khurana
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..

}
 
Arjun Srivastava
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??
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic