Counting Positive Array Elements
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++;
Question Leaderboard
Not enough data yet to show leaderboard.
