Unit 5: Classes and objects; Object oriented programming terminology - Practice Quiz

INT108 — Python Programming 50 Questions
0 Correct 0 Wrong 50 Left
0/50

1 Which keyword is used to define a class in Python?

A. def
B. object
C. class
D. struct

2 What is an object in Python?

A. A reserved keyword
B. A function defined inside a class
C. A built-in data type
D. An instance of a class

3 Which method is automatically called when a new object of a class is created?

A. object
B. create
C. class
D. init

4 What does the 'self' parameter represent in a class method?

A. The current class definition
B. The parent class
C. The instance of the class calling the method
D. Global variables

5 How do you access the attribute 'name' of an object 'obj'?

A. obj::name
B. obj->name
C. obj[name]
D. obj.name

6 Which of the following correctly creates an instance of a class named 'Car'?

A. my_car = Car()
B. my_car = Car
C. my_car = new Car()
D. my_car = create Car()

7 What is the output of the following code?
class A:
def init(self):
self.x = 1
a = A()
print(a.x)

A. 1
B. 0
C. None
D. Error

8 Which feature of OOP allows a class to derive properties from another class?

A. Encapsulation
B. Inheritance
C. Polymorphism
D. Data Hiding

9 What is the correct syntax to create a class 'Child' that inherits from 'Parent'?

A. class Child inherits Parent:
B. class Child extends Parent:
C. class Child : Parent:
D. class Child(Parent):

10 What happens when a method in a child class has the same name as a method in the parent class?

A. Both methods are merged
B. The parent method is called
C. A syntax error occurs
D. The child method overrides the parent method

11 How do you achieve data hiding in Python to make an attribute private?

A. Prefix the name with double underscores (__)
B. Declare it with the 'private' keyword
C. Prefix the name with a single underscore (_)
D. Suffix the name with double underscores

12 Does Python support function overloading (multiple methods with the same name but different arguments) natively?

A. Yes, exactly like Java or C++
B. No, the latest definition overwrites previous ones
C. Yes, but only for constructors
D. No, it causes a syntax error immediately

13 Which function is used to determine if a class is a subclass of another class?

A. isinstance()
B. inherits()
C. issubclass()
D. typeof()

14 What is the purpose of the 'super()' function?

A. To make a variable global
B. To delete an object
C. To initialize a class
D. To call the superclass (parent) methods

15 What is a class variable?

A. A variable defined inside the class but outside any methods
B. A global variable defined outside the class
C. A variable defined inside a method using 'self'
D. A private variable

16 If you try to access a private variable 'secret' from outside the class directly using 'obj.secret', what happens?

A. It returns the value
B. It returns None
C. It raises a SyntaxError
D. It raises an AttributeError

17 Which special method is known as the destructor?

A. del
B. end
C. init
D. destroy

18 What is the output?
class Test:
def init(self):
print('Hello')
t = Test()

A. Nothing
B. Hello
C. Error
D. Test

19 In Python, what is the result of 'isinstance(obj, ClassName)'?

A. Creates a new instance
B. Returns True if obj is an instance of ClassName or its subclass
C. Returns the memory address
D. Returns the class name

20 Which concept implies wrapping data and methods into a single unit?

A. Overloading
B. Encapsulation
C. Inheritance
D. Polymorphism

21 How can you simulate function overloading in Python?

A. Defining multiple functions with same name
B. Using default argument values or *args
C. Importing the overloading module
D. Using the 'overload' keyword

22 What is Multiple Inheritance?

A. A class inherits from multiple parents
B. A class has multiple methods
C. A method has multiple arguments
D. A class has multiple children

23 Which dictionary contains the namespace of a class or object?

A. dir
B. vars
C. map
D. dict

24 What is the output?
class Dog:
legs = 4
d1 = Dog()
d2 = Dog()
d1.legs = 3
print(d1.legs, d2.legs)

A. 4 3
B. 3 4
C. 4 4
D. 3 3

25 Consider 'class A: pass' and 'class B(A): pass'. What is B.bases?

A. ()
B. (A,)
C. (object,)
D. None

26 When overriding a method, how can you ensure the parent class's logic for that method is also executed?

A. It happens automatically
B. Use super().method_name()
C. Use parent.method_name()
D. Copy paste the code

27 What is the built-in string representation method meant for developers/debugging?

A. repr
B. string
C. print
D. str

28 If a class does not define an init method, what happens?

A. Syntax Error
B. Python provides a default constructor
C. It cannot be instantiated
D. The program crashes

29 What implies that a variable or method is 'protected' (convention only) in Python?

A. No prefix
B. Double underscore prefix (__)
C. Single underscore prefix (_)
D. The keyword 'protected'

30 Can you add attributes to an instance object after it has been created?

A. Yes, anytime
B. Only using setter methods
C. Only if the class is empty
D. No, only inside init

31 What is MRO in the context of Python inheritance?

A. Main Runtime Object
B. Method Return Object
C. Method Resolution Order
D. Memory Read Operation

32 Given: 'class A: pass'. What is type(A)?

A. String
B. <class 'class'>
C. <class 'type'>
D. <class 'object'>

33 What is the output?
class P:
def show(self): print('P')
class C(P):
def show(self): print('C')
c = C()
c.show()

A. P
B. PC
C. Error
D. C

34 How do you access a private variable '__hidden' of class 'Test' from outside via name mangling?

A. Test.__hidden
B. instance._Test__hidden
C. instance.hidden
D. Test._Test__hidden

35 Which parameter is not required when calling an instance method, even though it is defined in the method signature?

A. The last argument
B. Keyword arguments
C. None
D. The first argument (self)

36 What is the output?
class X:
def m(self, a=None):
if a: print(a)
else: print('Empty')
x = X()
x.m()
x.m(5)

A. 5 then Empty
B. Error
C. Empty then 5
D. Empty then Empty

37 Which of the following is FALSE about class attributes?

A. They are defined outside init
B. They are shared by all instances
C. Changing them via an instance changes the class variable for everyone
D. They can be accessed via the Class name

38 In class MyClass(object):, what is 'object'?

A. The base class for all classes in Python 3
B. A keyword for instantiation
C. A variable
D. A module

39 What keyword is used to delete a reference to an object?

A. remove
B. delete
C. del
D. free

40 What defines the documentation string (docstring) of a class?

A. The help() method
B. Comments starting with #
C. The doc assignment
D. The first string literal inside the class definition

41 What is the output?
class A:
count = 0
print(A.
count)

A. None
B. False
C. 0
D. AttributeError

42 Polymorphism in Python is best demonstrated by:

A. Creating multiple classes
B. Same method name performing different tasks based on the object
C. Using strict type declaration
D. Hiding data

43 If Class B inherits from Class A, which class is the Superclass?

A. Neither
B. Both
C. Class A
D. Class B

44 What happens if you define def __init__(self, a): and def __init__(self, a, b): in the same class?

A. Both constructors are available
B. Only the second one is available
C. Only the first one is available
D. Syntax Error

45 Which built-in function returns the attributes and methods of an object?

A. help()
B. dir()
C. info()
D. list()

46 Can a class call a method defined in its own class?

A. Only static methods
B. Yes, using self.method_name()
C. Yes, using method_name() directly
D. No, never

47 What is the default return value of init?

A. True
B. None
C. It cannot return anything
D. The object created

48 Which of the following represents an 'Is-A' relationship?

A. Composition
B. Association
C. Inheritance
D. Aggregation

49 When using Data Hiding, the data is:

A. Encrypted in memory
B. Completely inaccessible
C. Deleted
D. Protected from accidental access

50 What is the output?
class A:
def f(self): return 1
class B(A):
def f(self): return 2
obj = B()
print(obj.f())

A. 2
B. 12
C. None
D. 1