nerdexam
Python_Institute

PCEP-30-02 · Question #288

How many stars will the following snippet print to the monitor? i = 4 while i > 0: i -= 2 print('') if i == 2: break else: print('')

The code initializes i to 4. The while loop starts. In the first iteration, i becomes 2, and one star is printed. Then, if i == 2 is true, so break is executed, terminating the loop. The else block of the while loop is not executed because the loop was terminated by break…

Module 3: Boolean Values, Conditional Execution, Loops, Lists and List Processing, Logical and Bitwise Operations

Question

How many stars will the following snippet print to the monitor? i = 4 while i > 0: i -= 2 print('') if i == 2: break else: print('')

Explanation

The code initializes i to 4. The while loop starts. In the first iteration, i becomes 2, and one star is printed. Then, if i == 2 is true, so break is executed, terminating the loop. The else block of the while loop is not executed because the loop was terminated by break. Thus, only one star is printed.

Topics

#while loop#break statement#loop else clause#loop tracing

Community Discussion

No community discussion yet for this question.

Full PCEP-30-02 Practice