32.5k views
3 votes
Consider the following code segment.

int j = 1;
while (j <= 5)
{
for (int k = 4; k > 1; k--)
{
System.out.println("ha"); // line 6
}
j++;
}
How many times will the print statement on line 6 execute?
A 15
B 16
C 20
D 24
E 25

User Antwann
by
7.3k points

1 Answer

5 votes

Final answer:

The 'System.out.println("ha");' statement will execute 15 times, because the inner loop runs 3 times inside the outer loop, which runs 5 times, resulting in 15 total prints.

Step-by-step explanation:

The code segment provided mixes conditional loops with iterations. The outer loop is a while loop that will repeat the inner loop as long as the condition j ≤ 5 is true.

The inner loop is a for loop that decrements from 4 to greater than 1. So for each iteration of the inner loop, it will execute 3 times (for values of k: 4, 3, 2).

Since the outer loop runs 5 times (j values: 1, 2, 3, 4, 5), and the inner loop runs 3 times for each outer loop iteration, we can calculate the total executions of the print statement as 5 * 3 = 15 times.

User Andykellr
by
7.6k points