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

Conditional Summation of Even Numbers

Mark
< Prev
Next >

Consider the following RecordProcessor class.

public class RecordProcessor {
  private int[] records;

  public RecordProcessor(int[] initialRecords) {
    records = initialRecords;
  }

  public void calculateSum() {
    int sum = 0;
    for (int val : records) {
      if (/* condition missing */) {
        sum += val;
      }
    }
    System.out.println("Sum: " + sum);
  }
}

Which of the following boolean expressions, when substituted for /* condition missing */, will cause the calculateSum method to compute the sum of only the even numbers in the records array?

A

val != records[0]

B

val <= 0

C

val % 2 == 0

D

val > 5

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|