ProgrammingHardTCS NQT · Infosys SP
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Boolean comp[0..30] = all false
Integer s = 0
for (each i from 2 to 30)
if (not comp[i])
s = s + i
for (each j from i * i to 30 step i)
comp[j] = true
end for
end if
end for
Print s- A. 100
- B. 160
- C. 128
- D. 129
Correct answer: D. 129
The sieve leaves exactly the primes up to 30 unmarked: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, whose sum is 129.
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? Function isPrime(Integer n) if (n < 2) return false for (each d from 2 to n - 1) if…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Function phi(Integer n)…
- 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…
- 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…