| |
|
Test your Java language skills - Java Quiz 3 |
|
|
|
Written by Content Team
|
|
Jan 31, 2007 at 12:00 AM |
|
Page 1 of 2 A Java quiz that tests your knowledge of Generics, equals and the
hashCode
method. If you are looking for a user contributed database of Java
questions, check out the Java
Interview Questions Bank or check out Java
Quiz One and Java
Quiz Two .
These questions are courtesy of Whizlabs
Software and will help you prepare for the Sun Certified Java
Programmer Exam 5.0 (SCJP).
Question No. 1 -
What is the result of compiling and running the following code?
{moscode}
import java.util.*;
class Gen {
T obj;
Gen(T o){
obj=o;
}
T get() {
return obj;
}
}
class Generics19{
public static void main(String[] args){
Gen i1=new Gen(new Integer(10));
int i=i1.get();
System.out.println(i);
}
}
A.Compiler error
B.Exception
C.Prints 10
D.Prints 0
{/moscode}
Question No. 2 - Select the most
appropriate implementation of the hashCode() method
that can be inserted at line 15 in the following code. Assume that the
equals() method is implemented correctly in this class.
1. public class TestHash 2. { 3. private int length; 4. private int width; 5. private final double area; 6. 7. // only one constructor 8. public TestHash(int l, int w) 9. { 10. length = l; 11. width = w; 12. area = length * width; 13. } 14. 15. // what should be inserted here? 16. 17. // equals method not shown 18. }
A. public int hashCode() { int hash = 7; hash = 31 * hash + length; hash = 31 * hash + width; long bits = Double.doubleToLongBits(area); return hash; }
B. public int hashCode() { int hash = 7; hash = 31 * hash + length; hash = 31 * hash + width; long bits = Double.doubleToLongBits(area); hash = 31 * hash + (int)(bits (bits >>> 32)); return hash; }
C. public int hashCode() { int hash = 7; hash = 31 * hash + length; hash = 31 * hash + width; return hash; }
D. public long hashCode() { int hash = 7; hash = 31 * hash + length; return hash; }
Answers On Page 2
<< Start < Previous 1 2 Next > End >> |
|
|
|