What is a key difference between instance methods and class methods in Python?
Instance methods are defined using the 'classmethod' decorator, while class methods use the 'staticmethod' decorator.
Instance methods operate on specific instances of a class, while class methods operate on the class itself.
Class methods can access instance variables, while instance methods cannot access class variables.
There is no practical difference; both can be used interchangeably.
What is the output of the following code snippet?
data = [[1, 2, 3], [4, 5, 6]] result = [x * 2 for x in data[1]] print(result)
[2, 4, 6, 8, 10, 12]
[1, 2, 3, 4, 5, 6]
[4, 5, 6]
[8, 10, 12]
What is the most efficient way to concatenate a large number of strings in Python?
Using the '+' operator.
Using the '%s' operator.
Using the ''.join() method.
It depends on the size of the strings.
Which statement correctly imports the 'math' module in Python?
require math
import math
using math
include math
What is the primary advantage of using a list comprehension over a traditional for loop when creating a new list in Python?
List comprehensions can be used with any iterable, while for loops are limited to lists.
List comprehensions allow you to modify existing lists, while for loops can only create new ones.
List comprehensions are more readable and concise for simple list creations.
List comprehensions offer significant performance improvements, especially for large lists.
What does the following decorator do?
def my_decorator(func): def wrapper(*args, **kwargs): print('Something is happening before the function is called.') result = func(*args, **kwargs) print('Something is happening after the function is called.') return result return wrapper @my_decorator def say_hello(): print('Hello!')
It adds extra print statements before and after the execution of say_hello.
say_hello
It modifies the arguments passed to say_hello.
It changes the return value of say_hello.
It prevents say_hello from being called.
What is the primary purpose of decorators in Python?
To change the behavior of a function without modifying its source code.
To create recursive functions.
To define a function within another function.
To improve the performance of a function.
What is the purpose of the OrderedDict class in Python?
OrderedDict
It optimizes dictionary lookups for faster retrieval of values.
It allows you to create dictionaries with keys as immutable data types only.
It provides a dictionary that remembers the order in which keys were inserted.
It prevents duplicate keys from being added to a dictionary.
What is the output of the following Python code?
with open('data.txt', 'w') as file: file.write('Hello, world!') print(file.read())
The code will print nothing.
The code will raise an IOError.
Hello, world!
The code will raise a ValueError.
What does the 're.findall()' function return?
A boolean value indicating whether the pattern is present in the string.
The first match of the pattern in the string.
None of the above.
A list of all non-overlapping matches of the pattern in the string.