ProgrammingModerateAccenture · Cognizant pattern
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer x = 15, y = 4, r = 0
while (x >= y)
x = x - y
r = r + 1
end while
Print r, x- A. 3 4
- B. 3 3
- C. 4 -1
- D. 3 0
Correct answer: B. 3 3
Repeated subtraction computes quotient and remainder: 15 − 4 − 4 − 4 = 3, after 3 subtractions. So r = 15 / 4 = 3 and x = 15 mod 4 = 3. Using x > y would behave the same here but fail for x = 16 (remainder 4).
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 = 1, b = 2, c = 3 a = (a < b) ? b : c b = (b < c AND a == 2) ? c : a c = (a…
- What will be the output of the following pseudocode? Integer Function h(Integer n) if (n <= 0) return 1 if (n mod 2 == 1) return h(n - 1)…
- What will be the output of the following pseudocode? Integer v = 0 for (i = 1 to 5) v = (v << 1) | (i & 1) end for Print v
- What will be the output of the following pseudocode? Integer a[] = {10, 20, 30, 40} Integer i = 0, s = 0 while (i < 4) s = s + a[i] i = i…
- What will be the output of the following pseudocode? (memo starts with every entry equal to −1; calls counts every call, including the…
- What will be the output of the following pseudocode? (calls counts every call, including the first.) Integer calls = 0 Function g(Integer…