ProgrammingEasyTCS NQT · Accenture pattern
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer x = 10, s = 0
while (x > 0)
s = s + x
x = x - 3
end while
Print s, x- A. 22 -2
- B. 22 1
- C. 21 -2
- D. 18 -2
Correct answer: A. 22 -2
The loop adds 10, 7, 4, 1 (s = 22); x then becomes −2 and the condition fails. The final x is the first non-positive value, not 1.
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 a = 10, b = 20, c = 30, r if (a > b AND b > c) r = 1 else if (a < b OR c < b)…
- What will be the output of the following pseudocode? Integer Function f(Integer n) if (n == 1) return 1 return f(n / 2) + n // integer…
- What will be the output of the following pseudocode? Integer count = 0 for (i = 2 to 30) Boolean prime = true for (d = 2 while d d <= i)…
- What will be the output of the following pseudocode? Integer a[6] = {1, 2, 3, 4, 5, 6}, n = 6 for (i = 0 to n/2 - 1) swap(a[i], a[n - 1…
- What will be the output of the following pseudocode? // C-style integer arithmetic: / truncates toward zero, mod keeps the dividend's sign…
- What will be the output of the following pseudocode? Integer Function g(Integer n) if (n <= 1) return n return g(n - 1) + 2 g(n - 2) End…