What is the result?

Given:

What is the result?

Given:

What is the result?

A.
peach
orange
apple

B.
peach
orange

C.
apple
orange

D.
The program does not compile.

E.
The program generates an exception at runtime.

Explanation:

int compare(T obj1, T obj2)
0 if equal
positive if obj1 greater
negative if obj2 greater
The compiler has a problem with the line:
public boolean compare(String s1, String s2) {
return s1.length() > s2.length();

error: <anonymous comparetest.CompareTest$1> is not abstract and does not override abstract
method
compare(String,String) in Comparator
new Comparator<String>() {
Error: compare(String,String) in <anonymous comparetest.CompareTest$1> cannot implement
compare(T,T)
in Comparator
public boolean compare(String s1, String s2) {
return type boolean is not compatible with int
where T is a type-variable:
T extends Object declared in interface Comparator



Leave a Reply 4

Your email address will not be published. Required fields are marked *


jalex

jalex

The answer is D.

peter

peter

compare(T, T) returns int instead of boolean

gelete

gelete

D

line:
public boolean compare(String s1, String s2) { return s1.length() > s2.length(); }

Compiler:
implements java.util.Comparator.compare
The return type is incompatible with Comparator.compare(String, String)