60.5k views
1 vote
Consider the following block of code. What value is returned from solution(5)?

public int solution(int limit)
{
int s = 0;

for (int outside = 1; outside <= limit; outside++)
{
for (int middle = 1; middle <= limit; middle++)
{
for (int inside = 1; inside <= limit; inside++)
{
s++;
}
}
}
return s;
}


a. 25
b. 15
c. 125
d. 64
e. 625

User Asish AP
by
7.1k points

1 Answer

7 votes

Final answer:

The value returned from solution(5) is 125, obtained by incrementing a count in a triple nested loop that each run from 1 to the limit, making five iterations for each loop.

Step-by-step explanation:

The value returned from solution(5) when the input is 5 is calculated by a nested loop structure where each loop runs from 1 to the limit, which in this case is 5. The variable s is incremented for every iteration of the innermost loop, which runs a total of limit × limit × limit times, also known as limit to the power of 3. So, the value of s after the execution of the nested loops would be 5³, which is 125.

Therefore, the correct answer is c. 125.

User LazyCubicleMonkey
by
8.0k points