Unit 5 - Practice Quiz

INT108 50 Questions
0 Correct 0 Wrong 50 Left
0/50

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

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

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. create
B. class
C. init
D. object

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. Global variables
D. The parent class

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. 0
B. None
C. 1
D. Error

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

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

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(Parent):
D. class Child inherits 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. Suffix the name with double underscores
D. Prefix the name with double underscores (__)

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

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

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

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

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

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

15 What is a class variable?

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

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 returns the value
C. It raises a SyntaxError
D. It raises an AttributeError

17 Which special method is known as the destructor?

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

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

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

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

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

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

A. Inheritance
B. Encapsulation
C. Overloading
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 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. dir
C. map
D. vars

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

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

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

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

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

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

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

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

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

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

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

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

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

A. Only if the class is empty
B. No, only inside init
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 'object'>
B. String
C. <class 'class'>
D. <class 'type'>

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

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. Keyword arguments
B. The last argument
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. Empty then Empty
B. Empty then 5
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 are shared by all instances
D. They can be accessed via the Class name

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

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

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

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

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

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

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

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

42 Polymorphism in Python is best demonstrated by:

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

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

A. Both
B. Neither
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. 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. dir()
C. help()
D. info()

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

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

47 What is the default return value of init?

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

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

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

49 When using Data Hiding, the data is:

A. Completely inaccessible
B. Deleted
C. Protected from accidental access
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. 12
B. 1
C. None
D. 2