Win a copy of Machine Learning with R: Expert techniques for predictive modeling this week in the Artificial Intelligence and Machine Learning 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:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

Enthuware Toughest questions on Collections

 
Ranch Hand
Posts: 634
Eclipse IDE Chrome 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
IntelliJ IDE Clojure 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
Eclipse IDE Chrome 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
MyEclipse IDE Chrome Java
  • 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
MyEclipse IDE Chrome Java
  • 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
MyEclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in this question...what is the problem??
 
If I had asked people what they wanted, they would have said faster horses - Ford. Tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!