Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt on Inquisition mock exam

 
Mamadou Touré
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following code is from the inquisition SCJP quizz by the muppeteer:


import java.util.Set;
import java.util.TreeSet;


public class Testing123 {

public static void main(String... args)
{
Set t = new TreeSet();
t.add("a");t.add("b");t.add(666);
for(Object o:t)System.out.print(o);
}

}

Choose one option

1 - Thise doesn't compile
2 - ab666
3- ab

My response was that none of the 3 (unfortunately there no such option), but the resulte is 1 (doesn't compile), and this is the reason given

Every collection with 'Tree' in it, need objects that implement Comparable. Next to that they have to be mutually comparable and Integer (666 is autoboxed) and String are not.

I tried to paste the code on my IDE and compile it, it compiles fine, and when I execute it, it raises the following exception :
Exception in thread "main" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer

So I think the response given bu the inquisition (option 1) is not correct
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are correct. Using javac, it compiles (with warnings). The exception is thrown at runtime.
 
Raphael Rabadan
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We need to take care with those mocks. Sometimes they do some mistakes like this. Like said before, it does compile, but will throw a ClassCastException in runtime when trying to compare a String to a Integer.
[ July 11, 2008: Message edited by: Raphael Rabadan ]
 
Matt Russell
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yep, that's a bug -- I'm guessing you must be running an old version (< 0.14) of Inquisition, because that error was fixed in the release last month.
 
Mamadou Touré
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Matt,

Ok, I downloaded the newest version, an we have a forth option thats says "throws an exception at runtime"
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic