Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Doubt on Inquisition mock exam

 
Ranch Hand
Posts: 189
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You are correct. Using javac, it compiles (with warnings). The exception is thrown at runtime.
 
Ranch Hand
Posts: 141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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"
 
I am a man of mystery. Mostly because of this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic