Skip to content

Commit 7ce2ce0

Browse files
authored
Added new question to python-quiz.md (#7086)
1 parent d4b47dc commit 7ce2ce0

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

Diff for: python/python-quiz.md

+22
Original file line numberDiff line numberDiff line change
@@ -2672,3 +2672,25 @@ The code defines a `square` function to calculate the square of a number. It the
26722672
- [ ] Hello {name1} and {name2}
26732673
- [ ] Error
26742674
- [ ] Hello and
2675+
2676+
### Q185. What will be the ouput of the following code snippet?
2677+
2678+
```python
2679+
def outer_func(x):
2680+
y = x + 1
2681+
def inner_func():
2682+
return y + x
2683+
return inner_func
2684+
2685+
x = 10
2686+
y = 20
2687+
closure_func = outer_func(x)
2688+
print(closure_func())
2689+
```
2690+
- [ ] 30
2691+
- [x] 21
2692+
- [ ] 11
2693+
- [ ] 31
2694+
2695+
**Explanation**: When `outer_func(10)` is called, `y` is set to 11 within `outer_func`. The `inner_func`, which has access to `outer_func`'s scope, returns `y` + `x`. When `closure_func()` is called, it uses `y` = `11` (from `outer_func`) and `x` = `10` from the global scope, not from the function’s argument. Therefore, `closure_func()` returns 11 + 10 = 21.
2696+

0 commit comments

Comments
 (0)