PCAP Functions and Modules 1 — Questions and Answers
Question 1: What does the `*args` parameter in a function definition allow?
- Keyword arguments only
- A variable number of positional arguments (Correct answer)
- Default argument values
- A single list argument
Correct answer: A variable number of positional arguments
`*args` collects extra positional arguments into a tuple inside the function.
Question 2: What does `**kwargs` allow in a function definition?
- Variable positional arguments
- Variable keyword arguments as a dictionary (Correct answer)
- Default arguments
- Forced keyword-only arguments
Correct answer: Variable keyword arguments as a dictionary
`**kwargs` collects extra keyword arguments into a dictionary inside the function.
Question 3: What is a lambda function in Python?
- A named multi-line function
- An anonymous single-expression function (Correct answer)
- A function stored in a module
- A built-in function
Correct answer: An anonymous single-expression function
A lambda is an anonymous function defined with the `lambda` keyword that contains a single expression.
Question 4: What does the `return` statement do in a function?
- Pauses the function
- Exits the function and optionally passes a value back (Correct answer)
- Prints the result
- Restarts the function
Correct answer: Exits the function and optionally passes a value back
`return` ends function execution and sends a value back to the caller.
Question 5: Which built-in function applies a function to every item in an iterable?
- filter()
- apply()
- map() (Correct answer)
- each()
Correct answer: map()
`map()` applies a function to each element of an iterable and returns a map object.
Question 6: How do you import only the `sqrt` function from the `math` module?
- import math.sqrt
- from math import sqrt (Correct answer)
- include math.sqrt
- import sqrt from math
Correct answer: from math import sqrt
The `from module import name` syntax imports a specific name from a module.
What does the `*args` parameter in a function definition allow?