Font size
WorksheetsOOPS Part 2
Total questions: 20
Worksheet time: 15mins
Encapsulation in programming means keeping an object's data private so that it can't be changed directly from outside the object. Is this true or false?
True
False
In programming, if a child class does not write its own version of a method, will it use the method from the parent class?
TRUE
FALSE
In Python, you can use the keyword private to hide an attribute from outside access. Is this statement true or false?
class MyClass:
private_var = 10
True
False
In Python, does using del on an object always remove it from memory immediately?
TRUE
FALSE
In programming, does method overriding mean that a child class creates a method with the same name as the parent class, but with different code?
TRUE
FALSE
Which of the following shows how to make a Dog class inherit from an Animal class in Python?
class Dog(Animal):
passclass Animal(Dog):
passclass Dog:
pass
Choose the correct answer.
Suppose you have a Parent class and a Child class, and both have a show_info() method. If you create a Child object and call show_info(), does it run the Child's version of the method?
True
False
Why do we use super().__init__() in a child class in Python?
- A) To call the parent class constructor
- B) To delete the parent class
- C) To create a new method
- D) To end the program
Choose the correct option and write a small code to test your answer.
True or False: In Python, if you create a class B that inherits from class A, and class A has a variable x = 1, then creating an object of B and printing b.x will output 1.
Test it online:class A:
x = 1
class B(A):
pass
b = B()
print(b.x)
(a)
True or False: The following line of code will delete the object my_student from memory.del my_student
(a)
True or False: Running the following code will print Parent to the output.class Parent:
def show(self):
print("Parent")
class Child(Parent):
pass
c = Child()
c.show()
(a)
True or False: In the following code, the output will be B because the who method in class B overrides the one in class A.class A:
def who(self):
print("A")
class B(A):
def who(self):
print("B")
b = B()
b.who()
(a)
Which of the following is NOT a feature of object-oriented programming?
Encapsulation
Inheritance
Compilation
Polymorphism
In Python, which symbol is commonly used to indicate that an attribute is intended to be private?
_ (single underscore)
$ (dollar sign)
# (hash)
@ (at symbol)
True or False: In Python, you can access a parent class's method from a child class using the super() function.
False
True
Which of the following best describes inheritance in object-oriented programming?
Creating new classes from existing ones
Hiding data from outside access
Executing code line by line
Storing data in a database
True or False: In Python, double underscores (e.g., __var) are used to name-mangle attributes to make them harder to access from outside the class.
False
True
In object-oriented programming, what is the main purpose of polymorphism?
To allow objects of different classes to be treated as objects of a common superclass
To store multiple values in a single variable
To execute code faster
To prevent code duplication
Which keyword is used in Python to refer to the parent class when calling its methods from a child class?
super()
parent()
base()
main()
True or False: Polymorphism allows different classes to implement methods with the same name, enabling them to be called in a uniform way.
False
True
