ProgrammingModerateTCS NQT · Infosys

What is printed?

What is printed?
n, steps = 27, 0
while n != 1:
    n = n // 2 if n % 2 == 0 else 3 * n + 1
    steps += 1
print(steps, n)
  1. A. 112 1
  2. B. 111 1
  3. C. 111 0
  4. D. 110 1

Correct answer: B. 111 1

The Collatz sequence from 27 takes 111 steps to reach 1, and the loop stops exactly when n is 1. The trap is off-by-one counting of steps.

More Python Programming questions

Exams that ask this topic