This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question on collections Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question on collections" Watch "Question on collections" New topic
Author

Question on collections

Ram Murthy
Ranch Hand

Joined: Aug 02, 2005
Posts: 91
I was taking a mock exam on javabeat.com and got stumped by the first 5 questions , all of them were about collections and were programs. I had did my prepearation based on Kathy Sierra book and in it says we need not know in detail about collections. Anyway listed below is one of the question I faced, don't think from the theory I read I can answer this question...

Will the actual exam contain this kind of questions ??
If so can someone point me to some reading materials so that I can be prepared for such questions ?

import java.util.*;
public class Test1{
public static void main(String a[]){
Set s = new TreeSet();
s.add(new Person(20));
s.add(new Person(10));
System.out.println(s);
}
}
class Person{
Person(int i){}
}

What is the output?

A1 10 20
A2 ClassCastException
A3 Compiler Error
A4 Compiler Error

Answer is A2. ClassCastException

Cheers,
JP


Cheers,
Ram
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by JP Ravi:
... I had did my prepearation based on Kathy Sierra book and in it says we need not know in detail about collections...

You do not need to know extensive details, but you do need to know the basics. For example, elements in a TreeSet are ordered based on natural order. According to the API...
This class guarantees that the sorted set will be in ascending element order, sorted according to the natural order of the elements (see Comparable), or by the comparator provided at set creation time, depending on which constructor is used.

Ref: http://java.sun.com/j2se/1.4.2/docs/api/java/util/TreeSet.html

In this case, the problem is that the Person elements are not Comparable.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
srikanth reddy
Ranch Hand

Joined: Jul 28, 2005
Posts: 252
hi ravi ]

can u please send me the link for the mockexam in javabeat..as i was not able to find it ..

thanks

sri


Thanks & Regards<br /> <br />-Srikanth
Ram Murthy
Ranch Hand

Joined: Aug 02, 2005
Posts: 91
Here u go....

http://www.javabeat.net/javabeat/scjp2/mocks/index.php
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question on collections
 
Similar Threads
what is ClassCastException...any body plz Explain?
ClassCastException
why ClasscastException????
class cast exception ? how?
code