• 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
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

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.
 
passwords must contain 14 characters, a number, punctuation, a small bird, a bit of cheese and a tiny ad.
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic