Skipping the diagonal — Pseudocode Aptitude Question
Skipping the diagonal: What is printed? Answer and worked solution on CodeKairo.
- Topic: Pseudocode
- Section: Programming MCQs
- Difficulty: Easy
- Time target: 75 seconds
Question
What is printed?
Integer i, j, k
Set k = 0
For i = 1 to 3
For j = 1 to 3
If i == j Then
Continue
End If
Set k = k + 1
End For
End For
Print kOptions
- 3
- 6
- 9
- 12
Show the answer and the worked solution
Correct answer: B. 6
Worked solution
- The nested loops generate 3 × 3 = 9 pairs (i, j).
- Continue skips the increment whenever i equals j, which happens for (1,1), (2,2) and (3,3) — 3 pairs.
- The counter therefore advances 9 - 3 = 6 times and 6 is printed.
Answer: 6.
The idea
Continue skips the rest of the current iteration but not the loop itself, so counting the total and subtracting the skipped cases is the quickest route.
Shortcut. The counter moves once for each ordered pair of different values from {1, 2, 3}: 3 × 2 = 6.
Common trap. 9 reads Continue as "carry on to the next line", so the increment would run for all 3 × 3 pairs; in fact it skips the rest of that pass.