Win a copy of Spring Boot in Practice this week in the Spring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Liutauras Vilda
  • Henry Wong
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Mikalai Zaikin
  • Himai Minh

Question on collections

 
Ranch Hand
Posts: 91
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here u go....

http://www.javabeat.net/javabeat/scjp2/mocks/index.php
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic