The following Python function takes an integer x as input and prints a character: Which...
Accenture technical mcq question, verified with a worked answer. Free to practise - no sign-up.
The following Python function takes an integer x as input and prints a character:
def describe(x):
if x == 0:
if x > 0:
print('A')
else:
print('B')
else:
if x > 0:
print('C')
else:
print('D')
Which character will never be printed?
Show answer & explanation
Answer: A. 'A'
The outer condition checks if x == 0. When this is true, the inner condition if x > 0 can never be true because x is already 0, so the branch printing 'A' is unreachable. The other branches ('B', 'C', 'D') are all reachable under different values of x (x == 0 with fallback for 'B', x > 0 for 'C', x < 0 for 'D').
Step-by-step Derivation:
Trace through all possible cases:
- x == 0: Enters outer if block. Inner condition
x > 0is false (0 is not > 0), so prints 'B'. Path to 'A' is unreachable. - x > 0: Enters outer else block. Inner condition
x > 0is true, so prints 'C'. - x < 0: Enters outer else block. Inner condition
x > 0is false, so prints 'D'.
Conclusion: 'A' is the only character that can never be printed because the condition x == 0 AND x > 0 is logically impossible.