Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider the following declarations. public class Book { private String myTitle;

ID: 3643385 • Letter: C

Question

Consider the following declarations.

public class Book
{
private String myTitle;
private int myNumPages;

public int numPages()
{
return myNumPages;
}
// constructors and other methods not shown
}

ArrayList<Book> readingList;

Assume that readingList has had several books added to it. Consider the following code segments.

I. int sum = 0;
for(Book bk: readingList)
sum += bk.numPages();

II. int sum = 0;
for(int k = 0; k < readingList.size(); k++)
sum += readingList.get(k).numPages();

III. int sum = 0;
int k = 0;
while(k < readingList.size())
{
sum += readingList.get(k).numPages();
k++;
}

Which of these code segments correctly calculates sum to be the total number of pages of books in readingList?

A.) II only
B.) I and II only
C.) I and III only
D.) II and III only
E.) I, II, and III

Explanation / Answer

E) I, II, and III