In the following code: Which string will never be printed?
Accenture technical mcq question, verified with a worked answer. Free to practise - no sign-up.
In the following code:
def describe(n):
if n == 5:
if n != 5:
print('A')
else:
print('B')
else:
print('C')
Which string will never be printed?
Show answer & explanation
Answer: A. 'A'
The outer condition if n == 5: restricts execution to cases where n equals 5. Inside that block, the nested condition if n != 5: can never be true because n is already confirmed to be 5. Therefore, the branch printing 'A' is unreachable dead code. The else branch prints 'B' when n == 5, and 'C' is printed when n != 5.
Step-by-step Derivation:
Execution trace:
- If n == 5: Enter outer if block → evaluate inner condition
n != 5→ this is FALSE (since n IS 5) → skip 'A' branch → execute else → print('B') - If n != 5: Skip outer if block → execute outer else → print('C')
The condition n != 5 inside the if n == 5: block creates a logical contradiction. When n equals 5 (outer condition true), n cannot simultaneously not equal 5 (inner condition). Thus 'A' is dead code and will never execute.