Examveda

What will be the output of the following Python code?
i = 1
while True:
    if i%3 == 0:
        break
    print(i)
 
    i + = 1

A. 1 2 3

B. error

C. 1 2

D. none of the mentioned

Answer: Option B

Solution (By Examveda Team)

Explanation:
The code starts with `i` initialized to 1.
The `while True` loop runs indefinitely until a `break` statement is encountered.
Inside the loop, the `if i%3 == 0:` condition checks if `i` is divisible by 3.
If `i` is divisible by 3, the `break` statement will terminate the loop.
If `i` is not divisible by 3, the value of `i` is printed.
Then, there's an error `i += 1` it should be not `i + = 1`.
So the correct answer is Option B: error

This Question Belongs to Python Program >> Introduction To Python

Join The Discussion

Comments (1)

  1. Tejas Patil
    Tejas Patil:
    4 months ago

    as per me the answere should be C
    i have also tried running this code


    i=1
    while True:
    if(i%3==0):
    break
    print(i)
    i+=1

Related Questions on Introduction to Python