What is a key difference between instance methods and class methods in Python?
Class methods can access instance variables, while instance methods cannot access class variables.
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.
There is no practical difference; both can be used interchangeably.
What is the primary purpose of using inheritance in Python's Object-Oriented Programming?
To reduce code duplication and promote reusability
To enhance data security within a program
To enforce strict data type checking
To improve code execution speed
What is the output of the following code snippet?
matrix = [[1, 2, 3], [4, 5, 6]] print(matrix[0][2])
IndexError: list index out of range
1
3
2
In Python, what is the difference between a class variable and an instance variable?
Class variables are declared inside a method, while instance variables are declared outside methods.
There is no difference; they are interchangeable terms.
Class variables are shared by all instances of a class, while instance variables are unique to each instance.
Class variables can only store integers, while instance variables can store any data type.
In Python, how do you access an instance variable within a class method?
Using the 'self' keyword followed by the variable name.
Instance variables cannot be accessed within class methods.
Using the class name followed by the variable name.
Directly using the variable name.
How can you get the current date and time using the 'datetime' module?
datetime.datetime.now()
datetime.now
datetime.currentTime()
datetime.today()
What is the primary purpose of using classes in Python?
To control program flow and logic.
To create reusable blueprints for objects with shared attributes and behaviors.
To define functions for specific tasks.
To store data in a structured format like dictionaries.
What does the csv.reader() function in Python return?
csv.reader()
A list of dictionaries.
A list of lists, representing rows and cells.
A list of strings.
A string containing the entire CSV file content.
What does the 'sys.argv' list in Python store?
System error messages
Command line arguments passed to the script
Contents of the current working directory
Environment variables
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 modifies the arguments passed to say_hello.
say_hello
It prevents say_hello from being called.
It changes the return value of say_hello.
It adds extra print statements before and after the execution of say_hello.