Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Question on collections

 
Ram Murthy
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
srikanth reddy
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi ravi ]

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

thanks

sri
 
Ram Murthy
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here u go....

http://www.javabeat.net/javabeat/scjp2/mocks/index.php
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic