Skip to content

Commit

Permalink
Added new question to python-quiz.md (#7086)
Browse files Browse the repository at this point in the history
  • Loading branch information
r-vik authored Oct 27, 2024
1 parent d4b47dc commit 7ce2ce0
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions python/python-quiz.md
Original file line number Diff line number Diff line change
Expand Up @@ -2672,3 +2672,25 @@ The code defines a `square` function to calculate the square of a number. It the
- [ ] Hello {name1} and {name2}
- [ ] Error
- [ ] Hello and

### Q185. What will be the ouput of the following code snippet?

```python
def outer_func(x):
y = x + 1
def inner_func():
return y + x
return inner_func

x = 10
y = 20
closure_func = outer_func(x)
print(closure_func())
```
- [ ] 30
- [x] 21
- [ ] 11
- [ ] 31

**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.

0 comments on commit 7ce2ce0

Please sign in to comment.