Bird
Raised Fist0

Identify the error in the following fine calculation code snippet:

medium📝 Analysis Q14 of Q15
LLD - Design — Library Management System

Identify the error in the following fine calculation code snippet:

violation_fine_rates = {'speeding': 100, 'parking': 50}
violations = ['speeding', 'parking', 'signal_jump']
total_fine = sum(violation_fine_rates[v] for v in violations)
print(total_fine)
ASyntaxError due to missing colon
BKeyError occurs because 'signal_jump' is not in the rates dictionary
CTypeError because sum cannot add strings
DNo error, code runs fine
Step-by-Step Solution
Solution:
  1. Step 1: Check dictionary keys against violations

    'signal_jump' is not a key in violation_fine_rates, so accessing it causes a KeyError.
  2. Step 2: Understand error type

    Attempting to access a missing key in a dictionary raises KeyError in Python.
  3. Final Answer:

    KeyError occurs because 'signal_jump' is not in the rates dictionary -> Option B
  4. Quick Check:

    Missing key access = KeyError [OK]
Quick Trick: Check if all violation keys exist in the rates dictionary [OK]
Common Mistakes:
MISTAKES
  • Assuming missing keys return zero
  • Confusing KeyError with SyntaxError
  • Ignoring runtime errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes