Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Problem in question on TreeMap because of Java 1.6 and Java 1.7 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Problem in question on TreeMap because of Java 1.6 and Java 1.7" Watch "Problem in question on TreeMap because of Java 1.6 and Java 1.7" New topic
Author

Problem in question on TreeMap because of Java 1.6 and Java 1.7

Sidharth Khattri
Ranch Hand

Joined: Sep 16, 2013
Posts: 121

Question is from CertPal
So, I'm compiling this code through cmd and have Java 1.7 installed on my system. When I try to compile this code, I get a ClassCastException:
Exception in thread "main" java.lang.ClassCastException: 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 Drip.main(Drip.java:8)

But the answer given on CertPal is:
1. Explanation:
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

This problem have been asked in the following thread:
http://www.coderanch.com/t/607606/java-programmer-SCJP/certification/CertPal-confusion

What I want to ask is, if such kind of a question appears on the exam, what should be my answer? "Runtime Exception" or "Hi"?

Given this program what will be the output ?
Options

1.) Hi

2.) No output

3.) Compile error

4.) Runtime error

5.) None of the above


OCPJP 6 - 96%
Sidharth Khattri
Ranch Hand

Joined: Sep 16, 2013
Posts: 121

Guys? Is anyone going to reply?
And similarly, does TreeSet/TreeMap allow null value to be added to it if only one add/put statement is used for adding that null value to the Set/Map? As in the above example.

On my system, I'm getting ClassCastException(for the example in original post above) and NullPointerException(for the second problem stated in this post) whether I use javac or javac -source 1.6 command line statement.
Dieter Quickfend
Bartender

Joined: Aug 06, 2010
Posts: 503
    
    4

Don't ask what the correct answer is, but what the problem is. Do you know what the problem is? If you know what the problem is, then it will be easy to answer this question.



Read up on the TreeSet and Comparable interface. TreeSet, btw, uses the object's 'compareTo()' method to order its elements. It will call compareTo whenever there is more than one element in the set. What will happen when you call "x.compareTo(y)" when x is null? This should clue you in on what happens in your code sample.


Oracle Certified Professional: Java SE 6 Programmer && Oracle Certified Expert: (JEE 6 Web Component Developer && JEE 6 EJB Developer)
Sidharth Khattri
Ranch Hand

Joined: Sep 16, 2013
Posts: 121

Dieter Quickfend wrote:Don't ask what the correct answer is, but what the problem is. Do you know what the problem is? If you know what the problem is, then it will be easy to answer this question.



Read up on the TreeSet and Comparable interface. TreeSet, btw, uses the object's 'compareTo()' method to order its elements. It will call compareTo whenever there is more than one element in the set. What will happen when you call "x.compareTo(y)" when x is null? This should clue you in on what happens in your code sample.


I read the same thing like you said, but when I run it on my system, I get the exception when I add only one element to the TreeSet. So, I'm not sure what should I mark as the correct answer if I encounter such problem in the real exam.
Because of this doubt, another thing popped up in my mind that TreeSet and TreeMap also won't throw any Exception if I add only one null element/key, because Comparable will only kick in when second element/key is added.
I read that because of different java versions the output is different in this case. So, I tried compiling using java -source 1.6, but got the same output, ClassCastException/NullPointerException even though I added only one element/key to the Set/Map.

I just don't want to make a mistake on the exam because of something I ignored, that's why I made this thread.
Any help would be appreciated.
Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1000
    
  16
If I got this question in the SCJP exam, I would choose option 3 - Compile error.

Chan.
Sidharth Khattri
Ranch Hand

Joined: Sep 16, 2013
Posts: 121

Chan Ag wrote:If I got this question in the SCJP exam, I would choose option 3 - Compile error.

Chan.


I copied this question from CertPal, I think they are assuming appropriate import statements are given for every code though they won't mention it on the question.
E Armitage
Rancher

Joined: Mar 17, 2012
Posts: 892
    
    9
You must choose an answer according to the Javadocs or Java language specification

Throws:
ClassCastException - if the specified object cannot be compared with the elements currently in this set
NullPointerException - if the specified element is null and this set uses natural ordering, or its comparator does not permit null elements

Implementations can have bugs which can get fixed as happened with this RFE http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5045147
Sidharth Khattri
Ranch Hand

Joined: Sep 16, 2013
Posts: 121

E Armitage wrote:You must choose an answer according to the Javadocs or Java language specification

Throws:
ClassCastException - if the specified object cannot be compared with the elements currently in this set
NullPointerException - if the specified element is null and this set uses natural ordering, or its comparator does not permit null elements

Implementations can have bugs which can get fixed as happened with this RFE http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5045147


As I see the bug got fixed, should I choose
1.) Hi
4.) Runtime error ?
Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1000
    
  16
Isn't the OCPJP exam either an OCPJO6 or an OCPJP7 or doesn't it say it is with respect to a particular version of Java? If so, why not answer accordingly?
Sidharth Khattri
Ranch Hand

Joined: Sep 16, 2013
Posts: 121

Chan Ag wrote:Isn't the OCPJP exam either an OCPJO6 or an OCPJP7 or doesn't it say it is with respect to a particular version of Java? If so, why not answer accordingly?


Thanks Chan. Someone should have just confirmed it instead of so many circuitous replies :P
Chan Ag
Bartender

Joined: Sep 06, 2012
Posts: 1000
    
  16
You are welcome. There is nothing like a 'should have' though.
Sidharth Khattri
Ranch Hand

Joined: Sep 16, 2013
Posts: 121

Chan Ag wrote:You are welcome. There is nothing like a 'should have' though.


Sorry, I mean *could have
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Problem in question on TreeMap because of Java 1.6 and Java 1.7
 
Similar Threads
NoClassDefFoundError with packages
CertPal question confusion.
java
SCJP 1.4 Answer Options Format - Like Dan or Marcus ?
query