Hackerrank - HackerRank Python Functions and Modularity Questions and Answers 1 — Questions and Answers
Question 1: What is the primary characteristic of a lambda function in Python?
- It can contain multiple expressions.
- It is an anonymous function. (Correct answer)
- It must be assigned to a variable.
- It uses the 'def' keyword for creation.
Correct 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.
Question 2: 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?
- ['b']
- ['a']
- ['a', 'b'] (Correct answer)
- A syntax error will occur.
Correct 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.
Question 3: Which of the following describes the correct order of scope resolution in Python according to the LEGB rule?
- Global, Enclosing, Local, Built-in
- Local, Enclosing, Global, Built-in (Correct answer)
- Local, Global, Enclosing, Built-in
- Built-in, Global, Enclosing, Local
Correct 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).
Question 4: 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?
- Lambda Functions
- List Comprehensions
- Decorators (Correct answer)
- Conditional Statements
Correct 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.
Question 5: What is the primary advantage of using `import math` over `from math import *` when working with modules in a large Python script?
- It is faster because it only imports the module object once.
- It prevents namespace pollution and improves code readability. (Correct answer)
- It allows you to use math functions without the 'math.' prefix.
- It consumes significantly less memory.
Correct 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.
Question 6: 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?
- Parameters with default values must be placed at the very end, after all non-default parameters. (Correct answer)
- Parameters with default values must be placed at the beginning, before all non-default parameters.
- The order does not matter; default and non-default parameters can be mixed.
- All parameters must have default values if at least one does.
Correct 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`.
What is the primary characteristic of a lambda function in Python?