Choosing how to earn college credit? CLEP vs AP guide
| preferred AP College board partner for AP classes
AP Computer Science A/Unit 4: Data Collections
Start Practice TestPractice Test
About Exam
easy

Array Iteration to Find Maximum

Mark
< Prev
Next >

Consider the following Storage class definition.

public class Storage {
  private int[] items;

  public Storage(int size) {
    items = new int[size];
  }
}

The following code segment is intended to find the maximum value in the items array of a Storage object. Assume the array has been populated with values.

Storage store = new Storage(10); // assume values are already populated
int maxItem = store.items[0];
for (int item : store.items) {
  /* missing code */
}

Which of the following can be substituted for /* missing code */ so that the code segment works as intended?

A

if (store.items[item] > maxItem) {
maxItem = store.items[item];
}

B

if (item > store.maxItem) {
store.maxItem = item;
}

C

if (item > maxItem) {
maxItem = item;
}

D

if (store.items[maxItem] < item) {
maxItem = item;
}

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

Question Leaderboard

Not enough data yet to show leaderboard.

No comments yet. Be the first to comment!

AI Tutor

How can I help?

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