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

Counting Positive Array Elements

Mark
< Prev
Next >

Consider the following DataSet class definition and a code segment designed to count the number of positive values in the data instance variable.

public class DataSet {
  private double[] data;

  public DataSet(double[] initialData) {
    data = initialData;
  }
}

DataSet dataSet = new DataSet(new double[]{-1.5, 2.3, 5.6, -4.0, 3.3});
int countPositives = 0;
for (double num : dataSet.data) {
  /* missing code */
}

Which of the following should replace /* missing code */ for the code segment to work as intended?

A

if (double > 0) {
countPositives++;
}

B

countPositives += num > 0 ? 1 : 0;

C

countPositives = num > 0 ? countPositives + 1 : countPositives;

D

if (num > 0) countPositives++;

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|