What is a key difference between instance methods and class methods in Python?
There is no practical difference; both can be used interchangeably.
Instance methods operate on specific instances of a class, while class methods operate on the class itself.
Instance methods are defined using the 'classmethod' decorator, while class methods use the 'staticmethod' decorator.
Class methods can access instance variables, while instance methods cannot access class variables.
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)
[8, 10, 12]
[2, 4, 6, 8, 10, 12]
[1, 2, 3, 4, 5, 6]
[4, 5, 6]
Which of the following is NOT a built-in exception in Python?
IndexError
FileError
TypeError
ValueError
What is a key difference between OrderedDict and a regular dictionary in Python?
OrderedDict
OrderedDict supports list comprehensions for creating dictionaries, while regular dictionaries do not.
OrderedDict is mutable, while regular dictionaries are immutable.
OrderedDict allows duplicate keys, while regular dictionaries do not.
OrderedDict preserves the order of key insertion, while regular dictionaries do not guarantee order.
Which module would you use to interact with the operating system, like creating directories?
datetime
os
math
sys
What is the purpose of the OrderedDict class in Python?
It provides a dictionary that remembers the order in which keys were inserted.
It prevents duplicate keys from being added to a dictionary.
It allows you to create dictionaries with keys as immutable data types only.
It optimizes dictionary lookups for faster retrieval of values.
What does inheritance allow in object-oriented programming?
Creating new classes that inherit properties and methods from existing classes.
Restricting access to certain data and methods within a class.
Creating objects of one class inside another class.
Defining methods with the same name but different implementations in different classes.
Consider the function definition: def my_func(a, b=5, *args, **kwargs): .... What does *args achieve?
def my_func(a, b=5, *args, **kwargs): ...
*args
It collects any number of keyword arguments into a dictionary.
It enforces that the function must be called with at least two arguments.
It defines a variable number of default arguments.
It collects any number of positional arguments into a tuple.
Which method is used to write a single line of text to a file in Python?
writeline()
write()
put()
writeln()
What is the primary purpose of using inheritance in Python's Object-Oriented Programming?
To enhance data security within a program
To improve code execution speed
To reduce code duplication and promote reusability
To enforce strict data type checking