ProgrammingVery hardProduct company OA · code-tracing level
What will be the output of the following pseudocode?
What will be the output of the following pseudocode?
Integer c = 0, d, k
for (each x from 1 to 30)
k = 0
for (each d from 1 to x)
if (x mod d == 0)
k = k + 1
end if
end for
if (k == 4)
c = c + 1
end if
end for
Print c- A. 7
- B. 9
- C. 3
- D. 16
Correct answer: B. 9
c counts numbers up to 30 with exactly 4 divisors: p³ (8, 27) or p × q for distinct primes (6, 10, 14, 15, 21, 22, 26), 9 in all. 7 forgets the cubes, 3 is what k == 3 would count (4, 9, 25), and 16 counts every number with at least 4 divisors, such as 12 and 30.
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? (Array positions run from 1 to 10.) Integer alive[1..10] = all 1, left = 10, pos = 0…
- What will be the output of the following pseudocode? (& is bitwise AND, | is bitwise OR, << and >> are shifts.) Integer x = 22, r = 0 for…
- What will be the output of the following pseudocode? (Arrays are indexed from 0; / is integer division.) Integer a[7] = {1, 3, 5, 5, 5, 7…
- What will be the output of the following pseudocode? Integer s = 0 Procedure p(Integer n) if (n == 0) return end if p(n - 1) s = s + n p(n…
- What will be the output of the following pseudocode? (/ is integer division.) Function A(Integer n) if (n <= 0) return 1 end if return B(n…
- What will be the output of the following pseudocode? (g is a global variable.) Integer g = 0 Function f(Integer n) if (n == 0) return 0…