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. struct
B. class
C. object
D. def

2 What is an object in Python?

A. An instance of a class
B. A reserved keyword
C. A function defined inside a class
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. Global variables
B. The parent class
C. The instance of the class calling the method
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 = 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. None
B. 1
C. 0
D. Error

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

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

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. Both methods are merged
D. A syntax error occurs

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

A. Prefix the name with double underscores (__)
B. Suffix the name with double underscores
C. Declare it with the 'private' keyword
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. No, it causes a syntax error immediately
D. Yes, exactly like Java or C++

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 make a variable global
B. To initialize a class
C. To delete an object
D. To call the superclass (parent) methods

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

17 Which special method is known as the destructor?

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

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

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

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

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

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

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

21 How can you simulate function overloading in Python?

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

22 What is Multiple Inheritance?

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

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

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

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. repr
B. print
C. str
D. string

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

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

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

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

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

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

31 What is MRO in the context of Python inheritance?

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

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

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

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

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

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 can be accessed via the Class name
B. They are defined outside init
C. They are shared by all instances
D. Changing them via an instance changes the class variable for everyone

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

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

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. Comments starting with #
B. The doc assignment
C. The help() method
D. The first string literal inside the class definition

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

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

42 Polymorphism in Python is best demonstrated by:

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

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

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

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

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

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

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

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

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

47 What is the default return value of init?

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

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

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

49 When using Data Hiding, the data is:

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