What does inheritance allow in object-oriented programming?
Defining methods with the same name but different implementations in different classes.
Creating new classes that inherit properties and methods from existing classes.
Creating objects of one class inside another class.
Restricting access to certain data and methods within a class.
Which concept in OOP emphasizes providing a simplified view of an object, hiding its complexity?
Polymorphism
Encapsulation
Inheritance
Abstraction
What is the primary purpose of decorators in Python?
To create recursive functions.
To define a function within another function.
To change the behavior of a function without modifying its source code.
To improve the performance of a function.
What is the output of the following code snippet?
matrix = [[1, 2, 3], [4, 5, 6]] print(matrix[0][2])
1
3
2
IndexError: list index out of range
In Python, how do you access an instance variable within a class method?
Using the class name followed by the variable name.
Using the 'self' keyword followed by the variable name.
Instance variables cannot be accessed within class methods.
Directly using the variable name.
Which string method is used to determine if a string ends with a specified suffix?
find()
index()
startswith()
endswith()
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 passes a list of arguments, while **kwargs passes a dictionary of arguments.
There is no difference; both *args and **kwargs can be used interchangeably.
*args is used for passing arguments by value, while **kwargs is used for passing arguments by reference.
In a function definition, what is the order in which you should define different types of arguments?
**kwargs, keyword arguments, *args, positional arguments
*args, positional arguments, **kwargs, keyword arguments
positional arguments, keyword arguments, *args, **kwargs
keyword arguments, *args, positional arguments, **kwargs
What is the output of the following Python code:
string = 'Hello, world!' print(string.replace('o', '0', 1))
Hell0, world!
Error
Hell0, w0rld!
Hello, world!
How do you create a custom exception class in Python?
By defining a new class that inherits from the BaseException class or one of its subclasses.
BaseException
By using the define_exception() keyword.
define_exception()
By creating a new instance of the Exception class.
Exception
By using the custom_exception() function.
custom_exception()