ProgrammingHardTCS NQT Advanced · Capgemini
What will be the output of the following pseudocode? (i++ gives the old value of i and then adds 1)
What will be the output of the following pseudocode? (i++ gives the old value of i and then adds 1)
Integer i = 0, c = 0
while (i++ < 5)
c = c + i
end while
Print c, i- A. 15 6
- B. 10 5
- C. 15 5
- D. 10 6
Correct answer: A. 15 6
i++ compares the OLD value and then increments. The tests use 0, 1, 2, 3, 4 (true) and 5 (false), so the body sees i = 1 to 5: c = 15. The failing test still increments, leaving i = 6: 15 6.
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? (--n subtracts 1 from n and then gives the new value) Integer n = 5, s = 0 do s = s +…
- 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)…