Functions and Modularity Flashcards
6 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 6 Functions and Modularity flashcards as text
What is the primary characteristic of a lambda function in Python?
Answer: It is an anonymous function.
Lambda functions in Python are small, anonymous functions defined with the `lambda` keyword. They are restricted to a single expression and do not require a name, which is why they are called anonymous.
Consider the following Python code snippet: ```python def add_item(item, my_list=[]): my_list.append(item) return my_list list1 = add_item('a') list2 = add_item('b') print(list2) ``` What will be the output?
Answer: ['a', 'b']
In Python, default argument values are evaluated only once when the function is defined, not each time it's called. When a mutable object like a list is used as a default, the same list is used across all calls that don't provide a value for that argument. Therefore, the first call appends 'a' to the list, and the second call appends 'b' to the *same* list.
Which of the following describes the correct order of scope resolution in Python according to the LEGB rule?
Answer: Local, Enclosing, Global, Built-in
Python resolves names using the LEGB rule, which specifies the search order for variables: Local (inside the current function), Enclosing (in the scope of any enclosing functions), Global (at the top-level of the module), and finally Built-in (names pre-assigned in Python).
A developer wants to add logging functionality to several functions without modifying their original code. This logging should occur every time the function is called. Which Python feature is most suitable for this scenario?
Answer: Decorators
Decorators are designed to modify or enhance the behavior of functions or methods without permanently changing their source code. A decorator can wrap a function, allowing you to execute code before and after the wrapped function runs, which is ideal for tasks like logging, timing, or access control.
What is the primary advantage of using `import math` over `from math import *` when working with modules in a large Python script?
Answer: It prevents namespace pollution and improves code readability.
Using `import math` keeps the math functions within the `math` namespace, requiring you to prefix them (e.g., `math.sqrt()`). This makes it clear where the function comes from, improving readability and preventing potential name clashes with other functions or variables in your code. `from math import *` dumps all names from the module into the global namespace, which can lead to confusion and is generally discouraged.
A function is defined with some parameters having default values. Which rule must be followed regarding the order of these parameters in the function definition?
Answer: Parameters with default values must be placed at the very end, after all non-default parameters.
In Python function definitions, all parameters with default values must come after all parameters that do not have default values. Attempting to place a non-default parameter after a default parameter will result in a `SyntaxError`.