Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (2.2k points)

i = 0

while i < 5:

    print(i)

    i += 1

    if i == 3:

        break

else:

    print(0)

A) 0 1 2 0

B) 0 1 2

C) error

D) none of the mentioned

2 Answers

0 votes
by (3.9k points)

The correct answer to the question “What will be the output of the following Python code” is option (B). 0 1 2. Because there is a break control statement, in the script, so else statement won’t be executed. If you wish to learn Python, then check out the Python certification course, from Intellipaat. And also check out the Python Programming video made by our expert team to get your fundamentals correct.

0 votes
ago by (1.9k points)

The following Python code will print: B) 0 1 2. 

Explanation: 

The while loop starts at i = 0 and runs while true for i < 5 In the body of the loop, it prints i and then increments i When i is equal to 3, the loop will exit with the break statement. The else of the while loop is never executed because the loop terminates with break. So the printed output will be like this 0,1,2. There'll be no more output on the else block. 

Final output: 

2

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...