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 will the following code print? ```python def add(a, b): return a + b result = add(b=3, a=7) print(result) ```
Answer: 10
Keyword arguments can be passed in any order; Python maps them by name, so a=7 and b=3 gives 10.
Which module provides the `wraps` decorator used to preserve a wrapped function's metadata?
Answer: functools
`functools.wraps` copies `__name__`, `__doc__`, and other attributes from the wrapped function to the wrapper.
What is the output? ```python def greet(name, msg='Hello'): return f'{msg}, {name}!' print(greet('Alice', msg='Hi')) ```
Answer: Hi, Alice!
Passing `msg='Hi'` overrides the default value 'Hello', so the function returns 'Hi, Alice!'.
What does a generator function use instead of `return` to yield values lazily?
Answer: yield
The `yield` statement pauses the function and returns a value to the caller, resuming on the next iteration.
How do you call a function stored in a variable `fn` with arguments unpacked from a list `args`?
Answer: fn(*args)
The `*` operator unpacks a list (or tuple) into positional arguments when calling a function.
What is the scope resolution order Python uses when looking up a variable name?
Answer: Local → Enclosing → Global → Built-in
Python follows LEGB: Local → Enclosing → Global → Built-in when resolving names.
What will this code output? ```python def make_adder(n): return lambda x: x + n add5 = make_adder(5) print(add5(3)) ```
Answer: 8
`make_adder(5)` returns a lambda that adds 5 to its argument; calling it with 3 returns 8.