About Question enthuware.ocpjp.v7.2.1301 :

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
alex
Posts: 42
Joined: Tue Feb 12, 2013 4:35 pm
Contact:

About Question enthuware.ocpjp.v7.2.1301 :

Post by alex »

Hi,

StudyGroup mathGroup = new StudyGroup();       
mathGroup.add(new Person("MATH"));       
System.out.println("A");

There are two answers:
1. It will compile without warning but will throw an exception at runtime.
2. It will only print : A

Answer 2 is not correct. Because add method will throw Exception
Exception in thread "main" java.lang.ClassCastException: main.java.Person cannot be cast to java.lang.Comparable
at java.util.TreeMap.compare(TreeMap.java:1188)
at java.util.TreeMap.put(TreeMap.java:531)
at java.util.TreeSet.add(TreeSet.java:255)

If look at the code of TreeMap, they will compare keys beetween each other
public V put(K key, V value) {
Entry<K,V> t = root;
if (t == null) {
compare(key, key); // type (and possibly null) check

So, explanation: "Thus, when you add the first element, since there is nothing to compare this element to, there is no exception. " also is not correct.

Pleach check. (jdk 1.7.0_02)

alex
Posts: 42
Joined: Tue Feb 12, 2013 4:35 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1301 :

Post by alex »

Hi, any comments?

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1301 :

Post by admin »

We are still investigating. Apparently, the behavior changed recently. It used to work as given earlier.

-Paul.

alex
Posts: 42
Joined: Tue Feb 12, 2013 4:35 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1301 :

Post by alex »

Ok, my apologies.
I thought that you just missed my post.

Alex

admin
Site Admin
Posts: 10388
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: About Question enthuware.ocpjp.v7.2.1301 :

Post by admin »

Hi Alex,
In light of how it behaves currently, we have updated the question and the explanation.

thank you for your feedback!
Paul.

Post Reply

Who is online

Users browsing this forum: Google [Bot] and 4 guests