WorksheetsOOP in Python
Total questions: 15
Worksheet time: 11mins
Fill in the blank to correctly define an initialization method for a class named Book that takes two parameters (besides self): title and author, and assigns them to instance variables.
(a)
What does an object consist of?
Methods
Attributes
Procedures
Variables
The ability of a programming language to process objects differently depending on their class is called ...
Encapsulation
Inheritance
Polymorphism
Instantiation
Attributes
You can instantiate an object more than once?
True
False
Which of the following statements correctly creates an instance of a class named Vehicle in Python?
class Vehicle: new_vehicle = Vehicle()
new_vehicle = Vehicle.class()
new_vehicle = Vehicle()
Vehicle new_vehicle = Vehicle()
Which of the following is true about inheritance in Python?
Inheritance allows a class to inherit attributes and methods only from a single parent class.
Inheritance allows a class to inherit attributes and methods from multiple parent classes, known as multiple inheritance.
Inheritance is a mechanism where a class can copy methods from modules and packages.
Inheritance is not supported in Python due to its dynamic nature.
Fill in the blank to complete the definition related to the Open/Closed Principle of SOLID design principles:
"The Open/Closed Principle states that software entities (like classes, modules, functions, etc.) should be open for (a) , but closed for modification."
How is encapsulation typically achieved in Python?
By declaring all variables within a class as private using the private keyword.
By prefixing the name of a variable or method with a double underscore (__) to make it private.
By using the @property decorator to strictly control access to class attributes.
By making all methods static.
Which of the following statements accurately describe design patterns in Python?
The Singleton pattern ensures a class has only one instance and provides a global point of access to that instance.
The Builder pattern is used to separate the construction of a complex object from its representation, allowing the same construction process to create different representations.
The Decorator pattern allows for the addition of new functionality to an existing object without modifying its structure.
The Singleton pattern is primarily used to impose strict type-checking mechanisms in Python, similar to those found in statically typed languages.
Which SOLID principle states that "objects in a program should be replaceable with instances of their subtypes without altering the correctness of the program"?
Single Responsibility Principle (SRP)
Open/Closed Principle (OCP)
Liskov Substitution Principle (LSP)
Dependency Inversion Principle (DIP)
What distinguishes composition from aggregation in OOP?
Composition requires child objects to be exclusively owned by a single parent object.
Only aggregation allows for shared ownership of child objects.
In composition, child objects can exist independently of the parent.
Aggregation represents a "is-a" relationship between objects.
Inheritance in Python allows a new class, known as the (a) class, to take on the attributes and methods of an existing class, known as the parent class
Which statement accurately describes an abstract class in object-oriented programming?
An abstract class can be instantiated and used to create objects.
An abstract class is a template that defines methods and properties, which can be completely implemented.
An abstract class is designed to be a base class that contains at least one abstract method and cannot be instantiated.
All methods in an abstract class are fully implemented and can be directly used by the subclasses.
A completely abstract class, which has only abstract methods, is called a(n) (a) .
