Flow of Control · Class 11 · CBSE · Science Computer Science

What is the output of the following Python code? total = 0 for i in range(1, 4): for j in range(i): if j == 1: break total += 1 print(total)

Practice question from Flow of Control (Class 11 · CBSE · Science Computer Science), with the correct answer and a step-by-step solution.

hard · MCQ · Q-DC9105

What is the output of the following Python code? total = 0 for i in range(1, 4): for j in range(i): if j == 1: break total += 1 print(total)

  1. A.1
  2. B.2
  3. C.3Correct answer
  4. D.6

Solution

For i = 1, the inner loop increments total once. For i = 2 and i = 3, it increments once for j = 0 and then stops at j = 1 because of break. Therefore, the final value of total is 3.

More from Flow of Control

All of Flow of Control

Practise more Computer Science

Sign up free for adaptive practice on this chapter, with instant feedback and hints on every question.

Start practising free

Free to start · No credit card needed