• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Paul Clapham
  • Devaka Cooray
  • Bear Bibeault
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Liutauras Vilda
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Piet Souris
Bartenders:
  • salvin francis
  • Carey Brown
  • Frits Walraven

collections framework doubt

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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 ]
 
Brace yourself while corporate america tries to sell us its things. Some day they will chill and use tiny ads.
Two software engineers solve most of the world's problems in one K&R sized book
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic