Sample questions
Question 1Easy
What is the output of the following pseudocode?
Integer i, j, c
Set c = 0
for (each i from 1 to 6)
for (each j from 1 to 6)
if (i * j < 6)
c = c + 1
End if
End for
End for
Print c
- 14
- 11
- 10
- 5
Answer: C. 10
Explanation
c counts ordered pairs (i, j) with i * j < 6: 10.
Step-by-step solution
- C counts the pairs (i, j) with i and j from 1 to 6 where i × j is less than 6.
- For i = 1, j can be 1 to 5, which is 5 pairs. For i = 2, j is 1 or 2, which is 2 pairs. For i = 3, j is 1, which is 1 pair.
- For i = 4 and i = 5, j = 1 gives 1 pair each, and i = 6 gives none. The total is 5 + 2 + 1 + 1 + 1 = 10: option C.
Why the other options are wrong
- A. 14 comes from testing i * j <= 6 instead of i * j < 6, which also counts the products equal to 6.
- B. 11 is one more than the correct total of 10.
- D. 5 counts only the pairs where i is 1.
Tip: For a nested loop with a product test, count row by row: for each i, count the j values that pass.
Question 2Easy
What is the output of the following pseudocode?
Integer i, j, c
Set c = 0
for (each i from 1 to 4)
for (each j from 1 to 7)
c = c + 1
End for
End for
Print c
- 28
- 35
- 29
- 21
Answer: A. 28
Explanation
The inner body runs 4 * 7 times: 28.
Step-by-step solution
- The outer loop runs 4 times, and the inner loop runs 7 times on each outer pass.
- C adds 1 on each inner pass, so c is 4 × 7.
- The output is 28: option A.
Why the other options are wrong
- B. 35 is 5 × 7; the outer loop runs only 4 times.
- C. 29 is one more than 28.
- D. 21 is 3 × 7; the outer loop has 4 passes, not 3.
Tip: Nested loops multiply: the inner count is repeated once for each outer pass.
Question 3Easy
Predict the output of the following pseudocode.
Integer i, j, c
Set c = 0
for (each i from 1 to 10)
for (each j from 1 to 10)
if (i * j < 10)
c = c + 1
End if
End for
End for
Print c
- 23
- 24
- 14
- 27
Answer: A. 23
Explanation
c counts ordered pairs (i, j) with i * j < 10: 23.
Step-by-step solution
- C counts the pairs (i, j) from 1 to 10 where i × j is less than 10.
- i = 1 gives 9 pairs, i = 2 gives 4, i = 3 gives 3, i = 4 gives 2 and i = 5 gives 1.
- i = 6, 7, 8 and 9 give 1 pair each, and i = 10 gives none. The total is 23: option A.
Why the other options are wrong
- B. 24 is one too many; i = 10 gives no pair, because 10 × 1 is not less than 10.
- C. 14 misses many pairs; the row counts add up to 23.
- D. 27 is too many; the correct count is 23.
Tip: For a nested loop with a product test, count row by row: for each i, count the j values that pass.
Question 4Easy
What will be the output of the following pseudocode?
Integer i, j, c
Set c = 0
for (each i from 1 to 6)
for (each j from i to 6)
c = c + 1
End for
End for
Print c
- 15
- 20
- 22
- 21
Answer: D. 21
Explanation
For each i the inner loop runs 6 - i + 1 times, so c = 6 + ... + 1 = 21.
Step-by-step solution
- I starts at 1, j runs from 1 to 6: c = 0 + 6 = 6.
- i = 2, j runs from 2 to 6: c = 6 + 5 = 11.
- i = 3, j runs from 3 to 6: c = 11 + 4 = 15.
- i = 4, j runs from 4 to 6: c = 15 + 3 = 18.
- i = 5, j runs from 5 to 6: c = 18 + 2 = 20.
- i = 6, j runs from 6 to 6: c = 20 + 1 = 21.
Why the other options are wrong
- A. 15 is the count after i equals 3, so the loop has stopped early.
- B. 20 misses the last pair where i and j both equal 6.
- C. 22 is one too many; there are 21 pairs with i not greater than j from 1 to 6.
Tip: Sum of first n natural numbers = n(n+1)/2.
Question 5Medium
What is the output of the following pseudocode?
Integer i, j, c
Set c = 0
for (each i from 1 to 6)
for (each j from 1 to 6)
if (i + j == 7)
c = c + 1
End if
End for
End for
Print c
- 6
- 7
- 8
- 5
Answer: A. 6
Explanation
c counts ordered pairs (i, j) from 1..6 with i + j = 7 (like two dice): 6.
Step-by-step solution
- The loop checks every pair (i, j) with i and j from 1 to 6.
- i + j = 7 holds for (1, 6), (2, 5), (3, 4), (4, 3), (5, 2) and (6, 1).
- That is 6 pairs, so c = 6, the correct answer.
Why the other options are wrong
- B. 7 is wrong; the list has 6 pairs, not 7.
- C. 8 is wrong; there are only 6 pairs.
- D. 5 is wrong; the list has 6 pairs.
Tip: For pairs that add to a fixed sum, list the first values one by one and check that the second value stays in range.
Question 6Medium
Predict the output of the following pseudocode.
Integer i, j, c
Set c = 0
for (each i from 1 to 8)
for (each j from i + 1 to 8)
if ((i + j) mod 4 == 0)
c = c + 1
End if
End for
End for
Print c
- 1
- 6
- 4
- 7
Answer: B. 6
Explanation
c counts pairs i < j from 1..8 whose sum is a multiple of 4: 6.
Step-by-step solution
- List pairs i<j in 1..8 with i+j a multiple of 4.
- Sum 4: (1,3). Sum 8: (1,7),(2,6),(3,5). Sum 12: (4,8),(5,7).
- Sum 16 would need (8,8), but j starts at i+1, so none.
- Total c = 1+3+2 = 6.
Why the other options are wrong
- A. Counts only one sum value.
- C. Misses some pairs with sum 8 or 12.
- D. Counts one pair too many, probably (8,8) which is excluded.
Tip: Group by target sum (4, 8, 12) to avoid missing pairs.
Question 7Medium
What is the output of the following pseudocode?
Integer i, j, s
Set s = 0
for (each i from 1 to 5)
for (each j from 1 to i)
if ((i + j) mod 2 == 0)
s = s + 1
End if
End for
End for
Print s
- 12
- 9
- 0
- 6
Answer: B. 9
Explanation
s counts pairs 1 <= j <= i <= 5 with i + j even, i.e. i and j of the same parity: 9.
Step-by-step solution
- S starts at 0.
- For i = 1 to 5, j runs from 1 to i.
- If (i + j) is even, s increases by 1.
- Total pairs with same parity: 9.
- Final value of s is 9.
Why the other options are wrong
- A. 0 is wrong; pairs with an even sum are counted, and there are several of them.
- C. 12 is too many; only 9 of the 15 pairs have an even sum.
- D. 6 counts the pairs with an odd sum, but the code adds only when the sum is even.
Tip: Count same parity pairs for i and j in range 1 to 5.
Question 8Medium
What is the output of the following pseudocode?
Integer i, j, s
Set s = 0
for (each i from 1 to 4)
for (each j from 1 to 4)
if (j > i)
break
End if
s = s + j
End for
End for
Print s
- 30
- 20
- 19
- 10
Answer: B. 20
Explanation
For each i the inner loop adds 1 + 2 + ... + i before breaking, so s = sum of i(i + 1)/2 for i = 1..4
= 20.
Step-by-step solution
- Inner loop adds
j up to i. i=1: 1;
i=2: 3;
i=3: 6;
i=4: 10.- Total = 20.
Why the other options are wrong
- A. 30 is not the result; ignoring the break would give 40.
- C. 19 is off by one.
- D. 10 is only the last row.
Question 9Hard
What will be the output of the following pseudocode?
Integer i, j, c
Set c = 0
for (each i from 1 to 7)
for (each j from 1 to 7)
if (i mod j == 0)
c = c + 1
break
End if
End for
End for
Print c
- 7
- 6
- 9
- 8
Answer: A. 7
Explanation
j = 1 always divides i, so the inner loop breaks immediately each time and c = 7.
Step-by-step solution
- For each i from 1 to 7, the inner loop starts at j = 1.
- I mod 1 is always 0, so the if test is true at j = 1 for every i.
- Each i adds 1 to c once and then break leaves the inner loop.
- So c = 7 after 7 outer loops. Print c gives 7, option A.
Why the other options are wrong
- B. 6 would need one i with no divisor found, but every i has j = 1 as a divisor.
- C. 9 is more than the 7 outer loop values, and each i adds at most 1.
- D. 8 is more than the 7 outer loop values, and each i adds at most 1.
Tip: Any number is divisible by 1, so a break-on-divisor loop always finds one at j = 1.
Question 10Hard
What will be printed by the following pseudocode?
Integer i, j, c
Set c = 0
for (each i from 1 to 12)
for (each j from 1 to 12)
if (i mod j == 0)
c = c + 1
break
End if
End for
End for
Print c
- 11
- 13
- 12
- 14
Answer: C. 12
Explanation
j = 1 always divides i, so the inner loop breaks immediately each time and c = 12.
Step-by-step solution
- For each i, the inner loop stops at the first j that divides i.
- j=1 always divides i, so the loop breaks at once.
- Each of 12 values of i adds 1, total 12.
Why the other options are wrong
- A. 11 skips one i.
- B. 13 adds an extra count.
- D. 14 ignores the break.
Practise all 44 questions free
The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.
Open this topic in the app →