What will be the output of the following Python program?
i = 0
while i < 5:
print(i)
i += 1
if i == 3:
break
else:
print(0)
i = 0
while i < 5:
print(i)
i += 1
if i == 3:
break
else:
print(0)A. error
B. 0 1 2 0
C. 0 1 2
D. none of the mentioned
Answer: Option C
Solution (By Examveda Team)
The code initializes the variablei to 0.Then, it enters a
while loop that continues as long as i is less than 5.Inside the loop, it prints the current value of
i.It then increments
i by 1.If
i becomes equal to 3, the if condition is met, and the break statement is executed, exiting the loop.Otherwise, the loop continues until
i reaches 5.Since the loop is exited via the
break statement and not by reaching the end condition, the else block associated with the loop is not executed.Therefore, the output will be: 0 1 2.
Related Questions on Introduction to Python
What is Python primarily used for?
A. Web browsing
B. Data analysis
C. Video editing
D. Game development
Who developed Python Programming Language?
A. Wick van Rossum
B. Rasmus Lerdorf
C. Guido van Rossum
D. Niene Stom
Which of the following is an advantage of Python?
A. It compiles to machine code
B. It enforces strict typing
C. It has a large standard library
D. It requires extensive memory management
What is the role of the Python interpreter?
A. To convert Python code to assembly language
B. To manage memory allocation
C. To execute Python code line by line
D. To compile Python code into bytecode

Join The Discussion