Data Structures & AlgorithmsHardGATE CS · DSA

What does this loop-counting code print? (Python)

What does this loop-counting code print? (Python)
n, c = 300, 0
i = 2
while i <= n:
    c += 1
    i = i * i
print(c)
  1. A. 4
  2. B. 5
  3. C. 8
  4. D. 6

Correct answer: A. 4

i goes 2, 4, 16, 256, 65536, … (squaring), so the loop runs Θ(log log n) times. Exact count: 4.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic