ProgrammingVery hardTCS NQT Advanced · Capgemini
What will be the output of the following pseudocode? (C-style for loop: the update i = i + 1 runs after each pass of the body)
What will be the output of the following pseudocode? (C-style for loop: the update i = i + 1 runs after each pass of the body)
Integer c = 0, i
for (i = 0; i < 10; i = i + 1)
if (i mod 3 == 0)
i = i + 2
end if
c = c + i
end for
Print c, i- A. 45 10
- B. 18 10
- C. 26 12
- D. 26 11
Correct answer: C. 26 12
The body changes i and then the update adds 1 more. i goes 0 → 2, 3 → 5, 6 → 8, 9 → 11, so c = 2 + 5 + 8 + 11 = 26; the final update makes i = 12, which ends the loop. Output 26 12.
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? (Strings are indexed from 0) String s = "110100" Integer v = 0, p = 1 for (each i…
- What will be the output of the following pseudocode? Boolean mark[2..30] = all false Integer ops = 0, j for (each p from 2 to 30) if (p p…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division; arrays are indexed from 0)…
- What will be the output of the following pseudocode? Stack s1 = empty, s2 = empty Procedure enqueue(Integer x) push(s1, x) End Procedure…
- What will be the output of the following pseudocode? (^ is bitwise XOR) Integer s = 0 for (each i from 0 to 15) s = s + (i ^ 5) end for…
- What will be the output of the following pseudocode? (parameters are passed by value; inside g, x is the global x; operands are evaluated…