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
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

Related questions

0 votes
1 answer
0 votes
3 answers
0 votes
2 answers
0 votes
2 answers
asked Feb 9, 2021 in Python by Rekha (2.2k points)

1.2k questions

2.7k answers

501 comments

693 users

Browse Categories

...