Question 200 / 297:  What is the result of compiling and running the following code?
import java.util.ArrayList;

import java.util.Collections;

import java.util.Comparator;

import java.util.List;

public class Tester{

public static void main(String[] args) {

List<Human> humans = new ArrayList<Human>();

humans.add(new Human(13));

humans.add(new Human(33));

humans.add(new Human(21));

humans.add(new Human(21)); // line 1

HumanComparator c = new HumanComparator(); // line 2

Collections.sort(humans, c); // line 3

System.out.print(humans.get(0).age);

Collections.sort(humans); // line 4

System.out.print(humans.get(0).age);

}

}

class Human implements Comparable<Human> {

Integer age;

public Human(int age) {

this.age = age;

}

public int compareTo(Human h) {

return h.age.compareTo(this.age);

}

}

class HumanComparator implements Comparator<Human> {

public int compare(Human h1, Human h2) {

return h1.age.compareTo(h2.age);

}

}

A  line 1 causes a compilation error (no duplicates are allowed)
B  line 2 causes a compilation error, the correct instantiation for HumanComparator is as so :
new HumanComparator<Human>()
C  line 4 causes a compilation error, must supply a Comparator to methos sort.
D  The program compiles and runs fine printing 1333
E  The program compiles and runs fine printing 3313
<< First < Previous Next > Last >>
Exam Home Page
Ask
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>