What is the expected result of executing the following code?
def o(p): def q(): return '*' * p return q r = o(1) s = o(2) print(r() + s())
- It will print *
- It will print ****
- It will print ***
- It will print **
Explanation: This code snippet implements Closures, which are inner functions enclosed within an outer function. Let’s analyze this code snippet:
- The code begins by creating a Closure named r by invoking the outer function o(1). The Closure contains the inner function q() that will return the result of the operation ‘*’ * 1, which is * .
- A second Closure named s is created by invoking the outer function o(2). The Closure contains the inner function q() that will return the result of the operation ‘*’ * 2, which is ** .
- Finally, both closures are invoked within a print function concatenating their results. Therefore, the console output is ***.
More Questions: Python Essentials 2 – Module 4 Test
More Questions: Python Essentials 2 (PE2) Course Final Test
Please login or Register to submit your answer