CS FundamentalsVery hardProduct-company placement · Python

What does this code print (lines joined by ' / ')?

What does this code print (lines joined by ' / ')?
handlers = {'+': lambda a, b: a + b}
def fallback():
    print('fallback')
    return lambda a, b: None

print(handlers.get('+', fallback())(2, 3))
  1. A. 5
  2. B. fallback / 5
  3. C. fallback / None
  4. D. None

Correct answer: B. fallback / 5

Arguments are evaluated before get runs, so fallback() executes (printing 'fallback') even though '+' exists; get then returns the addition lambda, which gives 5. A lazy default needs handlers.get('+') or fallback_function and a separate call.

More Software Engineering, Git and Cloud Basics questions

Exams that ask this topic