Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes 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
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

collections framework doubt

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi friends ,

will such questions ( implementing comparator... using compare() etc come for the exam?)




choose:
Compilation succeeds, compare() throws ClassCastException
Compilation succeeds, the output is 0 1 2 3
Compilation succeeds, the output is 1 2 3
Compile time error, the compare() method does not have the correct signature
Compile Time error, compareTo method from the Comparator interface has not been implemented.


Solution provided:
Answer 3 is correct.
The compilation succeeds and the output will be in ascending order (e.g.: 1 2 3).
The class Car is a Comparator but at the same time also a business object that contains the total of wheels for the car.

At line 20 the TreeSet is created, the TreeSet of the constructor takes in the Car() Comparator, this constructor creates a new empty set sorted according to the Car criteria. All elements must be mutually comparable by the car.compare(Object o1, Object o2) method. The Car object used to create the TreeSet does not belong to the set.

The cars are sorted by the total amount of wheels. 3 new objects are created and added to a list. When the TreeSet start sorting it will call the compare method and it compares the total of wheels. It uses the Integer compareTo method.
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Depends whether you are taking SCJP 1.4 or 1.5
 
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 Vikrant Sahdev:
Depends whether you are taking SCJP 1.4 or 1.5


Aren't these questions on both?
 
Radha Kamesh
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry i forgot to mention that...

I meant the SCJP 1.4 exam...

Thanks,
Radha Kamesh
[ August 31, 2007: Message edited by: Radha Kamesh ]
 
All of life is a contant education - Eleanor Roosevelt. Tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic