(SCORED) A+
Consider the following class definition
public class Book { private double cost; public double getCost() { return cost; }
// There may be instance variables, constructors, and methods not shown. }
The following method appears in a class other than Book. It is intended to sum
all the cost instance variables of the Book objects in its ArrayList parameter.
/** Precondition: bookList is not null */ public static double
getTotal(ArrayList<Book> bookList) { double total = 0.0; /* missing code */
return total; }
Which of the following can replace /* missing code */ so the getTotal method
works as intended?
I.
for (int x = 0; x < bookList.size(); x++) { total += bookList.get(x).getCost(); }
II.
for (Book b : bookList) { total += b.getCost(); }
III.
for (Book b : bookList) { total += getCost(b); }
A. I only
B. II only
C. III only
D. I and II
E. I and III Answer - I and II
, Consider the following code segment.
ArrayList<Integer> data = new ArrayList<Integer>();
data.add(4);
data.add(5);
data.add(1, 3);
data.add(1, 6);
What is stored in data after the code segment is run?
A. [4, 5, 3, 6]
B. [6, 3, 5, 4]
C. [4, 3, 4]
D. [4, 6]
E. [4, 6, 3, 5] Answer - [4, 6, 3, 5]
Consider the following code segment.
ArrayList<Double> speedRating = new ArrayList<Double>();
speedRating.add(8.94); speedRating.add(7.33); speedRating.add(6.56);
speedRating.add(4.24); speedRating.set(0, 2.14); speedRating.set(2, 7.63);
System.out.println(speedRating);
What is printed when this code segment is executed?
A. [8.94, 7.63, 4.24]
B. [8.94, 7.33, 6.56, 4.24]
C. [2.14, 7.33, 7.63, 4.24]
D. [8.94, 2.14, 7.63, 6.56]
E. [2.14, 7.63, 6.56, 4.24] Answer - [2.14, 7.33, 7.63, 4.24]
Consider the following code segment.