ProgrammingModerateCapgemini · Accenture
What will be the output of the following pseudocode? (--n subtracts 1 from n and then gives the new value)
What will be the output of the following pseudocode? (--n subtracts 1 from n and then gives the new value)
Integer n = 5, s = 0
do
s = s + n
while (--n > 2)
Print s, n- A. 9 3
- B. 14 2
- C. 12 2
- D. 12 3
Correct answer: C. 12 2
--n decreases n before the comparison. The body adds 5, then n = 4 > 2, adds 4, then n = 3 > 2, adds 3, then n = 2 fails. s = 12 and n = 2.
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? (num[i] - '0' converts a digit character to its value) String num = "79927398713"…
- What will be the output of the following pseudocode? (>> is right shift, & is bitwise AND) Integer bits[0..15], total = 0 bits[0] = 0 for…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer a[6] = {3, 2, 2, 3, 4, 3}, k = 0, val = 3 for…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Integer n = 7, c = 1 for (each…
- What will be the output of the following pseudocode? Integer count = 0 String last = "" Procedure gen(String s, Integer n) if (length(s)…
- What will be the output of the following pseudocode? Integer p[4] = {2, 0, 3, 1} // coefficients of 2x³ + 0x² + 3x + 1 Integer x = 3, m =…