Exam Permalink:
https://www.jobilize.com/java-certification-questions
Question Permalink:
https://www.jobilize.com/how-to-sort-a-generic-collection
Question 173 / 297:  What is the expected output of compiling and running the following code?
import java.util.ArrayList;

import java.util.Collections;

import java.util.List;

public class Test{

public static void main(String[] args) {

List<Foo> myFooList = new ArrayList<Foo>();

myFooList.add(new Foo("C"));

myFooList.add(new Foo("A"));

myFooList.add(new Foo("D"));

Collections.sort(myFooList);

System.out.print(myFooList.get(0).code);

}

}

class Foo implements Comparable<Foo> {

String code;

Foo(String c) {

code = c;

}

int compareTo(Foo f) {

return this.code.compareTo(f.code);

}

}

A  A
B  C
C  D
D  no output is printed
E  compilation error
F  exception thrown at run time
<< First < Previous Next > Last >>
Explanation:

In class Foo when overriding method compareTo() we must keep the access specifier "public" since the overridign rules say: the access specifier for an overriding method can allow more, but not less, access than the overridden method

Exam Home Page
https://www.jobilize.com/java-certification-questions

Oracle Certified Professional Java Programmer

Author:

Access: Public Instant Grading

Ask
Heather McAvoy
Start Quiz
Copy and paste the following HTML code into your website or blog.
<iframe src="https://www.jobilize.com/embed/java-certification-questions" width="600" height="600" frameborder="0" marginwidth="0" marginheight="0" scrolling="yes" style="border:1px solid #CCC; border-width:1px 1px 0; margin-bottom:5px" allowfullscreen webkitallowfullscreen mozallowfullscreen> </iframe>