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.82Medium
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.83Medium
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.
Q.84Medium
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.85Medium
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
Advertisement
Q.86Medium
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.87Medium
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.88Medium
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.89Medium
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.90Medium
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.91Medium
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.92Medium
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.93Medium
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.94Medium
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.95Medium
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.96Medium
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.97Medium
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.98Medium
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.
Q.99Medium
Which of the following Python data structures does NOT allow duplicate elements?
Answer: C
A set in Python is an unordered collection that automatically discards duplicate values. Lists, tuples, and deques all allow duplicate elements. For example, {1, 2, 2, 3} evaluates to {1, 2, 3}.
Q.100Medium
What will be the output of the following code?
```python
stack = []
stack.append(10)
stack.append(20)
stack.append(30)
stack.pop()
print(stack)
```
Answer: B
A Python list used as a stack follows the LIFO (Last In, First Out) principle. After appending 10, 20, and 30, the list is [10, 20, 30]. Calling pop() removes the last element (30), leaving [10, 20].