ProgrammingVery hardGoldman Sachs OA · TCS Digital
What will be the output of the following pseudocode?
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 > 30)
break
end if
if (not mark[p])
j = p * p
while (j <= 30)
mark[j] = true
ops = ops + 1
j = j + p
end while
end if
end for
Print ops- A. 24
- B. 19
- C. 29
- D. 26
Correct answer: A. 24
Only primes p with p² ≤ 30 sieve: p = 2 marks 4, 6, …, 30 (14 times), p = 3 marks 9, 12, …, 30 (8 times), p = 5 marks 25 and 30 (2 times); p = 4 is skipped as already marked. ops = 24, more than the 19 composites because 12, 18, 24 and 30 are marked more than once.
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; 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…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division; + joins strings) Function…
- What is the time complexity of the following pseudocode, as a function of n? i = n while (i > 1) j = i while (j < n) j = j 2 end while i =…