Java J2EE Portal
Enterprise Java Station
J2EE curve
Java News / Articles
Java News / Articles
Using the Java ByteCode Verifier To Prevent Malicious Access
Rich Internet Applications and AJAX - Selecting the right technology
AJAXEnabled
Are you Ajax Enabled? Everybody else is...
Processing...
Buy Java, Deals On Software Technology Store
Click here for great deals on computers, laptops, software and books
Test your Java language skills - Java Quiz 3 PDF Print
Written by Content Team   
Jan 31, 2007 at 12:00 AM
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


Add This Feed Button

Enter your Email


Java Expert Interviews
Anil Saldhana JBoss
JBoss is light years ahead of other open source application servers
EclipseExecutiveDirectorMikeMilinkovich
Eclipse is focused on closing in on Visual Studio - Switching campaigns are for marke
RoelStalmanOracleJDeveloper
JDeveloper is the most comprehensive Java IDE available
Processing...
Go to top of page  Home |
SiteMap

Copyright 2004 to 2008 Rightrix Solutions. All rights reserved. All product names are trademarks of their respective companies. Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. Rightrix Solutions and IndicThreads.com are independent of Sun Microsystems, Inc.

Views expressed at IndicThreads.com reflect the views of the authors alone, and do not necessarily reflect those of IndicThreads.com. IndicThreads.com and it's authors are not responsible for reader comments and opinions.

Enterprise Java J2EE JEE Portal >> IndicThreads.com