Which of the following statements about lambda functions in Python is CORRECT?
Answer: B
A lambda function in Python is an anonymous function defined with the lambda keyword. It can accept multiple arguments but its body can only be a single expression (not multiple statements). It does not use a return statement; the expression is implicitly returned. Lambda functions can also be used inline without assigning to a variable.
Q.142Medium
What will be the output of the following code?
def outer():
x = 10
def inner():
nonlocal x
x += 5
inner()
return x
print(outer())
Answer: C
The nonlocal keyword allows the inner function to modify the variable x from the enclosing (outer) function's scope. Initially x = 10. After inner() runs, x becomes 10 + 5 = 15. outer() then returns 15, so the output is 15.
Q.143Medium
What is the purpose of the __init__.py file in a Python package directory?
Answer: B
__init__.py marks a directory as a Python package, making it importable. When a package is imported, Python executes __init__.py. It can be empty or contain initialization code, define __all__ to control exports, or import submodules. Without it (in Python 2 and some Python 3 contexts), the directory would not be recognized as a package.
Q.144Medium
What will be the output of the following code?
def add(a, b):
return a + b
result = add
print(result(3, 4))
Answer: D
In Python, functions are first-class objects. Assigning result = add makes result reference the same function object as add. Calling result(3, 4) is identical to calling add(3, 4), which returns 3 + 4 = 7. So the output is 7.
Q.145Medium
Which of the following correctly imports only the sqrt function from the math module?
Answer: B
The correct syntax to import a specific name from a module is: from module_name import name. So from math import sqrt imports only the sqrt function into the current namespace, allowing it to be used directly as sqrt() without the math. prefix. The other options are syntactically incorrect.
Advertisement
Q.146Medium
What will be the output of the following code?
def counter():
count = 0
def increment():
nonlocal count
count += 1
return count
return increment
c = counter()
print(c())
print(c())
print(c())
Answer: C
counter() returns the inner function increment, which is a closure that retains access to the count variable in the enclosing scope. Each call to c() increments count by 1 and returns the new value. First call: count becomes 1, returns 1. Second call: count becomes 2, returns 2. Third call: count becomes 3, returns 3. Output: 1, 2, 3.
Q.147Medium
What will be the output of the following code?
import math as m
print(m.floor(4.7))
print(m.ceil(4.2))
Answer: C
math.floor(4.7) returns the largest integer less than or equal to 4.7, which is 4. math.ceil(4.2) returns the smallest integer greater than or equal to 4.2, which is 5. The module is aliased as m using import math as m. So the output is:
4
5
Q.148Medium
What will be the output of the following code?
def apply(func, value):
return func(value)
result = apply(lambda x: x ** 2, 5)
print(result)
Answer: B
The apply() function takes a function and a value as arguments and returns func(value). A lambda function lambda x: x 2 is passed as func and 5 as value. Inside apply(), func(5) evaluates to 5 2 = 25. The result is 25, which is printed.
Q.149Medium
What does the regular expression pattern `^\d{3}-\d{4}$` match?
Answer: B
The `^` anchor asserts the start of the string and `$` asserts the end. `\d{3}` matches exactly three digits, `-` matches a literal hyphen, and `\d{4}` matches exactly four digits. Together, the pattern matches a complete string of the form `123-4567` and nothing else.
Q.150Medium
Which of the following `re` module functions returns all non-overlapping matches of a pattern in a string as a list of strings?
Answer: C
`re.findall()` scans the string from left to right and returns all non-overlapping matches as a list. `re.match()` only checks at the beginning, `re.search()` returns only the first match object, and `re.fullmatch()` requires the entire string to match.
Q.151Medium
What is the output of the following code?
```python
import re
result = re.sub(r'\s+', '-', 'Hello World Python')
print(result)
```
Answer: A
`re.sub(r'\s+', '-', ...)` replaces every sequence of one or more whitespace characters with a single hyphen. `'Hello World Python'` has two groups of spaces, so both are replaced by `-`, giving `Hello-World-Python`.
Q.152Medium
What does the `re.IGNORECASE` (or `re.I`) flag do when used in a regular expression?
Answer: B
`re.IGNORECASE` makes the regex engine perform case-insensitive matching, so patterns like `[a-z]` will also match uppercase letters. The flag that makes `.` match newlines is `re.DOTALL`, and `re.VERBOSE` allows whitespace/comments in patterns.
Q.153Medium
What is the difference between `re.match()` and `re.search()` in Python?
Answer: B
`re.match()` only attempts to match the pattern at the start of the string (position 0), while `re.search()` scans through the entire string looking for any location where the pattern matches. Both return a match object on success and `None` on failure.
Q.154Medium
What will the following code print?
```python
import re
pattern = r'(\w+)@(\w+)\.com'
m = re.search(pattern, 'Contact: user@example.com today')
if m:
print(m.group(1), m.group(2))
```
Answer: B
The pattern has two capturing groups: `(\w+)` before `@` and `(\w+)` before `.com`. `m.group(1)` returns the first captured group `user` and `m.group(2)` returns the second captured group `example`. So the output is `user example`.
Q.155Medium
Which regular expression pattern correctly matches a string that contains ONLY alphabetic characters (no digits, spaces, or special characters)?
Answer: A
`^[a-zA-Z]+$` uses `+` to require at least one character and `[a-zA-Z]` to allow only letters. Option B uses `*` which also matches an empty string. Option C (`\w`) also matches digits and underscores. Option D only matches lowercase letters.
Q.156Medium
What does the `?` quantifier mean when used in a regular expression like `colou?r`?
Answer: C
The `?` quantifier makes the preceding element optional, matching it zero or one time. So `colou?r` matches both `color` (without `u`) and `colour` (with `u`). It does not mean zero-or-more (`*`) or one-or-more (`+`).
Q.157Medium
What will the following code output?
```python
import re
text = 'cat bat sat'
result = re.findall(r'[cbr]at', text)
print(result)
```
Answer: B
The character class `[cbr]` matches only `c`, `b`, or `r`. So `cat` (matches `c`) and `bat` (matches `b`) are found. `sat` does not match because `s` is not in `[cbr]`. The result is `['cat', 'bat']`.
Q.158Medium
What is the purpose of using a raw string (prefix `r`) for regular expression patterns in Python, such as `r'\d+'`?
Answer: B
In a regular Python string, `\d` would be interpreted as an escape sequence (which is just `d` since `\d` is not a recognised Python escape). Using a raw string `r'\d+'` ensures the backslash is passed literally to the regex engine, which then correctly interprets `\d` as the digit shorthand. This avoids the need to double-escape patterns like `'\\d+'`.
Q.159Medium
What is the time complexity of accessing an element by index in a Python list?
Answer: C
Python lists are implemented as dynamic arrays. Accessing an element by index is a constant-time operation O(1) because the memory address of any element can be computed directly using the base address and the index.
Q.160Medium
What will be the output of the following code?
```python
d = {'a': 1, 'b': 2, 'c': 3}
print(list(d.keys()))
print(list(d.values()))
```
Answer: A
From Python 3.7 onwards, dictionaries maintain insertion order. The keys() method returns a view of keys and values() returns a view of values. Wrapping them in list() converts them to lists, producing ['a', 'b', 'c'] and [1, 2, 3] respectively.