Q1 /20 The following python function takes an input of integer n and prints out 1 character.
Accenture technical mcq question, verified with a worked answer. Free to practise - no sign-up.
/20
The following python function takes an input of integer n and prints out 1 character. Which character will never be printed?
def describe(n):
if n >= 0:
if n < 3:
print("A");
else:
print("B");
else:
if n < 3:
print("C");
else:
print("D");
Show answer & explanation
Answer: D. D
Character 'D' is never printed because it requires n < 0 AND n < 3. However, when n is negative (n < 0), the condition n < 3 is always true for all negative integers (e.g., -5 < 3, -1 < 3), so the else branch (printing 'D') in the inner if-else is never executed. Characters A, B, and C are all reachable: A prints when n ∈ [0, 3), B prints when n ≥ 3, and C prints when n < 0.
Step-by-step Derivation:
Trace through all possible integer ranges:
- n ≥ 0 AND n < 3 (e.g., n=0,1,2): prints 'A' ✓
- n ≥ 0 AND n ≥ 3 (e.g., n=3,4,5): prints 'B' ✓
- n < 0 AND n < 3 (e.g., n=-1,-5,-100): prints 'C' ✓ (ALL negative numbers are < 3)
- n < 0 AND n ≥ 3: IMPOSSIBLE (no number can be both negative and ≥ 3)
Therefore, 'D' is unreachable.