Skip to content
C

Python Interview Questions

Functions Interview Questions

Function definitions, argument passing, *args/**kwargs, lambdas, decorators, recursion, and variable scope.

Question 1: What is a function?

Ans

A function is a reusable block of code defined with def that can accept arguments and return a result. Functions reduce duplication and separate responsibilities.

Example

python
def add(a, b): return a + b print(add(2, 3))

Important Point

A function without an explicit return statement returns None.

Question 2: What are positional arguments?

Ans

Positional arguments are matched to parameters according to their order in the function call.

Example

python
def greet(name, city): print(name, city) greet("Amit", "Pune")

Important Point

Passing too few or too many required positional arguments raises TypeError.

Question 3: What are keyword arguments?

Ans

Keyword arguments pass values using parameter names, making the call's intent clearer and allowing arguments to be supplied in a different order.

Example

python
def connect(host, port): print(host, port) connect(port=5432, host="localhost")

Important Point

After a positional argument, keyword arguments are normally used; positional-only and keyword-only parameters can further constrain calls.

Question 4: What are default arguments?

Ans

A default argument provides a value used when the caller does not supply that parameter.

Example

python
def greet(name="Guest"): return f"Hello {name}" print(greet())

Important Point

Avoid mutable objects such as [] or {} as defaults when you intend a fresh object per call.

Question 5: Why are mutable default arguments dangerous?

Ans

Default argument expressions are evaluated once when the function is defined, so a mutable default list or dictionary is shared across calls.

Example

python
def add_item(item, items=[]): items.append(item) return items print(add_item("A")) print(add_item("B"))

Important Point

Use `None` as the default and create a new list inside the function.

Question 6: What is *args?

Ans

*args collects extra positional arguments into a tuple, allowing a function to accept a variable number of positional values.

Example

python
def total(*numbers): return sum(numbers) print(total(10, 20, 30))

Important Point

The name `args` is conventional; the asterisk is the syntax that matters.

Question 7: What is **kwargs?

Ans

**kwargs collects extra keyword arguments into a dictionary.

Example

python
def show(**details): print(details) show(name="Amit", city="Pune")

Important Point

The name `kwargs` is conventional; the double asterisk performs keyword collection.

Question 8: What is lambda?

Ans

A lambda is a small anonymous function expression. It is useful when a short function is needed as an argument to another function.

Example

python
square = lambda x: x * x print(square(5))

Important Point

Use `def` when the function needs multiple statements, documentation, or meaningful standalone reuse.

Question 9: What is a higher-order function?

Ans

A higher-order function accepts functions as arguments, returns a function, or both. Python supports this because functions are first-class objects.

Example

python
def apply(fn, value): return fn(value) print(apply(lambda x: x * 2, 5))

Important Point

The concept is useful in decorators, callbacks, sorting, and functional-style code.

Question 10: What is a decorator?

Ans

A decorator is a callable that wraps another function or class to add or modify behavior without changing the original implementation directly.

Example

python
def log_call(fn): def wrapper(*args, **kwargs): print("Calling", fn.__name__) return fn(*args, **kwargs) return wrapper @log_call def greet(): return "Hello" print(greet())

Important Point

Use `functools.wraps` in production decorators to preserve useful metadata such as the wrapped function's name and docstring.

Question 11: What is recursion?

Ans

Recursion is a function calling itself on a smaller subproblem until a base condition is reached.

Example

python
def factorial(n): if n <= 1: return 1 return n * factorial(n - 1)

Important Point

Python does not optimize tail recursion, and deep recursion can raise RecursionError.

Question 12: What is LEGB?

Ans

LEGB describes Python's common name-resolution order: Local, Enclosing, Global, and Built-in scopes.

Example

python
x = "global" def outer(): x = "enclosing" def inner(): print(x) inner() outer()

Important Point

The `global` and `nonlocal` statements can explicitly change which enclosing binding is assigned.

Question 13: What is global keyword?

Ans

global tells a function that assignments to a specified name should target the module-level global binding instead of creating a local binding.

Example

python
count = 0 def increment(): global count count += 1 increment() print(count)

Important Point

Global mutable state can make testing and concurrency harder, so use it deliberately.

Question 14: What is nonlocal?

Ans

nonlocal tells a nested function to assign to a variable in an enclosing function scope rather than creating a new local variable.

Example

python
def counter(): n = 0 def inc(): nonlocal n n += 1 return n return inc c = counter() print(c(), c())

Important Point

`nonlocal` cannot refer to a module-global variable; it needs an enclosing function scope.

Question 15: What is a docstring?

Ans

A docstring is a string literal used to document a module, class, or function. It can be accessed through __doc__ and tooling can use it for documentation.

Example

python
def add(a, b): """Return the sum of two numbers.""" return a + b print(add.__doc__)

Important Point

A comment is not the same as a docstring; documentation tools commonly inspect docstrings.

Continue Your Preparation