Loading samples...
Loading samples...
py-sample-11
Understanding Python functions, arguments, and variable scope
Which keyword defines a function?
What is a lambda function?
Which is a valid default argument?
What does *args represent?
What does **kwargs represent?
What is the scope of a variable inside a function?
How to access global variable inside function?
What is a recursive function?
What is returned if function has no return?
Which decorator is used for memoization?
Explain the difference between parameters and arguments with examples.
[3 marks]What are *args and **kwargs? When are they useful?
[3 marks]Explain LEGB rule for variable scope in Python.
[3 marks]What is the difference between global and nonlocal keywords?
[3 marks]Explain first-class functions in Python with examples.
[3 marks]What is a closure? When does Python create a closure?
[3 marks]Explain function decorators with a practical example.
[3 marks]What are pure functions? Why are they important?
[3 marks]Explain type hints and function annotations in Python.
[3 marks]What is recursion depth limit? How to handle it?
[3 marks]Write a recursive function to calculate factorial with memoization.
[5 marks]Create a decorator that logs function execution time.
[5 marks]Write a function that accepts any number of arguments and returns their product.
[5 marks]Create a closure that generates unique incrementing IDs.
[5 marks]Write a higher-order function that takes a function and applies it twice.
[5 marks]Create a function with default mutable argument fix.
[5 marks]Predict output: def f(): x = 10 f() print(x)
[2 marks]Explanation:x is local to f(). Not accessible outside function scope.
Find error: def greet(name, greeting='Hello'): print(greeting, name) greet(greeting='Hi', 'John')
[3 marks]Explanation:Keyword args must come after positional args. Swap order: greet('John', greeting='Hi')
Predict output: x = 5 def func(): global x x = 10 func() print(x)
[2 marks]Explanation:global x allows modifying module-level x. After func(), x becomes 10.
Find error: def outer(): x = 5 def inner(): x += 1 inner() outer()
[3 marks]Explanation:Need 'nonlocal x' in inner() to modify outer's x, or declare differently.
Predict output: def f(a, L=[]): L.append(a) return L print(f(1)) print(f(2))
[3 marks]Explanation:Default mutable argument persists! L is same list across calls. Classic Python gotcha.
Find error: lambda x: x + 1 print(result(5))
[2 marks]Explanation:Lambda wasn't assigned to variable. Should be: result = lambda x: x + 1
Predict output: def func(*args, **kwargs): print(args) print(kwargs) func(1, 2, a=3, b=4)
[3 marks]Explanation:*args collects positional args as tuple. **kwargs collects keyword args as dict.
Find error: def add(a, b): return a + b print(add(1, 2, 3))
[2 marks]Explanation:Function defined for 2 args but 3 provided. Use *args for variable args.