What does the following code print? x = 10 if x > 5: if x > 15: print("A") else: print("B") else: print("C")
Correct output.
Why this answer
The code first checks if x > 5, which is true because x = 10. Then it checks if x > 15, which is false, so the else branch of the inner if-else executes, printing 'B'. The outer else is skipped entirely.
Exam trap
Python Institute often tests the misconception that the outer else (printing 'C') will execute when the inner condition fails, but candidates must remember that the outer else only runs if the outer condition is false.
How to eliminate wrong answers
Option A is wrong because the code does produce output; the inner else branch executes. Option B is wrong because 'C' would only print if the outer condition x > 5 were false, but it is true. Option D is wrong because 'A' would print only if x > 15 were true, but x = 10 is not greater than 15.