aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes CertPal question confusion. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "CertPal question confusion." Watch "CertPal question confusion." New topic
Author

CertPal question confusion.

Adam Crawford
Greenhorn

Joined: Feb 27, 2012
Posts: 9
Hi all,

I've got the SCJP 6 exam coming up in just over a week and was going over some CertPal questions for practice. This evening I came across this one:


With one of the following answers:
1) Hi
2) No Ouput
3) Compile error
4) Runtime error
5) None of the above

I chose 4 (runtime error) as the class doesn't implement Comparable, but it was marked wrong and the correct answer given was 1 (Hi) because:

"The code to compare the elements in the set will kick in only when the next element is added. Adding more than one element to this set will result in a runtime exception".

Yet, when I try this out, I get a runtime exception until I add an "implements Comparable" and a compareTo method - at which point I get the answer "Hi".

Am I missing something?

Thanks.
Adam.
Zhenyi Luo
Ranch Hand

Joined: Sep 03, 2012
Posts: 33
Adam Crawford wrote:Hi all,

I've got the SCJP 6 exam coming up in just over a week and was going over some CertPal questions for practice. This evening I came across this one:


With one of the following answers:
1) Hi
2) No Ouput
3) Compile error
4) Runtime error
5) None of the above

I chose 4 (runtime error) as the class doesn't implement Comparable, but it was marked wrong and the correct answer given was 1 (Hi) because:

"The code to compare the elements in the set will kick in only when the next element is added. Adding more than one element to this set will result in a runtime exception".

Yet, when I try this out, I get a runtime exception until I add an "implements Comparable" and a compareTo method - at which point I get the answer "Hi".

Am I missing something?

Thanks.
Adam.


When I try it out, it prints "Hi", no need to modify anything
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 758
To make it compile, Set should be iterated, not in a for loop.


When I add one Drip object, it compiles and runs. But when I add one more, it throws a class cast exception. Because to add an element to a tree, a second element need to be compared with the first element.
In the TreeSet code, it allows you to add any element as the first node in the tree. But when you add the 2nd, 3rd ... nodes to the tree, this node is cast to Comparator or Comparable. However, Drip is not a Comparable or Comparator. That is why it throws an exception.
Adam Crawford
Greenhorn

Joined: Feb 27, 2012
Posts: 9
Thanks for the explanation which I understand, BUT:

This is REALLY weird. Forgive me if it's something stupid that I can't see because I've been staring at it for too long, but I tried this code at work and, like you say, it outputs "Hi".

Yet, when I type the exact same code on my home PC either via Eclipse or notepad, I get the following:

Exception in thread "main" java.lang.ClassCastException: uk.org.dreamboats.certpal.Drip cannot be cast to java.lang.Comparable
at java.util.TreeMap.compare(Unknown Source)
at java.util.TreeMap.put(Unknown Source)
at java.util.TreeSet.add(Unknown Source)
at uk.org.dreamboats.certpal.Drip.main(Drip.java:14)

How can that be??? Here's the home version giving the exception:
Adam Crawford
Greenhorn

Joined: Feb 27, 2012
Posts: 9
Ok - I'm not going mad - here's the problem.

I recently updated to Java 1.7 at home but am still using 1.6 at work.

There was a known issue in 1.6 that's now been fixed for 1.7 - it should have been causing a ClassCastException

Known Issues addressed in JDK 7

Area: API: Utilities
Synopsis: Due to an error in java.util.TreeMap, it was previously possible to insert invalid null elements and elements not implementing Comparable into empty TreeMaps and TreeSets. Only a single invalid element could be inserted into the empty TreeMaps or TreeSets; additional elements would cause the expected NullPointerException or ClassCastException. Most other operations upon the collection would also fail. As of JDK 7, inserting an invalid null element or an element not implementing Comparable into an empty TreeMap or TreeSet throws a NullPointerException.
RFE: 5045147

Or, in my case a ClassCastException as it's inserting a not null element.

One to remember.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: CertPal question confusion.