Unit 5 - Practice Quiz

INT108

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

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

2 What is an object in Python?

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

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

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

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

A. The current class definition
B. The instance of the class calling the method
C. The parent class
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 = new 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.
B. 1
C. None
D. Error

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

A. Polymorphism
B. Encapsulation
C. Inheritance
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(Parent):
C. class Child extends 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. The parent method is called
B. The child method overrides the parent method
C. A syntax error occurs
D. Both methods are merged

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

A. Declare it with the 'private' keyword
B. Prefix the name with a single underscore (_)
C. Prefix the name with double underscores (__)
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. typeof()
C. issubclass()
D. inherits()

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

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

15 What is a class variable?

A. A variable defined inside a method using 'self'
B. A variable defined inside the class but outside any methods
C. A global variable defined outside the class
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 an AttributeError
D. It raises a SyntaxError

17 Which special method is known as the destructor?

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

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

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

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

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

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

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

21 How can you simulate function overloading in Python?

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

22 What is Multiple Inheritance?

A. A class inherits from multiple parents
B. A class has multiple children
C. A class has multiple methods
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 3
C. 3 4
D. 4 3

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

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

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

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

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

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

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

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

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

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

31 What is MRO in the context of Python inheritance?

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

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

A. <class 'object'>
B. <class 'type'>
C. <class 'class'>
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. C
C. PC
D. Error

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

A. Test.__hidden
B. Test._Test__hidden
C. instance._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 last argument
B. The first argument (self)
C. Keyword arguments
D. None

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

37 Which of the following is FALSE about class attributes?

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

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

A. A variable
B. The base class for all classes in Python 3
C. A keyword for instantiation
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 first string literal inside the class definition
B. The doc assignment
C. Comments starting with #
D. The help() method

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

A.
B. None
C. AttributeError
D. False

42 Polymorphism in Python is best demonstrated by:

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

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

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

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

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

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

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

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

47 What is the default return value of init?

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

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

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

49 When using Data Hiding, the data is:

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

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. 1
B. 2
C. 12
D. None