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 built-in data type
B. An instance of a class
C. A reserved keyword
D. A function defined inside a class

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

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

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

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

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 = new Car()
B. my_car = Car
C. my_car = create Car()
D. my_car = Car()

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

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

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

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

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

A. class Child : Parent:
B. class Child extends Parent:
C. class Child inherits 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. A syntax error occurs
C. The parent method is called
D. The child method overrides the parent method

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

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

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

A. Yes, but only for constructors
B. No, the latest definition overwrites previous ones
C. Yes, exactly like Java or C++
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. inherits()
B. issubclass()
C. isinstance()
D. typeof()

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

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

15 What is a class variable?

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

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

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

17 Which special method is known as the destructor?

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

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

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

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

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

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

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

21 How can you simulate function overloading in Python?

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

22 What is Multiple Inheritance?

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

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

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

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

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

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

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

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. Copy paste the code
C. Use parent.method_name()
D. Use super().method_name()

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

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

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

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

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

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

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

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

31 What is MRO in the context of Python inheritance?

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

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

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

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. Error
C. C
D. PC

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

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

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

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

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. Empty then 5
B. Empty then Empty
C. Error
D. 5 then Empty

37 Which of the following is FALSE about class attributes?

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

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

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

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

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

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. AttributeError
B. None
C. False
D. 0

42 Polymorphism in Python is best demonstrated by:

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

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

A. Both
B. Class A
C. Neither
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. Syntax Error
C. Only the second one is available
D. Only the first one is available

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

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

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

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

47 What is the default return value of init?

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

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

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

49 When using Data Hiding, the data is:

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

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. 1
D. None