• 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:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

probem with comparable interface

 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please tell me why answer is option A and C,why not D and B , Double also implements comparable interface as Integer and String .... !!!???

Question 181
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
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Bharat,

the declaration of findLarger requires that the types for x, y and the return value are the same. Now have a look at the statements.

Object x = t.findLarger(123, "456");
needed return value: anything
x: Integer
y: String
The common type for String and Integer is Object, which is fine for the result value too.

int x = t.findLarger(123, new Double(456));
needed return value: Integer, Byte, Short or Character
x: Integer
y: Double
The common type for x and y is Number. But a Number cannot be converted automatically to an int.

int x = t.findLarger(123, new Integer(456));
needed return value: Integer, Byte, Short or Character
x: Integer
y: Integer
It should be obvious that this work. Integer is used as type, and an Integer can be converted automatically to an int.

int x = (int) t.findLarger(new Double(123), new Double(456));
needed return value: Integer, Byte, Short or Character
x: Double
y: Double
In this case, Double is used as type. But a Double cannot be cast to an int.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi ranchers,

Exactly as Manfred explained.

I think the question is fooling you by thinking, it is about generics in the first place. And then you might simply to look at the assignments and whether a specific cast would be required.

But, Bharat, you should tell us where this question # 181 is from!
Please supply the source of this mock.

And, second, you would please us if you use code tags.
To indent your code properly:

At the posting page, mark the part of your posting that should be indented. Usually that will be your code (or some tabellaric output).

Hit the - Button below.
that's it!

Trick:
You may also use the code tags for intended bad indentations, as you see then frequently in mock exams.

And it would be absolutely great, if you'd remove line numbers if you don't refer to it. Simply to make life of other ranchers a bit easier: Once and again, some rancher wants to copy/paste your test code in his or her system and look what's happening when this or that will be changed. And that would be easier without the line numbers.

As in your example:


I shifted the isolated line numbers 22 and 23 into a main method for clarity.



Yours,
Bu.
 
Did you just should on me? You should read this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic