?

AP® Computer Science A

Free Version

Upgrade subject to access all content

Moderate

While Loop Comparing Sum to Max

APCSA-PT1GVP

Consider the following method.

public static int sumUntilGreaterThan(int max)
{
     int sum=0, i=1;
     while(sum<=max)
     {
         sum+=i;
         i++;
     }
     return sum;
}

When calling sumUntilGreaterThan(12), how many times would it execute the body of the while loop?

A

4

B

5

C

6

D

7

E

12