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))- A. 5
- B. fallback / 5
- C. fallback / None
- 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.
Practise more Software Engineering, Git and Cloud Basics questionsSoftware Engineering, Git and Cloud Basics: notes and key points
More Software Engineering, Git and Cloud Basics questions
- A Python app keeps a shared Settings object at module level in settings.py as a singleton. Most code imports it as app.settings, but one…
- What does this code print? python class Count: def init(s, n): s.i = 0; s.n = n def iter(s): return s def next(s): if s.i >= s.n: raise…
- In a strictly layered system, 80% of requests pass through the business and service layers unchanged, which merely forward each call to…
- How many of these are Twelve-Factor App recommendations? 1. Write logs as an event stream to stdout and let the environment route them. 2…
- Before refactoring a legacy pricing module with no tests, a developer records its current outputs for many inputs, including results she…
- A new filter must be added to a 400-line untested method. Instead of editing the method's logic, the developer writes the filter as a new…