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
  1. A. 7
  2. B. 9
  3. C. 3
  4. 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.

More Pseudocode and Code Tracing questions

Exams that ask this topic