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

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

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

Creating classes Easy
A. class
B. struct
C. def
D. object

2 Which statement correctly defines an empty class named Student?

Creating classes Easy
A. def Student: pass
B. create Student: pass
C. class Student()
D. class Student: pass

3 Which special method is commonly used to initialize a new object's attributes?

Creating classes Easy
A. __init__()
B. __create__()
C. __main__()
D. __start__()

4 Given class Car: pass, which statement creates an instance of Car?

Creating instance objects Easy
A. car = Car()
B. car = class Car
C. Car = car()
D. new Car = car

5 What is an instance object?

Creating instance objects Easy
A. A function stored in a module
B. An object created from a class
C. A keyword used for inheritance
D. A variable shared by all modules

6 In an instance method, what does the parameter self normally refer to?

Creating instance objects Easy
A. The current instance
B. The parent class
C. The current module
D. The global namespace

7 If student has an attribute named name, how is that attribute accessed?

Accessing attributes Easy
A. student::name
B. student->name
C. student.name
D. student[name]

8 Which statement assigns the value 20 to the age attribute of person?

Accessing attributes Easy
A. age.person = 20
B. person.age = 20
C. person->age = 20
D. person::age = 20

9 Which built-in function retrieves a named attribute from an object?

Accessing attributes Easy
A. delattr()
B. setattr()
C. hasattr()
D. getattr()

10 What does class inheritance allow a child class to do?

Class inheritance Easy
A. Hide all local variables
B. Create only one object
C. Import every installed module
D. Reuse features of a parent class

11 Which syntax defines Dog as a child class of Animal?

Class inheritance Easy
A. class Animal(Dog):
B. class Dog extends Animal:
C. class Dog(Animal):
D. class Dog inherits Animal:

12 In class SavingsAccount(Account):, which class is the parent class?

Class inheritance Easy
A. object()
B. Account
C. class
D. SavingsAccount

13 What is method overriding?

Overriding methods Easy
A. Creating methods outside a class
B. Hiding a method inside a variable
C. Calling one method several times
D. Redefining an inherited method in a child class

14 A parent class and a child class both define display(). Which version is normally called on a child object?

Overriding methods Easy
A. The child class version
B. Both versions automatically
C. The parent class version
D. Neither version automatically

15 Which function is commonly used to call a parent class method from a child class?

Overriding methods Easy
A. super()
B. inherit()
C. parent()
D. base()

16 Which attribute name uses Python's double-underscore convention for data hiding?

Data hiding Easy
A. self.balance
B. self.balance__
C. self.balance
D. self.__balance

17 What is the main purpose of data hiding in a class?

Data hiding Easy
A. To convert every attribute into text
B. To prevent classes from being created
C. To limit direct access to internal data
D. To remove all methods from objects

18 What happens to an attribute named __code inside a class named Product?

Data hiding Easy
A. It becomes a class method
B. It becomes a global variable
C. It is name-mangled by Python
D. It is deleted after initialization

19 Does Python directly support traditional function overloading by defining several functions with the same name and different parameter lists?

Function overloading Easy
A. Yes, Python selects by parameter types
B. Yes, Python selects by return types
C. No, the latest definition replaces earlier ones
D. No, functions cannot accept parameters

20 Which Python feature can let one function accept different numbers of positional arguments?

Function overloading Easy
A. The global keyword
B. The *args parameter
C. The break statement
D. The yield expression

21 What is printed by the following code?

PYTHON
class Counter:
    total = 0

    def __init__(self):
        Counter.total += 1

Counter()
Counter()
c = Counter()
print(c.total)

Creating classes Medium
A. 0
B. 1
C. 3
D. 2

22 Which class definition correctly initializes each Book object with separate title and price attributes?

Creating classes Medium
A. class Book:\n def create(self, title, price):\n title = self.title\n price = self.price
B. class Book:\n def __init__(self, title, price):\n self.title = title\n self.price = price
C. class Book:\n def __init__(self, title, price):\n Book = title\n Book = price
D. class Book:\n def __init__(title, price):\n title.self = title\n price.self = price

23 What is the result of the following code?

PYTHON
class Rectangle:
    def __init__(self, width, height=2):
        self.width = width
        self.height = height

    def area(self):
        return self.width * self.height

r = Rectangle(5)
print(r.area())

Creating classes Medium
A. 5
B. 7
C. 10
D. 2

24 Given the class below, which statement creates a valid instance whose name is 'Asha'?

PYTHON
class Student:
    def __init__(self, name):
        self.name = name

Creating instance objects Medium
A. s = Student.Student('Asha')
B. s = Student(self, 'Asha')
C. s = Student('Asha')
D. s = new Student('Asha')

25 What is printed by the following code?

PYTHON
class Box:
    def __init__(self, value):
        self.value = value

b1 = Box(4)
b2 = b1
b2.value = 9
print(b1.value, b2.value)

Creating instance objects Medium
A. 9 9
B. 4 9
C. 9 4
D. 4 4

26 What does the expression p1 is p2 evaluate to?

PYTHON
class Point:
    def __init__(self, x):
        self.x = x

p1 = Point(3)
p2 = Point(3)

Creating instance objects Medium
A. True, because their attributes match
B. False, because they are separate objects
C. True, because they use the same class
D. False, because x is immutable

27 What is printed by this code?

PYTHON
class Device:
    category = 'electronic'

    def __init__(self, category):
        self.category = category

d = Device('sensor')
print(d.category, Device.category)

Accessing attributes Medium
A. electronic sensor
B. sensor sensor
C. sensor electronic
D. electronic electronic

28 Which expression safely returns the salary attribute of employee, or 0 if that attribute does not exist?

Accessing attributes Medium
A. hasattr(employee, 'salary', 0)
B. employee.get('salary', 0)
C. employee.salary or 0
D. getattr(employee, 'salary', 0)

29 What is printed by the following program?

PYTHON
class Account:
    rate = 5

    def __init__(self):
        self.rate = 7

a = Account()
del a.rate
print(a.rate)

Accessing attributes Medium
A. None
B. AttributeError
C. 7
D. 5

30 What is printed by this code?

PYTHON
class Vehicle:
    wheels = 4

class Bike(Vehicle):
    wheels = 2

class Scooter(Bike):
    pass

print(Scooter.wheels)

Class inheritance Medium
A. 4
B. None
C. AttributeError
D. 2

31 What is the output of the following code?

PYTHON
class Parent:
    def __init__(self, value):
        self.value = value

class Child(Parent):
    def __init__(self, value):
        super().__init__(value * 2)

c = Child(6)
print(c.value)

Class inheritance Medium
A. 12
B. 36
C. 6
D. 8

32 Given class Manager(Employee, Auditor):, which statement best describes where Python first searches for an inherited method called by a Manager object?

Class inheritance Medium
A. It searches according to Manager.__mro__
B. It searches every parent class simultaneously
C. It searches the newest parent class first
D. It searches parent classes alphabetically

33 What is printed by this code?

PYTHON
class Animal:
    def speak(self):
        return 'sound'

class Dog(Animal):
    def speak(self):
        return 'bark'

a = Animal()
d = Dog()
print(a.speak(), d.speak())

Overriding methods Medium
A. sound sound
B. sound bark
C. bark bark
D. bark sound

34 What does PremiumBill.total() return?

PYTHON
class Bill:
    def total(self):
        return 100

class PremiumBill(Bill):
    def total(self):
        return super().total() + 20

b = PremiumBill()

Overriding methods Medium
A. 100
B. 20
C. 80
D. 120

35 Which method definition in Child overrides Parent.process while still reusing the parent's result?

PYTHON
class Parent:
    def process(self, value):
        return value * 2

Overriding methods Medium
A. def process(self, value): return super().process(value) + 1
B. def process(value): return Parent.process(value) + 1
C. def parent_process(self, value): return value * 2 + 1
D. def process(self): return super.process(value) + 1

36 What is the most likely result of directly evaluating v.__speed?

PYTHON
class Vehicle:
    def __init__(self):
        self.__speed = 60

v = Vehicle()

Data hiding Medium
A. It raises AttributeError
B. It raises TypeError
C. It returns 60
D. It returns None

37 Which expression accesses the name-mangled value of __balance from the object a?

PYTHON
class Account:
    def __init__(self):
        self.__balance = 500

a = Account()

Data hiding Medium
A. a._balance
B. a.Account.__balance
C. a._Account__balance
D. a.__balance

38 What is printed by this code?

PYTHON
class Base:
    def __init__(self):
        self.__value = 10

class Child(Base):
    def __init__(self):
        super().__init__()
        self.__value = 20

c = Child()
print(c._Base__value, c._Child__value)

Data hiding Medium
A. 10 10
B. 20 10
C. 20 20
D. 10 20

39 What happens when this code runs?

PYTHON
class Calculator:
    def add(self, a, b):
        return a + b

    def add(self, a, b, c):
        return a + b + c

calc = Calculator()
print(calc.add(2, 3))

Function overloading Medium
A. It prints 5
B. It raises TypeError
C. It prints 6
D. It raises NameError

40 Which implementation most directly supports calls such as area(5) for a square and area(5, 3) for a rectangle?

Function overloading Medium
A. def area(a, b): return a * b if b else a * a
B. def area(a, b=None): return a * a if b is None else a * b
C. def area(*args): return args[0] + args[-1]
D. def area(a): return a * a\ndef area(a, b): return a * b

41 What is printed by the following code?

PYTHON
class A:
    def __new__(cls):
        print("new")
        return object()

    def __init__(self):
        print("init")

a = A()
print(type(a).__name__)

Creating instance objects Hard
A. new, then init, then object
B. new, then A
C. new, then object
D. new, then init, then A

42 What happens when this class definition is executed in Python 3?

PYTHON
class C:
    x = 4
    values = [x * i for i in range(3)]

Creating classes Hard
A. C.values becomes [0, 4, 8]
B. C.values becomes [4, 4, 4]
C. An UnboundLocalError occurs on first access
D. A NameError occurs during class creation

43 What is printed by the following descriptor example?

PYTHON
class D:
    def __get__(self, obj, owner):
        return 10

    def __set__(self, obj, value):
        obj.__dict__["x"] = value * 2

class C:
    x = D()

c = C()
c.x = 3
print(c.__dict__["x"], c.x)

Accessing attributes Hard
A. 3 3
B. 6 10
C. 6 6
D. 3 10

44 What is printed by this code?

PYTHON
class C:
    def __init__(self):
        self.x = 5

    def __getattribute__(self, name):
        if name == "x":
            raise AttributeError(name)
        return object.__getattribute__(self, name)

    def __getattr__(self, name):
        return f"missing:{name}"

print(C().x)

Accessing attributes Hard
A. missing:x
B. None
C. An uncaught AttributeError
D. 5

45 What is printed by this cooperative multiple-inheritance example?

PYTHON
class A:
    def f(self):
        return ["A"]

class B(A):
    def f(self):
        return ["B"] + super().f()

class C(A):
    def f(self):
        return ["C"] + super().f()

class D(B, C):
    def f(self):
        return ["D"] + super().f()

print(D().f())

Class inheritance Hard
A. ['D', 'B', 'C', 'A']
B. ['D', 'B', 'A', 'C', 'A']
C. ['D', 'B', 'A']
D. ['D', 'C', 'A']

46 Given the following hierarchy, which implementation handles D().ping()?

PYTHON
class A:
    def ping(self):
        return "A"

class B(A):
    pass

class C(A):
    def ping(self):
        return "C"

class D(B, C):
    pass

Class inheritance Hard
A. C.ping, returning C
B. A.ping, returning A
C. B.ping, returning B
D. The lookup is ambiguous and raises an error

47 What happens when the final two statements execute?

PYTHON
class Base:
    @classmethod
    def identify(cls):
        return cls.__name__

class Child(Base):
    def identify(self):
        return "instance"

print(Child().identify())
print(Child.identify())

Overriding methods Hard
A. It prints instance, then Child
B. It raises TypeError before printing anything
C. It prints Child, then Child
D. It prints instance, then raises TypeError

48 What is printed by this code?

PYTHON
class Box:
    def __len__(self):
        return 1

b = Box()
b.__len__ = lambda: 99
print(len(b), b.__len__())

Overriding methods Hard
A. 99 99
B. 1 1
C. 1 99
D. 99 1

49 What is printed by the following code using double-underscore names?

PYTHON
class A:
    __value = 1

    def get(self):
        return self.__value

class B(A):
    __value = 2

b = B()
print(b.get(), b._B__value)

Data hiding Hard
A. 1 2
B. 1 1
C. 2 2
D. 2 1

50 What is printed after directly placing x in the instance dictionary?

PYTHON
class C:
    @property
    def x(self):
        return 7

c = C()
c.__dict__["x"] = 20
print(c.x)

Data hiding Hard
A. 20
B. 7
C. None
D. An AttributeError

51 What happens when C().f(10) is evaluated?

PYTHON
class C:
    def f(self, x):
        return x

    def f(self, x, y):
        return x + y

Function overloading Hard
A. It raises TypeError for a missing argument
B. It returns 10 using the first definition
C. It raises SyntaxError during class creation
D. It returns 20 using both definitions

52 What is the runtime result of the final call?

PYTHON
from typing import overload

@overload
def convert(x: int) -> str: ...

@overload
def convert(x: str) -> int: ...

def convert(x):
    return x

print(convert(4))

Function overloading Hard
A. It prints 4 as an integer
B. It raises NotImplementedError
C. It prints 4 as a string
D. It raises a runtime type-checking error

53 What does this singledispatch function print?

PYTHON
from functools import singledispatch

@singledispatch
def describe(value):
    return "object"

@describe.register
def _(value: int):
    return "integer"

print(describe(True))

Function overloading Hard
A. boolean
B. integer
C. object
D. It raises KeyError

54 What is printed when the metaclass modifies each new class namespace?

PYTHON
class Meta(type):
    def __new__(mcls, name, bases, namespace):
        namespace["x"] = namespace.get("x", 0) + 1
        return super().__new__(mcls, name, bases, namespace)

class A(metaclass=Meta):
    x = 4

class B(A):
    pass

print(A.x, B.x)

Creating classes Hard
A. 4 1
B. 5 1
C. 5 5
D. 4 4

55 What happens in this descriptor example?

PYTHON
class Descriptor:
    def __set_name__(self, owner, name):
        self.name = name

    def __get__(self, obj, owner):
        return self.name

class C:
    pass

C.x = Descriptor()
print(C().x)

Accessing attributes Hard
A. It prints None
B. It raises AttributeError
C. It prints x
D. It raises TypeError

56 What is printed when an instance attribute has the same name as this descriptor?

PYTHON
class Descriptor:
    def __get__(self, obj, owner):
        return 10

class C:
    x = Descriptor()

c = C()
c.__dict__["x"] = 30
print(c.x)

Accessing attributes Hard
A. None
B. 30
C. 10
D. An AttributeError

57 What list is printed after cooperative initialization?

PYTHON
class A:
    def __init__(self, log):
        log.append("A")

class B(A):
    def __init__(self, log):
        log.append("B")
        super().__init__(log)

class C(A):
    def __init__(self, log):
        log.append("C")
        super().__init__(log)

class D(B, C):
    pass

log = []
D(log)
print(log)

Class inheritance Hard
A. ['B', 'A', 'C', 'A']
B. ['B', 'C', 'A']
C. ['B', 'A']
D. ['C', 'B', 'A']

58 What is printed when a subclass defines a method with the same private spelling?

PYTHON
class A:
    def __run(self):
        return "A"

    def execute(self):
        return self.__run()

class B(A):
    def __run(self):
        return "B"

print(B().execute())

Data hiding Hard
A. An AttributeError
B. B
C. A
D. None

59 What is printed after the augmented assignment and the ordinary assignment?

PYTHON
class C:
    items = []

a = C()
b = C()
a.items += [1]
b.items = b.items + [2]
print(C.items, a.items, b.items)

Creating instance objects Hard
A. [1] [1] [1, 2]
B. [1, 2] [1, 2] [1, 2]
C. [] [1] [2]
D. [] [1] [1, 2]

60 What happens when the final expression is evaluated?

PYTHON
class Base:
    def value(self):
        return 5

class Child(Base):
    @property
    def value(self):
        return 8

result = Child().value()

Overriding methods Hard
A. result becomes 5
B. result becomes 8
C. An AttributeError is raised
D. A TypeError is raised