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
  1. 21
  2. 17
  3. 26
  4. 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
  1. 576
  2. 721
  3. 540
  4. 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
  1. 256
  2. 320
  3. 192
  4. 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)
  1. 12
  2. 16
  3. 4
  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
  1. 36
  2. 38
  3. 35
  4. 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
  1. 36
  2. 34
  3. 26
  4. 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
  1. 2
  2. 1
  3. 4
  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
  1. 4
  2. 3
  3. 5
  4. 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
  1. 9
  2. 11
  3. 7
  4. 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
  1. 6
  2. 4
  3. 3
  4. 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
  1. 23
  2. 21
  3. 20
  4. 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
  1. 29
  2. 18
  3. 22
  4. 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
  1. 19
  2. 18
  3. 14
  4. 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)
  1. 0
  2. 7
  3. 8
  4. 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
  1. 12
  2. 15
  3. 10
  4. 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
  1. 7
  2. 8
  3. 9
  4. 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
  1. 4
  2. 6
  3. 3
  4. 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
  1. 4
  2. 1
  3. 3
  4. 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
  1. 8
  2. 4
  3. 6
  4. 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
  1. 6
  2. 4
  3. 5
  4. 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
  1. 5
  2. 6
  3. 4
  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
  1. 118
  2. 120
  3. 90
  4. 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
  1. 70
  2. 65
  3. 63
  4. 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)
  1. 17
  2. 18
  3. 2
  4. 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
  1. 7
  2. 11
  3. 10
  4. 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
  1. 14
  2. 13
  3. 15
  4. 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
  1. 3
  2. 1
  3. 4
  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
  1. 2
  2. 1
  3. 4
  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
  1. 6
  2. 2
  3. 4
  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
  1. 5
  2. 6
  3. 7
  4. 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
  1. 11
  2. 14
  3. 12
  4. 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
  1. 219
  2. 267
  3. 243
  4. 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. 1
  2. 15
  3. 16
  4. 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
  1. 9
  2. 10
  3. 14
  4. 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
  1. 26
  2. 21
  3. 17
  4. 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
  1. 3
  2. 5
  3. 1
  4. 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. 1
  2. 3
  3. 2
  4. 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
  1. 16
  2. 11
  3. 15
  4. 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
  1. 23
  2. 25
  3. 27
  4. 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
  1. 27
  2. 26
  3. 29
  4. 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)
  1. 17
  2. 2
  3. 15
  4. 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
  1. 12
  2. 16
  3. 15
  4. 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. 1
  2. 2
  3. 4
  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. 1
  2. 3
  3. 4
  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
  1. 5
  2. 4
  3. 7
  4. 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
  1. 5
  2. 3
  3. 4
  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)
  1. 12
  2. 10
  3. 11
  4. 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
  1. 10
  2. 12
  3. 13
  4. 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
  1. 24
  2. 19
  3. 15
  4. 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
  1. 9
  2. 11
  3. 12
  4. 15
Answer

C. 12 Adds i = 1, 4, 7: total 12.

Mock test

More Technical MCQs topics