Pseudocode questions
Trace loops and conditions (asked by Infosys, Capgemini and Accenture).
1. What will be the output of the following pseudocode?
Integer n = 8922, s = 0
while (n > 0)
s = s + (n mod 10)
n = n / 10 // integer division
end while
Print s- 21
- 17
- 26
- 16
Answer
A. 21 It adds the digits of 8922: 8 + 9 + 2 + 2 = 21.
2. What will be the output of the following pseudocode?
Integer f = 1
for (each i from 2 to 6)
f = f * i
end for
Print f- 576
- 721
- 540
- 720
Answer
D. 720 It computes 6! = 720.
3. What will be the output of the following pseudocode?
Integer r = 1
for (each i from 1 to 4)
r = r * 4
end for
Print r- 256
- 320
- 192
- 205
Answer
A. 256 4 multiplied 4 times = 4^4 = 256.
4. What will be the output of the following pseudocode? (& is bitwise AND, | is bitwise OR)
Integer a = 12, b = 4
Print (a & b) + (a | b)- 12
- 16
- 4
- 17
Answer
B. 16 a & b = 4, a | b = 12; sum 16 (which always equals a + b).
5. What will be the output of the following pseudocode?
Integer s = 0
for (each i from 1 to 8 with step 1)
s = s + i
end for
Print s- 36
- 38
- 35
- 37
Answer
A. 36 Adds i = 1, 2, 3, 4, 5, 6, 7, 8: total 36.
6. What will be the output of the following pseudocode?
Integer x = 0, y = 1, t
repeat 9 times
t = x + y
x = y
y = t
end repeat
Print x- 36
- 34
- 26
- 37
Answer
B. 34 x steps through the Fibonacci numbers; after 9 steps x = 34.
7. What will be the output of the following pseudocode?
Integer arr[5] = {1, 16, 16, 4, 5}
Integer c = 0
for (each i from 0 to 4)
if (arr[i] > 11) c = c + 1
end for
Print c- 2
- 1
- 4
- 3
Answer
A. 2 Values greater than 11: 16, 16 → 2.
8. What will be the output of the following pseudocode?
Integer a = 4, b = 20, t
while (b != 0)
t = b
b = a mod b
a = t
end while
Print a- 4
- 3
- 5
- 2
Answer
A. 4 This is Euclid's algorithm: it prints GCD(4, 20) = 4.
9. What will be the output of the following pseudocode?
Integer v = 27, count = 0
while (v > 0)
v = v - 3
count = count + 1
end while
Print count- 9
- 11
- 7
- 10
Answer
A. 9 Subtracting 3 from 27 until it is not positive takes 9 steps.
10. What will be the output of the following pseudocode?
Integer a = 4, b = 4, c = 1
if (a > b)
if (a > c) Print a else Print c
else
if (b > c) Print b else Print c
end if- 6
- 4
- 3
- 5
Answer
B. 4 It prints the largest of 4, 4, 1: 4.
11. What will be the output of the following pseudocode?
Integer n = 9237, s = 0
while (n > 0)
s = s + (n mod 10)
n = n / 10 // integer division
end while
Print s- 23
- 21
- 20
- 26
Answer
B. 21 It adds the digits of 9237: 9 + 2 + 3 + 7 = 21.
12. What will be the output of the following pseudocode?
Integer f = 1
for (each i from 2 to 4)
f = f * i
end for
Print f- 29
- 18
- 22
- 24
Answer
D. 24 It computes 4! = 24.
13. What will be the output of the following pseudocode?
Integer r = 1
for (each i from 1 to 2)
r = r * 4
end for
Print r- 19
- 18
- 14
- 16
Answer
D. 16 4 multiplied 2 times = 4^2 = 16.
14. What will be the output of the following pseudocode? (& is bitwise AND, | is bitwise OR)
Integer a = 5, b = 2
Print (a & b) + (a | b)- 0
- 7
- 8
- 9
Answer
B. 7 a & b = 0, a | b = 7; sum 7 (which always equals a + b).
15. What will be the output of the following pseudocode?
Integer s = 0
for (each i from 1 to 8 with step 3)
s = s + i
end for
Print s- 12
- 15
- 10
- 14
Answer
A. 12 Adds i = 1, 4, 7: total 12.
16. What will be the output of the following pseudocode?
Integer x = 0, y = 1, t
repeat 6 times
t = x + y
x = y
y = t
end repeat
Print x- 7
- 8
- 9
- 10
Answer
B. 8 x steps through the Fibonacci numbers; after 6 steps x = 8.
17. What will be the output of the following pseudocode?
Integer arr[5] = {8, 1, 11, 12, 14}
Integer c = 0
for (each i from 0 to 4)
if (arr[i] > 6) c = c + 1
end for
Print c- 4
- 6
- 3
- 5
Answer
A. 4 Values greater than 6: 8, 11, 12, 14 → 4.
18. What will be the output of the following pseudocode?
Integer a = 5, b = 28, t
while (b != 0)
t = b
b = a mod b
a = t
end while
Print a- 4
- 1
- 3
- 2
Answer
B. 1 This is Euclid's algorithm: it prints GCD(5, 28) = 1.
19. What will be the output of the following pseudocode?
Integer v = 16, count = 0
while (v > 0)
v = v - 3
count = count + 1
end while
Print count- 8
- 4
- 6
- 5
Answer
C. 6 Subtracting 3 from 16 until it is not positive takes 6 steps.
20. What will be the output of the following pseudocode?
Integer a = 3, b = 4, c = 5
if (a > b)
if (a > c) Print a else Print c
else
if (b > c) Print b else Print c
end if- 6
- 4
- 5
- 3
Answer
C. 5 It prints the largest of 3, 4, 5: 5.
21. What will be the output of the following pseudocode?
Integer n = 230, s = 0
while (n > 0)
s = s + (n mod 10)
n = n / 10 // integer division
end while
Print s- 5
- 6
- 4
- 7
Answer
A. 5 It adds the digits of 230: 2 + 3 + 0 = 5.
22. What will be the output of the following pseudocode?
Integer f = 1
for (each i from 2 to 5)
f = f * i
end for
Print f- 118
- 120
- 90
- 96
Answer
B. 120 It computes 5! = 120.
23. What will be the output of the following pseudocode?
Integer r = 1
for (each i from 1 to 3)
r = r * 4
end for
Print r- 70
- 65
- 63
- 64
Answer
D. 64 4 multiplied 3 times = 4^3 = 64.
24. What will be the output of the following pseudocode? (& is bitwise AND, | is bitwise OR)
Integer a = 14, b = 3
Print (a & b) + (a | b)- 17
- 18
- 2
- 15
Answer
A. 17 a & b = 2, a | b = 15; sum 17 (which always equals a + b).
25. What will be the output of the following pseudocode?
Integer s = 0
for (each i from 1 to 5 with step 2)
s = s + i
end for
Print s- 7
- 11
- 10
- 9
Answer
D. 9 Adds i = 1, 3, 5: total 9.
26. What will be the output of the following pseudocode?
Integer x = 0, y = 1, t
repeat 7 times
t = x + y
x = y
y = t
end repeat
Print x- 14
- 13
- 15
- 16
Answer
B. 13 x steps through the Fibonacci numbers; after 7 steps x = 13.
27. What will be the output of the following pseudocode?
Integer arr[5] = {20, 6, 1, 9, 16}
Integer c = 0
for (each i from 0 to 4)
if (arr[i] > 13) c = c + 1
end for
Print c- 3
- 1
- 4
- 2
Answer
D. 2 Values greater than 13: 20, 16 → 2.
28. What will be the output of the following pseudocode?
Integer a = 7, b = 16, t
while (b != 0)
t = b
b = a mod b
a = t
end while
Print a- 2
- 1
- 4
- 3
Answer
B. 1 This is Euclid's algorithm: it prints GCD(7, 16) = 1.
29. What will be the output of the following pseudocode?
Integer v = 23, count = 0
while (v > 0)
v = v - 6
count = count + 1
end while
Print count- 6
- 2
- 4
- 5
Answer
C. 4 Subtracting 6 from 23 until it is not positive takes 4 steps.
30. What will be the output of the following pseudocode?
Integer a = 4, b = 7, c = 7
if (a > b)
if (a > c) Print a else Print c
else
if (b > c) Print b else Print c
end if- 5
- 6
- 7
- 9
Answer
C. 7 It prints the largest of 4, 7, 7: 7.
31. What will be the output of the following pseudocode?
Integer n = 1163, s = 0
while (n > 0)
s = s + (n mod 10)
n = n / 10 // integer division
end while
Print s- 11
- 14
- 12
- 13
Answer
A. 11 It adds the digits of 1163: 1 + 1 + 6 + 3 = 11.
32. What will be the output of the following pseudocode?
Integer r = 1
for (each i from 1 to 5)
r = r * 3
end for
Print r- 219
- 267
- 243
- 182
Answer
C. 243 3 multiplied 5 times = 3^5 = 243.
33. What will be the output of the following pseudocode? (& is bitwise AND, | is bitwise OR)
Integer a = 13, b = 3
Print (a & b) + (a | b)- 1
- 15
- 16
- 17
Answer
C. 16 a & b = 1, a | b = 15; sum 16 (which always equals a + b).
34. What will be the output of the following pseudocode?
Integer s = 0
for (each i from 1 to 7 with step 3)
s = s + i
end for
Print s- 9
- 10
- 14
- 12
Answer
D. 12 Adds i = 1, 4, 7: total 12.
35. What will be the output of the following pseudocode?
Integer x = 0, y = 1, t
repeat 8 times
t = x + y
x = y
y = t
end repeat
Print x- 26
- 21
- 17
- 22
Answer
B. 21 x steps through the Fibonacci numbers; after 8 steps x = 21.
36. What will be the output of the following pseudocode?
Integer arr[5] = {11, 18, 20, 14, 9}
Integer c = 0
for (each i from 0 to 4)
if (arr[i] > 11) c = c + 1
end for
Print c- 3
- 5
- 1
- 4
Answer
A. 3 Values greater than 11: 18, 20, 14 → 3.
37. What will be the output of the following pseudocode?
Integer a = 8, b = 17, t
while (b != 0)
t = b
b = a mod b
a = t
end while
Print a- 1
- 3
- 2
- 4
Answer
A. 1 This is Euclid's algorithm: it prints GCD(8, 17) = 1.
38. What will be the output of the following pseudocode?
Integer v = 25, count = 0
while (v > 0)
v = v - 2
count = count + 1
end while
Print count- 16
- 11
- 15
- 13
Answer
D. 13 Subtracting 2 from 25 until it is not positive takes 13 steps.
39. What will be the output of the following pseudocode?
Integer n = 8728, s = 0
while (n > 0)
s = s + (n mod 10)
n = n / 10 // integer division
end while
Print s- 23
- 25
- 27
- 20
Answer
B. 25 It adds the digits of 8728: 8 + 7 + 2 + 8 = 25.
40. What will be the output of the following pseudocode?
Integer r = 1
for (each i from 1 to 3)
r = r * 3
end for
Print r- 27
- 26
- 29
- 32
Answer
A. 27 3 multiplied 3 times = 3^3 = 27.
41. What will be the output of the following pseudocode? (& is bitwise AND, | is bitwise OR)
Integer a = 15, b = 2
Print (a & b) + (a | b)- 17
- 2
- 15
- 18
Answer
A. 17 a & b = 2, a | b = 15; sum 17 (which always equals a + b).
42. What will be the output of the following pseudocode?
Integer s = 0
for (each i from 1 to 5 with step 1)
s = s + i
end for
Print s- 12
- 16
- 15
- 18
Answer
C. 15 Adds i = 1, 2, 3, 4, 5: total 15.
43. What will be the output of the following pseudocode?
Integer arr[5] = {2, 4, 17, 19, 3}
Integer c = 0
for (each i from 0 to 4)
if (arr[i] > 14) c = c + 1
end for
Print c- 1
- 2
- 4
- 3
Answer
B. 2 Values greater than 14: 17, 19 → 2.
44. What will be the output of the following pseudocode?
Integer v = 15, count = 0
while (v > 0)
v = v - 5
count = count + 1
end while
Print count- 1
- 3
- 4
- 2
Answer
B. 3 Subtracting 5 from 15 until it is not positive takes 3 steps.
45. What will be the output of the following pseudocode?
Integer n = 1112, s = 0
while (n > 0)
s = s + (n mod 10)
n = n / 10 // integer division
end while
Print s- 5
- 4
- 7
- 3
Answer
A. 5 It adds the digits of 1112: 1 + 1 + 1 + 2 = 5.
46. What will be the output of the following pseudocode?
Integer r = 1
for (each i from 1 to 2)
r = r * 2
end for
Print r- 5
- 3
- 4
- 6
Answer
C. 4 2 multiplied 2 times = 2^2 = 4.
47. What will be the output of the following pseudocode? (& is bitwise AND, | is bitwise OR)
Integer a = 8, b = 2
Print (a & b) + (a | b)- 12
- 10
- 11
- 0
Answer
B. 10 a & b = 0, a | b = 10; sum 10 (which always equals a + b).
48. What will be the output of the following pseudocode?
Integer s = 0
for (each i from 1 to 4 with step 1)
s = s + i
end for
Print s- 10
- 12
- 13
- 9
Answer
A. 10 Adds i = 1, 2, 3, 4: total 10.
49. What will be the output of the following pseudocode?
Integer n = 5851, s = 0
while (n > 0)
s = s + (n mod 10)
n = n / 10 // integer division
end while
Print s- 24
- 19
- 15
- 20
Answer
B. 19 It adds the digits of 5851: 5 + 8 + 5 + 1 = 19.
50. What will be the output of the following pseudocode?
Integer s = 0
for (each i from 1 to 9 with step 3)
s = s + i
end for
Print s- 9
- 11
- 12
- 15
Answer
C. 12 Adds i = 1, 4, 7: total 12.
Mock test
- Pseudocode Mock Test 15 questions · 15 min