Functions and Modularity Flashcards
7 cards from real HACKERRANK practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 7 Functions and Modularity flashcards as text
What does the `**kwargs` parameter collect inside a function?
Answer: Extra keyword arguments as a dictionary
`**kwargs` collects all extra keyword arguments into a dictionary, accessible by name inside the function.
Which decorator marks a function to be called with no arguments and caches its result after the first call?
Answer: @functools.lru_cache
`@functools.lru_cache` memoizes function results based on arguments, returning cached values on subsequent identical calls.
What is the correct way to define a function that accepts positional-only arguments in Python 3.8+?
Answer: Use `/` after the positional-only parameters
A `/` in the function signature marks all parameters before it as positional-only, preventing them from being passed as keyword arguments.
What happens when you call `dir()` on a module?
Answer: It returns a list of names defined in the module
`dir(module)` returns a sorted list of names (functions, classes, variables) defined in that module.
What does the `functools.partial` function do?
Answer: Creates a new function with some arguments pre-filled
`functools.partial` creates a new callable with some of the original function's arguments already bound.
What is the result of the following expression? ```python list(filter(lambda x: x % 2 == 0, [1, 2, 3, 4, 5])) ```
Answer: [2, 4]
`filter` keeps only elements for which the lambda returns True; even numbers 2 and 4 pass the condition.
In Python, what is the purpose of `__all__` in a module?
Answer: It controls which names are exported when `from module import *` is used
`__all__` is a list of public names that will be exported when `from module import *` is executed.