Intermediate Python Functions: Mastering Functionality
Assess your understanding of Python functions with intermediate-level questions.
Questions in this quiz
12 questions on Intermediate Python Functions: Mastering Functionality. Work through them here, then start the quiz to answer them and see your score.
1.What will be the output of the following code snippet? ```python def func(a, b=5, c=10): return a + b + c result = func(3, c=20) print(result) ```
- A28
- B38
- C18
- DNone
2.Which of the following is a valid way to define a function in Python?
- Adef my_func():
- Bfunction my_func():
- Cdef my_func[]:
- Dmy_func() => {}
3.What is the output of the following code? ```python def outer(): x = 'outer' def inner(): nonlocal x x = 'inner' inner() return x print(outer()) ```
- A'outer'
- BError
- CNone
- D'inner'
4.What will the following code output? ```python def func(a, *args): return args print(func(1, 2, 3, 4)) ```
- A(1,)
- B(2, 3, 4)
- C(1, 2, 3, 4)
- D[2, 3, 4]
5.How can you define a lambda function to add two numbers in Python?
- Aadd = def(x, y): x + y
- Badd = lambda x, y: x + y
- Cadd = function(x, y) { return x + y }
- Dadd = (x, y) => x + y
6.What is the purpose of the 'return' statement in a Python function?
- ATo define a function
- BTo pass control to another function
- CTo print the result
- DTo exit the function and return a value
7.Which of the following is true about default arguments in Python functions?
- AThey must be immutable
- BThey must be defined first
- CThey are evaluated at runtime
- DThey are evaluated at compile time
8.What will be the output of the following code? ```python def func(a, b=[]): b.append(a) return b print(func(1)) print(func(2)) ```
- A[1, 2] [1, 2]
- B[1] [1, 2]
- C[1] [2]
- D[1] [1]
9.How can you ensure a function parameter is always passed by keyword in Python?
- APlacing it after a * in the parameter list
- BUsing *args
- CUsing **kwargs
- DUsing a default value
10.What does the following code output? ```python def func(x, y, z): return x + y + z params = {'x': 1, 'y': 2, 'z': 3} print(func(**params)) ```
- A6
- BNone
- C123
- DError
11.What is the result of the following code? ```python def func(a, b): pass result = func(1, 2) print(result) ```
- A1
- BError
- CNone
- D2
12.Which of the following is a correct way to handle variable-length keyword arguments in a function?
- Adef func(**kwargs):
- Bdef func($kwargs):
- Cdef func(&kwargs):
- Ddef func(*kwargs):