Consider the Python function: Which character will never be printed for any integer n?
Accenture technical mcq question, verified with a worked answer. Free to practise - no sign-up.
Consider the Python function:
def describe(n):
if n > 0:
if n < 0:
print('X')
else:
print('Y')
else:
print('Z')
Which character will never be printed for any integer n?
Show answer & explanation
Answer: A. 'X'
The character 'X' is unreachable because it requires n > 0 AND n < 0 to be true simultaneously, which is logically impossible. For any integer n, either 'Y' (when n > 0), 'Z' (when n ≤ 0) will be printed, but never 'X'.
Step-by-step Derivation:
Trace the control flow:
- For n > 0: Enter first if block → Check inner condition (n < 0) → Since n > 0, n cannot be < 0 → Execute else block → Print 'Y' ✓
- For n ≤ 0: Skip first if block → Execute else block → Print 'Z' ✓
- For 'X' to print: Need n > 0 AND n < 0 simultaneously → Impossible for any real number
Therefore, 'X' is unreachable code.