ProgrammingVery hardTCS NQT Advanced · Goldman Sachs OA
What will be the output of the following pseudocode?
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 (d * d > n) break
if (n mod d == 0) return false
end for
return true
End Function
Integer c = 0
for (each p from 2 to 50)
if (isPrime(p) AND isPrime(100 - p))
c = c + 1
end if
end for
Print c- A. 5
- B. 12
- C. 6
- D. 7
Correct answer: C. 6
With p ≤ 50 each split of 100 into two primes p + q (p ≤ q) is counted once: 3 + 97, 11 + 89, 17 + 83, 29 + 71, 41 + 59, 47 + 53. c = 6.
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) 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…
- 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…