Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Generics SCJP 1.5 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 "Generics SCJP 1.5" Watch "Generics SCJP 1.5" New topic

Generics SCJP 1.5

Sagar Jani

Joined: Dec 10, 2007
Posts: 15
Hi folks,
Following is the code snippet I got from whizlabs mock test, which I am unable to understand :

1. public class Test {
2. public <T extends Comparable> T findLarger(T x, T y) {
3. if(x.compareTo(y) > 0) {
4. return x;
5. } else {
6. return y;
7. }
8. }
9. }
22. Test t = new Test();
23. // insert code here
Which two will compile without errors when inserted at line 23?
(Choose two.)
A. Object x = t.findLarger(123, �456�);
B. int x = t.findLarger(123, new Double(456));
C. int x = t.findLarger(123, new Integer(456));
D. int x = (int) t.findLarger(new Double(123), new Double(456));

Answer : AC

Can anybody explain me ?

Sandeep Bhandari
Ranch Hand

Joined: Apr 16, 2004
Posts: 201
Its more about Autoboxing than Generics.

Just a hint:
1)a primitive integer can be wrapped in Integer object
2)a String can be represented as integer and wrapped in Integer object as the overloaded constructor accepts String objects too

A integer can't be wrapped into Double object as the method says (T x, T y)

and D option is wrong because of typecasting of Double to int (Wider to narrower)

SCJP 96% | SCWCD 90%| SCJP mock exams | My SCJP blog
Sagar Jani

Joined: Dec 10, 2007
Posts: 15
Thanks Sandeep. But when I try these options all the options are compiled without errors. Because T can be any object.

So why only AC ?
Vishwanath Krishnamurthi
Ranch Hand

Joined: Jun 04, 2007
Posts: 331
Hi Sagar,

But when I try these options all the options are compiled without errors.

Really? But it did give me errors, when I tried...

So why only AC ?

Considering option B)

I think, this method invocation's signature would be like findLarger(Integer, Double) and even if it did compile, would cause a ClassCastException when it tries to compare an Integer with a Double...

But the compiler even before that guesses that there is a possibility that
a Double can be returned by the method(in the else part) and that it cannot be assigned to an int without an explicit cast... and hence the compiler error...

Considering Option D)
int x = (int) t.findLarger(new Double(123), new Double(456));

Now here, if at all it executes, there wont be any problems with the comparison of a Double with a Double and there wont be any ClassCastException... But then the compiler sees that you are trying to cast a Double to an int which is not possible... (Double to double is ok, double to int is ok, but Double to int is not...) and hence the error.

Options A and C are fine, but still option A gives a ClassCastException on running since a String cannot be compared with an Integer... anws thats not a compiler error, so option A holds..


I agree. Here's the link:
subject: Generics SCJP 1.5
jQuery in Action, 3rd edition