Functions & Lambdas Questions with Answers

7 practice questions on Functions & Lambdas (Python) with answers and step-by-step solutions. Try 7 samples below, then practise all 7 free in the AptiNest app.

Sample questions

Question 1Easy

What does a Python function return if it has no return statement?

  1. None
  2. It raises an error
  3. 0
  4. An empty tuple

Answer: A. None

Explanation

A function that ends without executing a return statement returns None.

Step-by-step solution

  1. A function without return finishes and hands back a default value.
  2. That default is None.

Why the other options are wrong

  • B. Python does not raise an error for a missing return.
  • C. None is not 0; it is its own type (NoneType).
  • D. No tuple is created implicitly.

Tip: Print the result of a no-return function to see None.

Question 2Easy

What is the output?

def f(*args):
    return len(args)

print(f(1, 2, 3))
  1. (1, 2, 3)
  2. 3
  3. 1
  4. Error

Answer: B. 3

Explanation

*args collects the positional arguments into a tuple, here (1, 2, 3), whose length is 3.

Step-by-step solution

  1. *args gathers all the positional arguments into a tuple.
  2. The call f(1, 2, 3) gives args = (1, 2, 3).
  3. len(args) is 3, the correct answer.

Why the other options are wrong

  • A. (1, 2, 3) is wrong; the function returns the length, not the tuple.
  • C. 1 is wrong; the tuple has three items.
  • D. Error is wrong; *args accepts any number of positional arguments.

Tip: *args is a tuple of the extra positional arguments, and kwargs is a dict of keyword arguments.

Question 3Easy

What is the output?

x = 10

def f():
    x = 20

f()
print(x)
  1. 20
  2. None
  3. UnboundLocalError
  4. 10

Answer: D. 10

Explanation

Assigning x inside f creates a new local variable. The global x is unchanged unless you declare global x.

Step-by-step solution

  1. f() assigns x locally.
  2. The local x = 20 does not affect the global.
  3. print(x) shows 10.

Why the other options are wrong

  • A. Local assignment does not change global.
  • B. print(x) prints the global x.
  • C. No local is read before assignment.

Question 4Medium

What is the output?

def f(a, b=2, *args, **kw):
    print(a, b, args, kw)

f(1, 3, 4, x=5)
  1. 1 3 (4, 5) {}
  2. 1 3 (4,) {'x': 5}
  3. 1 3 [4] {'x': 5}
  4. 1 2 (3, 4) {'x': 5}

Answer: B. 1 3 (4,) {'x': 5}

Explanation

Positional arguments fill a=1 and b=3, the extra positional 4 goes into args as (4,), and the keyword x=5 goes into kw.

Step-by-step solution

  1. In f(1, 3, 4, x=5), the first positional value 1 fills a, and the second value 3 fills b.
  2. The extra positional value 4 goes into args as the one-item tuple (4,).
  3. x=5 is a keyword argument, so kw is {'x': 5}. The output is 1 3 (4,) {'x': 5}, the correct answer.

Why the other options are wrong

  • A. (4, 5) is wrong; 5 is a keyword argument, so it goes into kw, not args.
  • C. [4] is wrong; args is a tuple, so it prints as (4,).
  • D. 1 2 (3, 4) is wrong; b gets 3, not the default 2, and 3 is not in args.

Tip: Positional arguments fill the parameters in order, *args collects the extra ones, and kw collects keyword arguments.

Question 5Medium

What is the output?

print(list(map(lambda x: x * 2, filter(lambda x: x % 2, range(6)))))
  1. [0, 4, 8]
  2. [2, 6, 10]
  3. [0, 2, 4, 6, 8, 10]
  4. [1, 3, 5]

Answer: B. [2, 6, 10]

Explanation

filter keeps the odd numbers 1, 3, 5 (x % 2 is truthy for them), and map doubles each one to give 2, 6, 10.

Step-by-step solution

  1. range(6) gives 0 to 5. filter keeps the values where x % 2 is not 0, which are 1, 3 and 5.
  2. Map doubles each of them: 2, 6 and 10.
  3. The output is [2, 6, 10], the correct answer.

Why the other options are wrong

  • A. [0, 4, 8] is wrong; filter keeps the odd values 1, 3 and 5, not the even ones.
  • C. [0, 2, 4, 6, 8, 10] is wrong; filter removes the even values before map runs.
  • D. [1, 3, 5] is wrong; map doubles the values, so they are not 1, 3 and 5.

Tip: Work from the inside out: filter runs first, then map.

Question 6Hard

What is the output?

fs = [lambda: i for i in range(3)]
print([f() for f in fs])
  1. [0, 1, 2]
  2. [3, 3, 3]
  3. [0, 0, 0]
  4. [2, 2, 2]

Answer: D. [2, 2, 2]

Explanation

Closures capture the variable i, not its value at creation time. By the time the lambdas run, the loop has finished and i is 2. Use lambda i=i: i to capture each value.

Step-by-step solution

  1. Each lambda refers to the variable i, not to the value i had when the lambda was made.
  2. The loop ends with i = 2, and all three lambdas read that same i.
  3. So the output is [2, 2, 2], the correct answer.

Why the other options are wrong

  • A. [0, 1, 2] is wrong; the lambdas share one variable, so they do not keep each value.
  • B. [3, 3, 3] is wrong; the loop ends at 2, not 3.
  • C. [0, 0, 0] is wrong; the final value of i is 2.

Tip: To capture each value, bind it as a default argument, as in lambda i=i: i.

Question 7Hard

What is the output?

def add(x, acc=[]):
    acc.append(x)
    return acc

add(1)
print(add(2))
  1. [[1], 2]
  2. [2]
  3. [1]
  4. [1, 2]

Answer: D. [1, 2]

Explanation

Default values are evaluated once, when the function is defined, so every call that omits acc shares the same list. Use acc=None and create the list inside the function.

Step-by-step solution

  1. The default list acc=[] is created once, when the function is defined.
  2. add(1) appends 1 to that shared list. add(2) appends 2 to the same list.
  3. The second call returns [1, 2], the correct answer.

Why the other options are wrong

  • A. [[1], 2] is wrong; the function returns one flat list, not nested lists.
  • B. [2] is wrong; the list is shared, so the 1 from the first call is still in it.
  • C. [1] is wrong; the second call adds 2 to the same list.

Tip: Avoid a mutable default like [] in a function. Use acc=None and create the list inside the function.

Practise all 7 questions free

The full question bank with timed practice, progress tracking and Vidya, your AI tutor. Free, no ads.

Open this topic in the app →

Related topics

See all Python topics · All topics