MCQ Single Best Answer Moderate

QHow many times is the inner loop executed?

for (k = 1; k <= 2; k++) { 
    for (m = 1; m <= 4; m++) { 
        System.out.println(m * 2);
    } 
}

ID: #23877 ICSE Computer Application - Class X - Selection Examination - 2024-25 - MCQ - CALCUTTA PUBLIC SCHOOL 72 views
Question Info
#23877Q ID
ModerateDifficulty
ICSE Computer Application - Class X - Selection Examination - 2024-25 - MCQ - CALCUTTA PUBLIC SCHOOLTopic

Choose the Best Option

Click any option to instantly check if you're correct.

  • A 4 times
  • B 2 times
  • C 8 times
  • D 16 times
Correct Answer: Option C

Explanation

for (k = 1; k <= 2; k++) { 
    for (m = 1; m <= 4; m++) { 
        System.out.println(m * 2);
    } 
}

The outer loop runs from k = 1 to k = 2.

So, the outer loop executes 2 times.

The inner loop runs from m = 1 to m = 4.

So, the inner loop executes 4 times for each outer loop execution.

Therefore:

2 × 4 = 8

Answer: The inner loop is executed 8 times in total.

Output

2
4
6
8
2
4
6
8

Simple Visualization

k = 1
   m = 1
   m = 2
   m = 3
   m = 4

k = 2
   m = 1
   m = 2
   m = 3
   m = 4

Share This Question

Challenge a friend or share with your study group.