ProgrammingHardTCS NQT · Infosys · Capgemini
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer n = 1, s = 0
Repeat
s = s + n
n = n * 2 + 1
Until s >= 100
Print n, s- A. 127 120
- B. 255 247
- C. 63 119
- D. 63 57
Correct answer: A. 127 120
Each pass adds n to s and then updates n: s runs 1, 4, 11, 26, 57, 120 while n becomes 3, 7, 15, 31, 63, 127. The loop stops when s first reaches 100 or more, after the sixth pass, so n and s are 127 and 120.
Practise more Pseudocode and Code Tracing questionsPseudocode and Code Tracing: notes and key points
More Pseudocode and Code Tracing questions
- What will be the output of the following pseudocode? Integer s = 0 for (each i from 1 to 5) s = s + i i end for Print s
- What will be the output of the following pseudocode? Integer i = 10 while (i > 2) i = i - 3 end while Print i
- What will be the output of the following pseudocode? Integer n = 4096, c = 0 while (n > 0) n = n / 10 c = c + 1 end while Print c
- What will be the output of the following pseudocode? Integer p = 29, q = 6 Print (p mod q) + (p / q)
- What will be the output of the following pseudocode? Integer n = 3140, r = 0 while (n > 0) r = r 10 + n mod 10 n = n / 10 end while Print r
- What will be the output of the following pseudocode? Integer c = 0 for (each i from 1 to 5) for (each j from 1 to 5) if ((i + j) mod 3 ==…