Are you taking CLEP classes? Try clep.ai
| preferred AP College board partner for AP classes
medium Solved by 3 students

Java Array Traversal Logic

< Prev
Next >

The following method is intended to calculate the total price of all items in a shopping cart, which is represented by an array of Item objects.

/** Returns the total price of all items in the cart.
 *  Precondition: The cart array has been initialized.
 */
public static double totalPrice(Item[] cart) {
    /* method body */
}

Which of the following code segments, when substituted for /* method body */, will cause the method to function correctly?

I.

double total = 0;
for (int i = 0; i <= cart.length; i++)
    total += cart[i].getPrice();
return total;

II.

double total = 0;
for (Item item : cart)
    total += item.getPrice();
return total;

III.

double total = cart[0].getPrice();
for (int i = 1; i < cart.length; i++)
    total += cart[i].getPrice();
return total;
A

II and III only

B

II only

C

I only

D

I and III only

Hint
Did You Know?
Explain Why
Explain All Answers
Check Answer
Show Correct Answer
Report Question
Mark for review

Question Leaderboard

Rank
User
Correct Count
Attempt Count
Total Time
Score
#1kaisuki12 0m 00s 90
#2singhris00011 1m 43s -3
#3bommasam00003 6m 22s -412
#4y.seong202714 34m 13s -1,983
Items per page:
10
1 – 4 of 4
No comments yet. Be the first to comment!

AI Tutor

How can I help?

APFIVE © 2020.
Email: apfive@apfive.org|Privacy Policy|FAQ