ProgrammingVery hardProduct company OA
What will be the output of the following pseudocode? (isComposite[] starts all false; ops counts every assignment inside the inner loop, even to an entry that is already true.)
What will be the output of the following pseudocode? (isComposite[] starts all false; ops counts every assignment inside the inner loop, even to an entry that is already true.)
Integer ops = 0, primes = 0
for (i = 2 to 50)
if (not isComposite[i])
primes = primes + 1
for (j = i * i; j <= 50; j = j + i)
isComposite[j] = true
ops = ops + 1
end for
end if
end for
Print primes, ops- A. 15 45
- B. 15 34
- C. 25 45
- D. 15 49
Correct answer: A. 15 45
There are 15 primes up to 50. Inner-loop work happens only for primes with p² ≤ 50: p = 2 marks 4, 6, …, 50 (24 times), p = 3 marks 9, 12, …, 48 (14), p = 5 marks 25, 30, …, 50 (6), p = 7 marks 49 (1): 24 + 14 + 6 + 1 = 45. That exceeds the 34 composites in 2..50 because numbers like 12 or 30 are crossed out by more than one prime. 25 is the number of primes below 100.
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? (&& stops as soon as the left side is false) Integer c = 0, k = 0 Function f(Integer…
- What will be the output of the following pseudocode? (All variables are integers, so / is integer division) Procedure show(Integer n) if…
- What will be the output of the following pseudocode? (| is bitwise OR) Integer x = 0, c = 0 while (x < 1000) x = x | (x + 1) c = c + 1 end…
- What will be the output of the following pseudocode? (Arrays are indexed from 0) Integer arr[7] = {1, 2, 3, 4, 5, 6, 7} reverse(arr, 0, 2)…
- What will be the output of the following pseudocode? (the static s keeps its value between calls; in f(n - 1) + s the call is evaluated…
- What will be the output of the following pseudocode? Integer c = 0 for (each i from 1 to 6) for (each j from 1 to 6) if ((i j) mod 4 == 0)…