ProgrammingModerateTCS NQT · Wipro
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer s = 0
for (each i from 1 to 99)
if (i mod 2 == 1)
s = s + i
else
s = s - i
end if
end for
Print s- A. 49
- B. 50
- C. 99
- D. 0
Correct answer: B. 50
Pair the terms: (1 − 2) + (3 − 4) + … + (97 − 98) = 49 × (−1) = −49, plus the last term 99: s = 50.
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? (All variables are integers, so / is integer division) Integer n = 100, steps = 0…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Integer dp[0..10] dp[1] = 0…
- What will be the output of the following pseudocode? (& is bitwise AND on two's-complement integers) Integer c = 0 for (each i from 0 to…
- What will be the output of the following pseudocode? (^ is XOR, & is AND; x & (−x) keeps the lowest 1-bit) Integer a[6] = {2, 4, 7, 9, 2…
- What will be the output of the following pseudocode? String w[3] = {"flower", "flow", "flight"} String p = w[0] for (each i from 1 to 2)…
- What will be the output of the following pseudocode? (substring(s, i, 2) is the 2 characters from index i; sorted(t) puts t's letters in…