File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


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 "Generics SCJP 1.5" Watch "Generics SCJP 1.5" New topic
Author

Generics SCJP 1.5

Sagar Jani
Greenhorn

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 :

Given:
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. }
and:
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 ?

Regrads,
Sagar
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
Greenhorn

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..


Regards,
Vishwa


Blog
 
wood burning stoves
 
subject: Generics SCJP 1.5
 
Similar Threads
compareing two values
probem with comparable interface
HashSet
Mock Exam doubt Generics
Please help... Black magic java generics