Learn with Yasir

Share Your Feedback

Python *args and **kwargs MCQs – Multiple Choice Questions for Practice and Learning

Test your understanding of Python *args and **kwargs with these multiple choice questions. Practice function arguments, flexible parameters, and advanced function concepts with beginner-friendly MCQs and detailed answers. Ideal for students and Python learners.

Topic: args-kwargs


📝 Multiple Choice Questions

🟢 Beginner

Q1. What is the type of the variable that collects positional arguments when using `*args` in a Python function?

  • 🟢 A. list
  • 🔵 B. tuple
  • 🟠 C. dict
  • 🔴 D. set
Answer

tuple

`*args` collects all extra positional arguments into a tuple inside the function.


Q2. What does `**kwargs` collect inside a Python function?

  • 🟢 A. a list of positional arguments
  • 🔵 B. a tuple of positional arguments
  • 🟠 C. a dictionary of keyword arguments
  • 🔴 D. a set of keyword arguments
Answer

a dictionary of keyword arguments

`**kwargs` collects all extra keyword arguments into a dictionary inside the function.


Q3. Which symbol is used to define `*args` in a Python function?

  • 🟢 A. *
  • 🔵 B. **
  • 🟠 C. &
  • 🔴 D. #
Answer

*

The single asterisk `*` is used to collect positional arguments.


Q4. Which symbol is used to define `**kwargs` in a Python function?

  • 🟢 A. *
  • 🔵 B. **
  • 🟠 C. &
  • 🔴 D. ##
Answer

**

The double asterisk `**` is used to collect keyword arguments into a dictionary.


🟡 Intermediate

Q1. What is the output of the following code?

def test(*args):
    return args
print(test(1, 2, 3))
  • 🟢 A. [1, 2, 3]
  • 🔵 B. (1, 2, 3)
  • 🟠 C. {1: 2, 3: None}
  • 🔴 D. Error
Answer

(1, 2, 3)

`*args` packs the arguments into a tuple, so the output is (1, 2, 3).


Q2. What is the output of the following code?

def test(**kwargs):
    return kwargs
print(test(a=1, b=2))
  • 🟢 A. {'a': 1, 'b': 2}
  • 🔵 B. [('a', 1), ('b', 2)]
  • 🟠 C. ('a', 'b')
  • 🔴 D. Error
Answer

{'a': 1, 'b': 2}

`**kwargs` collects the keyword arguments into a dictionary.


Q3. Which of the following is TRUE about using both `*args` and `**kwargs` in the same function?

  • 🟢 A. `*args` must come before `**kwargs`
  • 🔵 B. `**kwargs` must come before `*args`
  • 🟠 C. Order does not matter
  • 🔴 D. They cannot be used together
Answer

`*args` must come before `**kwargs`

In function definitions, `*args` must appear before `**kwargs`.


Q4. What will the following function print?

def show(*args, **kwargs):
    print(args)
    print(kwargs)
show(1, 2, x=3, y=4)
  • 🟢 A. (1, 2) and {'x': 3, 'y': 4}
  • 🔵 B. [1, 2] and {'x': 3, 'y': 4}
  • 🟠 C. (1, 2, 'x', 'y') and {}
  • 🔴 D. () and {'1': 2, 'x': 3, 'y': 4}
Answer

(1, 2) and {'x': 3, 'y': 4}

`*args` captures positional arguments into a tuple, `**kwargs` captures keyword arguments into a dictionary.


Q5. What is the correct order of parameters in a Python function definition when using `*args` and `**kwargs`?

  • 🟢 A. Regular arguments → *args → **kwargs
  • 🔵 B. *args → Regular arguments → **kwargs
  • 🟠 C. **kwargs → *args → Regular arguments
  • 🔴 D. Any order is valid
Answer

Regular arguments → *args → **kwargs

The correct order is: regular parameters first, then *args, then **kwargs.


🔴 Advanced

Q1. What is the output of the following code?

def func(a, *args, **kwargs):
    print(a, args, kwargs)
func(1, 2, 3, x=4, y=5)
  • 🟢 A. 1 (2, 3) {'x': 4, 'y': 5}
  • 🔵 B. 1 [2, 3] {'x': 4, 'y': 5}
  • 🟠 C. 1 (2, 3, 'x', 'y') {}
  • 🔴 D. 1 () {'a': 1, 'x': 4, 'y': 5}
Answer

1 (2, 3) {'x': 4, 'y': 5}

`a` takes the first positional argument, `*args` captures the remaining ones, and `**kwargs` captures the keyword arguments.


Q2. Which function definition is valid?

  • 🟢 A. def func(*args, a, **kwargs): pass
  • 🔵 B. def func(a, *args, **kwargs): pass
  • 🟠 C. def func(**kwargs, *args, a): pass
  • 🔴 D. def func(**kwargs, *args): pass
Answer

def func(a, *args, **kwargs): pass

The correct order is normal arguments, then *args, then **kwargs.


Q3. What will this code print?

def calc(*args):
    return sum(args)
print(calc(1, 2, 3, 4))
  • 🟢 A. 10
  • 🔵 B. (1, 2, 3, 4)
  • 🟠 C. Error
  • 🔴 D. [1, 2, 3, 4]
Answer

10

`*args` collects (1,2,3,4) into a tuple, and sum() adds them to return 10.


Q4. What will this code print?

def merge_dicts(**kwargs):
    return kwargs
print(merge_dicts(a=1, b=2, c=3))
  • 🟢 A. {'a': 1, 'b': 2, 'c': 3}
  • 🔵 B. ['a', 'b', 'c']
  • 🟠 C. (1, 2, 3)
  • 🔴 D. Error
Answer

{'a': 1, 'b': 2, 'c': 3}

`**kwargs` collects keyword arguments into a dictionary.


Q5. What is the output of this code?

def show_args_kwargs(*args, **kwargs):
    return len(args) + len(kwargs)
print(show_args_kwargs(1,2,3,x=4,y=5))
  • 🟢 A. 2
  • 🔵 B. 3
  • 🟠 C. 5
  • 🔴 D. Error
Answer

5

`*args` captures (1,2,3) → length 3, `**kwargs` captures {'x':4,'y':5} → length 2, total = 5.


Q6. What will happen if you pass duplicate keyword arguments to a function with `**kwargs`?

def test(**kwargs):
    print(kwargs)
# test(x=1, x=2)
  • 🟢 A. The last value will overwrite the previous one
  • 🔵 B. Both values will be stored
  • 🟠 C. Python raises a SyntaxError
  • 🔴 D. Python raises a RuntimeError
Answer

Python raises a SyntaxError

In Python, duplicate keyword arguments are not allowed, and defining them results in a SyntaxError.


Explore More Topics

📘 Learn Python

Tutorials, Roadmaps, Bootcamps & Visualization Projects

Python Fundamentals

Flow Control Statements


Python Functions


Fundamentals more ...




🧠 Python Advanced

Object-Oriented Programming in Python (OOP)

More...

🧠 Modules