*args, **kwargs & Argument Unpacking — Concept Check
Questions on *args, **kwargs & Argument Unpacking. *args collects extra positional arguments into a tuple, **kwargs collects extra keyword arguments into a dict — and the same stars unpack sequences/dicts INTO calls.
Questions in this quiz
8 questions on *args, **kwargs & Argument Unpacking — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A developer wants to pass a list of dimensions to a function that calculates volume. Which syntax correctly unpacks the list?
- Avolume([*dims])
- Bvolume(dims)
- Cvolume(**dims)
- Dvolume(*dims)
2.In a function signature, what is the correct order of parameters when using *args and **kwargs?
- A*args, positional, keyword-only, **kwargs
- Bpositional, *args, keyword-only, **kwargs
- Cpositional, **kwargs, *args, keyword-only
- Dpositional, keyword-only, *args, **kwargs
3.A function is defined as def func(a, *args, b, **kwargs). What happens if you call func(1, 2, b=3, c=4)?
- Aa=1, args=(2,), b=4, kwargs={'c': 3}
- Ba=1, args=(), b=2, kwargs={'c': 4}
- Ca=1, args=(2, 3), b=4, kwargs={}
- Da=1, args=(2,), b=3, kwargs={'c': 4}
4.What will be printed by the following code? first, *rest = [1, 2, 3, 4]; print(first, rest)
- A1 2 3 4
- B[1] [2, 3, 4]
- C[1, 2, 3] 4
- D1 [2, 3, 4]
5.A developer mistakenly believes that **kwargs can only be used with dictionaries. What is incorrect about this belief?
- A**kwargs can unpack lists as well
- B**kwargs is only for positional arguments
- C**kwargs is not used for unpacking
- D**kwargs can unpack any mapping, not just dictionaries
6.A function is defined with def example(*args, **kwargs). What is the type of args and kwargs?
- Aargs is a list, kwargs is a list
- Bargs is a tuple, kwargs is a dict
- Cargs is a list, kwargs is a dict
- Dargs is a dict, kwargs is a tuple
7.What will be the output of the following code? *init, last = [1, 2, 3, 4]; print(init, last)
- A[1, 2, 3, 4] []
- B1 [2, 3, 4]
- C[1, 2] [3, 4]
- D[1, 2, 3] 4
8.A developer uses ** to merge two dictionaries a and b. What is the correct syntax?
- Amerged = {**a, **b}
- Bmerged = {*a, **b}
- Cmerged = {*a, *b}
- Dmerged = [**a, **b]