ProgrammingModerateAccenture · Cognizant pattern
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer count = 0
for (i = 2 to 30)
Boolean prime = true
for (d = 2 while d * d <= i)
if (i mod d == 0) prime = false
end for
if (prime) count = count + 1
end for
Print count- A. 11
- B. 13
- C. 10
- D. 0
Correct answer: C. 10
Trial division up to √i finds the primes from 2 to 30: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, ten of them. Using d * d < i misses the prime squares 4, 9, 25 (13), starting i at 1 would count 1 (11), and starting d at 1 makes every number composite (0).
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? Integer a[6] = {1, 2, 3, 4, 5, 6}, n = 6 for (i = 0 to n/2 - 1) swap(a[i], a[n - 1…
- What will be the output of the following pseudocode? // C-style integer arithmetic: / truncates toward zero, mod keeps the dividend's sign…
- What will be the output of the following pseudocode? Integer Function g(Integer n) if (n <= 1) return n return g(n - 1) + 2 g(n - 2) End…
- What will be the output of the following pseudocode? String s = "" Integer n = 13 while (n > 0) s = (n mod 2) + s // prepend the digit n =…
- What will be the output of the following pseudocode? Integer i = 1, j, c = 0 while (i <= 16) j = i while (j <= 16) c = c + 1 j = j 2 end…
- What will be the output of the following pseudocode? Integer a = 0 for (i = 1 to 10) if (i mod 2 == 0) continue if (i > 7) break a = a + i…