| preferred AP College board partner for AP classes
AP Computer Science A/Unit 4: Iteration
Start Practice TestPractice Test
About Exam
easy Solved by 9 students
Java Enhanced For Loop Summation
< Prev
Next >

Consider the following NumberAnalyzer class definition.

public class NumberAnalyzer {
  private int[] nums;

  public int analyzeThis() {
    int sum = 0;
    for (int num : nums) {
      if (num % 2 == 0) {
        sum += num;
      }
    }
    return sum;
  }
  // Assume the constructor and other methods are defined elsewhere.
}

What is returned by a call to the analyzeThis method on a NumberAnalyzer object whose nums instance variable is initialized to {2, 5, 8, 11}?

A

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

B

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

C

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

D

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

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

Question Leaderboard

Rank
User
Correct Count
Attempt Count
Time
Score
#1lsj0803092211 0m 00s 100
#2hitrishabhatia12 0m 00s 90
#3Pelisx11 0m 36s 64
#4suhanakochhar00611 1m 26s 14
#5y.seong202723 3m 50s -40
#6gtsak3111 3m 33s -113
#7midnightasdark11 20m 44s -1,144
Items per page:
10
1 – 7 of 7
No comments yet. Be the first to comment!

AI Tutor

How can I help?

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