Bird
0
0

Analyze the following fine calculation code snippet. What issue will arise during execution?

medium📝 Analysis Q6 of 15
LLD - Design — Library Management System

Analyze the following fine calculation code snippet. What issue will arise during execution?

fineRates = {"speeding": 150, "parking": 75}
violations = ["speeding", "noHelmet"]
totalFine = 0
for v in violations:
    totalFine += fineRates[v]
AIncorrect total fine calculation due to duplicate keys
BKeyError due to missing 'noHelmet' key in fineRates
CSyntax error in the for loop
DNo error, totalFine will be 225
Step-by-Step Solution
Solution:
  1. Step 1: Check keys in fineRates

    Keys are "speeding" and "parking" only.
  2. Step 2: Check violations list

    Contains "speeding" and "noHelmet".
  3. Step 3: Identify issue

    Accessing fineRates["noHelmet"] will cause a KeyError since key is missing.
  4. Final Answer:

    KeyError due to missing 'noHelmet' key -> Option B
  5. Quick Check:

    Missing keys cause runtime errors [OK]
Quick Trick: Missing keys in map cause KeyError [OK]
Common Mistakes:
MISTAKES
  • Assuming missing keys default to zero
  • Ignoring runtime exceptions
  • Confusing syntax errors with key errors

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More LLD Quizzes