Consider the following code segment:

int counter = 5;
int sum = 0;
while(counter < 10)
{
sum+= counter;
counter+=2;
}

Which of the following for loops would return the same value if System.out.println(sum) were printed?

I.

int sum = 0;
for(int i = 0; i < 3; i++)
{
sum += i;
}

II.

int sum = 11;
for(int i = 0; i < 5; i ++)
{
sum+=i;
}

III.

int sum = 0;
for(int i = 5; i < 10; i+=2)
{
sum +=i;
}

IV.

int counter = 5;
int sum = 0;
for(int i = counter; i < 10; i+=2)
{
sum+= counter;
}

III only


II, III and IV


II only


II and III


III and I

Respuesta :

Code segments are simply extracted codes from a more complete program, and they may or may not run without errors.

The for loops that would return the same value as the code segment are II and II

The given code segment would return 21 as the value of sum, because it calculates the sum of odd numbers between 5 and 10 (inclusive)

Code segments II and II would return 21, when the for loop is implemented.

This is so because:

Code segment II calculates the sum of integers from 0 to 4, and the result is added to 11. i.e. 0 + 1 + 2 + 3 + 4 + 11, which is 21

Similarly, code segment III calculates the sum of odd numbers between 5 and 10 (inclusive)

Other code segments would return a different value

Read more about code segments at:

https://brainly.com/question/20475581