Bird
Raised Fist0

Consider this code snippet for versioning:

medium📝 Debug Q14 of Q15
Rest API - Versioning Strategies
Consider this code snippet for versioning:
def api_handler(request):
    version = request.GET['version']
    if version == '1':
        return 'Version 1'
    else:
        return 'Other version'

What error will occur if the client calls /api/data without a version parameter?
AKeyError because 'version' key is missing
BSyntaxError in code
CReturns 'Version 1' by default
DReturns 'Other version'
Step-by-Step Solution
Solution:
  1. Step 1: Understand how version is accessed

    The code uses request.GET['version'] which raises KeyError if 'version' is missing.
  2. Step 2: Analyze call without version parameter

    Calling without ?version=... means 'version' key is missing, causing KeyError.
  3. Final Answer:

    KeyError because 'version' key is missing -> Option A
  4. Quick Check:

    Missing key access causes KeyError [OK]
Quick Trick: Access query keys safely to avoid KeyError [OK]
Common Mistakes:
MISTAKES
  • Assuming default version is used automatically
  • Expecting no error when parameter is missing
  • Confusing KeyError with SyntaxError

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Rest API Quizzes