|
SCJP 5.0 Mock Exam 4 - Q1
Exam Objective: 6.5 Use capabilities in the java.util package to write code to manipulate a list by sorting, performing a binary search, or converting the list to an array. Use capabilities in the java.util package to write code to manipulate an array by sorting, performing a binary search, or converting the array to a list. Use the java.util.Comparator and java.lang.Comparable interfaces to affect the sorting of lists and arrays. Furthermore, recognize the effect of the "natural ordering" of primitive wrapper classes and java.lang. String on sorting.
Q1)
Given the following code:
import java.util.*;
public class TestLaptop {
public static void main (String args[]) {
ArrayList list = new ArrayList();
list.add (new Laptop ("ToshibaA10", 4560.99));
list.add (new Laptop ("DellNU6", 8213.99));
list.add (new Laptop ("IBM", 1298.99));
Collections.sort(list);
System.out.println (list);
}
}
Which line of code, when replaced with '// INSERT CODE HERE' , will enable the class TestLaptop to print out the sorted collection of class 'Laptop'.
// INSERT CODE HERE
String model;
double price;
Laptop(String model, double price) {
this.model = model;
this.price = price;
}
public String getModel() {
return model;
}
public int compareTo(Laptop otherLaptop) {
double otherLaptopPrice = otherLaptop.getPrice();
if (price < otherLaptopPrice) return -1;
else if (price > otherLaptopPrice) return 1;
else return 0;
}
public double getPrice() {
return price;
}
public String toString() {
return "\n" + model + " " + price;
}
}
- A) class Laptop implements Comparable <Laptop> {
- B) class Laptop implements Comparable {
- C) class Laptop implements Comparator {
- D) class Laptop implements Comparator <Laptop>{
- E) None of the above
Answer to Q1
>>>> Go to Q2 >>>>
Submit your feedback on this mock exam
|