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

Enhanced For-Loop Array Summation

Mark
< Prev
Next >

Consider the following NumberAnalyzer class.

public class NumberAnalyzer {
  private int[] nums;

  public int analyzeThis() {
    int sum = 0;
    for (int num : nums) {
      if (num % 2 == 0) {
        sum += num;
      }
    }
    return sum;
  }
}

What value is returned by a call to the analyzeThis method if the nums instance variable is initialized with the array {2, 5, 8, 11}?

A

The loop increments the sum by the array values that are divisible by 2: sum accumulates to 10.

B

The method returns a runtime error since the array includes values that are not integers.

C

The loop exits prematurely and returns a sum of 2 given an array index out of bounds at the first non-even number.

D

The loop includes all numbers, even and odd, making the sum 26.

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|