| Author |
Enthuware Toughest questions on Collections
|
Mohit G Gupta
Ranch Hand
Joined: May 18, 2010
Posts: 634
|
|
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
|
OCPJP 6.0 93%
OCPJWCD 5.0 98%
|
 |
Abimaran Kugathasan
Ranch Hand
Joined: Nov 04, 2009
Posts: 2066
|
|
|
Don't you execute this code? What is the answer?
|
|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
|
 |
Mohit G Gupta
Ranch Hand
Joined: May 18, 2010
Posts: 634
|
|
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
Joined: Sep 19, 2010
Posts: 68
|
|
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
Joined: Sep 19, 2010
Posts: 68
|
|
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
Joined: Jun 23, 2010
Posts: 431
|
|
|
hi sumit,see this question
|
SCJP 6 | FB : Java Certifications-Help. | India Against Corruption
|
 |
Sumit Khurana
Ranch Hand
Joined: Sep 19, 2010
Posts: 68
|
|
|
in this question...what is the problem??
|
 |
 |
|
|
subject: Enthuware Toughest questions on Collections
|
|
|