CS FundamentalsHardProduct-company placement · Python
What does this code print?
What does this code print?
ops = [lambda x: x * k for k in range(1, 4)] print([op(10) for op in ops])
- A. [10, 20, 30]
- B. [30, 30, 30]
- C. [0, 10, 20]
- D. [30, 20, 10]
Correct answer: B. [30, 30, 30]
The lambdas close over the variable k, not its value at creation, and are called after the comprehension ends with k = 3, so each returns 30. A strategy list built this way needs lambda x, k=k: x * k to bind each value.
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
- What does this code print (lines joined by ' / ')? python def tag(name): def deco(f): print(name) def w(): return f'<{name}>' + f() +…
- In this Observer code, handler a unsubscribes itself the first time it runs. What does it print (lines joined by ' / ')? python class Bus…
- A class gets its dependencies by calling ServiceLocator.get('Mailer') inside its methods, instead of receiving them through its…
- In a layered application, which pairing of the Repository and Unit of Work patterns is correct?
- How many of these statements about event sourcing are correct? 1. The append-only log of domain events is the source of truth, and current…
- Match each anti-pattern with its description. P. Golden hammer Q. Lava flow R. God object S. Cargo cult 1. One class knows and does far…