Oracle
1Z0-809 · Question #238
Given: class Block { String color; int size; Block(int size, String color) { this.size = size; this.color = color; } } and the code fragment: List<Block> blocks = new ArrayList<>(); blocks.add(new…
The correct answer is B. class ColorSorter implements Comparator<Block> { public int compareTo(Block o1, Block o2) { return o1.color.compareTo(o2.color); } }. You've hit your session limit · resets 5:20pm (America/New_York)
Question
Given:
class Block {
String color;
int size;
Block(int size, String color) {
this.size = size;
this.color = color;
}
}
and the code fragment:
List<Block> blocks = new ArrayList<>();
blocks.add(new Block(10, "Green"));
blocks.add(new Block(7, "Red"));
blocks.add(new Block(12, "Blue"));
Collections.sort(blocks, new ColorSorter());
Which definition of the ColorSorter class sorts the blocks list?
Options
- Aclass ColorSorter implements Comparable<Block> { public boolean compare(Block o1, Block o2) { return o1.color.equals(o2.color); } }
- Bclass ColorSorter implements Comparator<Block> { public int compareTo(Block o1, Block o2) { return o1.color.compareTo(o2.color); } }
- Cclass ColorSorter implements Comparator<Block> { public int compare(Block o1, Block o2) { return o1.color.compareTo(o2.color); } }
- Dclass ColorSorter implements Comparator<Block> { public boolean compare(Block o1, Block o2) { return o1.color.equals(o2.color); } }
How the community answered
(26 responses)- A4% (1)
- B77% (20)
- C4% (1)
- D15% (4)
Explanation
You've hit your session limit · resets 5:20pm (America/New_York)
Community Discussion
No community discussion yet for this question.