What is a key difference between instance methods and class methods in Python?
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.
There is no practical difference; both can be used interchangeably.
What is the primary role of the 'self' parameter in Python class methods?
It stores the return value of the method.
It refers to the current instance of the class, allowing access to instance variables and methods.
It's a placeholder that doesn't have any specific purpose.
It refers to the class itself, allowing access to class variables.
What is the primary purpose of using classes in Python?
To create reusable blueprints for objects with shared attributes and behaviors.
To store data in a structured format like dictionaries.
To control program flow and logic.
To define functions for specific tasks.
How do you create a custom exception class in Python?
By using the define_exception() keyword.
define_exception()
By creating a new instance of the Exception class.
Exception
By defining a new class that inherits from the BaseException class or one of its subclasses.
BaseException
By using the custom_exception() function.
custom_exception()
How do you create an instance of a class named 'Dog' in Python?
create Dog()```
my_dog = Dog()```
Dog = new()```
Dog.new()```
What will the following code snippet print?
d = {} d.setdefault('a', []).append(1) print(d['a'])
1
[1]
[]
KeyError: 'a'
What is a closure in Python?
A function that can be accessed from anywhere in the program.
A function that is defined inside another function and has access to the outer function's variables.
A function that returns another function.
A function that takes another function as an argument.
In Python, what is the difference between a class variable and an instance variable?
There is no difference; they are interchangeable terms.
Class variables can only store integers, while instance variables can store any data type.
Class variables are declared inside a method, while instance variables are declared outside methods.
Class variables are shared by all instances of a class, while instance variables are unique to each instance.
What is the purpose of the 'init.py' file in a Python package?
Defines the package's main function
Marks the directory as a Python package
Stores the package's documentation
Contains unit tests for the package
What is the difference between *args and **kwargs in Python?
*args passes a variable number of positional arguments, while **kwargs passes a variable number of keyword arguments.
*args is used for passing arguments by value, while **kwargs is used for passing arguments by reference.
*args passes a list of arguments, while **kwargs passes a dictionary of arguments.
There is no difference; both *args and **kwargs can be used interchangeably.