Softenant
Technologies
Python User-Defined Functions MCQs (25) — Answers at the End

Python User-Defined Functions MCQs (25)

Python Training in Vizag (Softenant)

1) What is printed?

def f():
    return 2 + 3
print(f())
A. 5
B. 23
C. None
D. Error

2) Implicit return value?

def f():
    x = 10
print(f())
A. 10
B. None
C. Error
D. 0

3) Positional vs keyword call

def add(a, b):
    return a + b
print(add(2, b=3))
A. 5
B. TypeError
C. 23
D. None

4) Default argument used?

def greet(name="World"):
    return f"Hi, {name}!"
print(greet())
A. Hi, name!
B. Hi, World!
C. None
D. TypeError

5) Mutable default pitfall

def push(x, acc=[]):
    acc.append(x)
    return acc
print(push(1), push(2))
A. [1] [2]
B. [1] [1, 2]
C. [] []
D. Error

6) Safe default pattern

def push(x, acc=None):
    if acc is None:
        acc = []
    acc.append(x)
    return acc
print(push(1), push(2))
A. [1] [2]
B. [1] [1, 2]
C. [] []
D. Error

7) *args collects extra positionals

def f(a, *args):
    return a, args
print(f(1, 2, 3))
A. (1, (2, 3))
B. (1, [2, 3])
C. (1, 2, 3)
D. TypeError

8) **kwargs collects keywords

def f(**kw):
    return sorted(kw.items())
print(f(x=1, y=2))
A. {‘x’:1,’y’:2}
B. [(‘x’,1), (‘y’,2)]
C. (‘x’,1,’y’,2)
D. TypeError

9) Argument unpacking

def add(a, b, c):
    return a + b + c
vals = (1, 2, 3)
print(add(*vals))
A. 6
B. 123
C. TypeError
D. (1,2,3)

10) Keyword-only parameter

def f(a, *, b=0):
    return a + b
print(f(2, b=3))
A. 5
B. TypeError
C. 23
None

11) Positional-only (Py ≥ 3.8)

def f(a, /, b):
    return a * b
print(f(2, b=3))
A. 6
B. TypeError
C. 23
D. None

12) LEGB scope

x = 5
def f():
    x = 7
    return x
print(f(), x)
A. 7 5
B. 5 5
C. 7 7
D. Error

13) global

x = 1
def f():
    global x
    x = 9
f()
print(x)
A. 1
B. 9
C. NameError
D. UnboundLocalError

14) nonlocal

def outer():
    x = 1
    def inner():
        nonlocal x
        x = 2
    inner()
    return x
print(outer())
A. 1
B. 2
C. NameError
D. UnboundLocalError

15) Closure capturing variable

def make_adder(n):
    def add(x):
        return x + n
    return add
f = make_adder(10)
print(f(5))
A. 10
B. 15
C. 5
D. TypeError

16) Late-binding gotcha

funcs = []
for i in range(3):
    funcs.append(lambda: i)
print([f() for f in funcs])
A. [0, 1, 2]
B. [2, 2, 2]
C. [1, 2, 3]
D. Error

17) Fix late-binding

funcs = []
for i in range(3):
    funcs.append(lambda i=i: i)
print([f() for f in funcs])
A. [0, 1, 2]
B. [2, 2, 2]
C. [1, 2, 3]
D. Error

18) Annotations are metadata

def f(x: int) -> str:
    return str(x)
print(f.__annotations__['x'], type(f(5)).__name__)
A. int str
B. str int
C. int int
D. None None

19) Recursion base case

def fact(n):
    return 1 if n == 0 else n * fact(n-1)
print(fact(3))
A. 3
B. 6
C. 9
D. Error

20) Decorator basics

def deco(f):
    def wrap():
        return "X" + f() + "X"
    return wrap
@deco
def hello():
    return "hi"
print(hello())
A. hi
B. XhiX
C. XXhi
D. Error

21) Generator function returns…

def gen():
    yield 1
print(type(gen()).__name__)
A. list
B. generator
C. iterator
D. function

22) return vs yield

def g():
    return 1
def h():
    yield 1
print(g(), list(h()))
A. 1 [1]
B. [1] 1
C. 1 1
D. Error

23) First-class functions

def square(x): return x*x
def apply(f, v): return f(v)
print(apply(square, 4))
A. 8
B. 16
C. square
D. Error

24) Docstring

def f():
    "hello"
print(f.__doc__)
A. None
B. hello
C.
D. Error

25) Function is an object

def f(): pass
g = f
print(f is g, callable(g))
A. True True
B. True False
C. False True
D. False False

Answer Key

1) A

2) B

3) A

4) B

5) B

6) A

7) A

8) B

9) A

10) A

11) B

12) A

13) B

14) B

15) B

16) B

17) A

18) A

19) B

20) B

21) B

22) A

23) B

24) B

25) A