Questão generics TestKiller

2 respostas
Gustavo_Santos

Mais uma galera:

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));

Eu marquei C, D pelo fato de ele estar chamando compareTo, e para classificar
nós sabemos que os objetos precisam ser multuamente comparáveis.

A resposta do testKiller é A, C, porém a A passa um Integer e uma String (123, "456") como ele
vai classificar isso? Isso não gera um ClassCastException ?

2 Respostas

C

Observe a pergunta…

Which two will compile without errors when inserted at line 23? (Choose two.)

Wil compile e não Will compile and run without exption

Gustavo_Santos

Ok, entendido …

Criado 13 de setembro de 2010
Ultima resposta 14 de set. de 2010
Respostas 2
Participantes 2