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

2 Which statement correctly defines an empty class named Student?

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

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

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

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

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

5 What is an instance object?

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

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

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

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. person.age = 20
B. age.person = 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. Create only one object
B. Reuse features of a parent class
C. Hide all local variables
D. Import every installed module

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

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

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

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

13 What is method overriding?

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

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

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

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

Overriding methods Easy
A. parent()
B. inherit()
C. super()
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 prevent classes from being created
B. To remove all methods from objects
C. To convert every attribute into text
D. To limit direct access to internal data

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

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

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. No, functions cannot accept parameters
C. No, the latest definition replaces earlier ones
D. Yes, Python selects by return types

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

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

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. 1
B. 3
C. 0
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__(title, price):\n title.self = title\n price.self = price
C. class Book:\n def __init__(self, title, price):\n self.title = title\n self.price = price
D. class Book:\n def __init__(self, title, price):\n Book = title\n Book = 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. 10
B. 7
C. 5
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('Asha')
B. s = Student.Student('Asha')
C. s = Student(self, '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 4
B. 4 9
C. 4 4
D. 9 9

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 they use the same class
B. True, because their attributes match
C. False, because x is immutable
D. False, because they are separate objects

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 electronic
B. sensor electronic
C. electronic sensor
D. sensor sensor

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

Accessing attributes Medium
A. employee.salary or 0
B. hasattr(employee, 'salary', 0)
C. employee.get('salary', 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. 7
B. AttributeError
C. 5
D. None

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. AttributeError
B. 4
C. 2
D. None

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. 8
B. 12
C. 36
D. 6

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 every parent class simultaneously
B. It searches according to Manager.__mro__
C. It searches parent classes alphabetically
D. It searches the newest parent class first

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 bark
B. bark bark
C. bark sound
D. sound 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. 120
C. 20
D. 80

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): return super.process(value) + 1
B. def parent_process(self, value): return value * 2 + 1
C. def process(self, value): return super().process(value) + 1
D. def process(value): return Parent.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 TypeError
B. It returns None
C. It raises AttributeError
D. It returns 60

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._Account__balance
B. a._balance
C. a.__balance
D. a.Account.__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. 20 20
B. 10 10
C. 20 10
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 6
B. It raises NameError
C. It raises TypeError
D. It prints 5

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): return a * a\ndef area(a, b): return a * b
C. def area(*args): return args[0] + args[-1]
D. def area(a, b=None): return a * a if b is None else 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 object
B. new, then init, then A
C. new, then A
D. new, then init, then object

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 [4, 4, 4]
B. An UnboundLocalError occurs on first access
C. A NameError occurs during class creation
D. C.values becomes [0, 4, 8]

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. 6 6
B. 6 10
C. 3 3
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. 5
C. An uncaught AttributeError
D. None

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', 'A']
B. ['D', 'B', 'C', 'A']
C. ['D', 'B', 'A', 'C', '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. A.ping, returning A
B. The lookup is ambiguous and raises an error
C. C.ping, returning C
D. B.ping, returning B

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 prints Child, then Child
C. It raises TypeError before printing anything
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. 1 1
B. 99 1
C. 1 99
D. 99 99

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. An AttributeError
B. 7
C. 20
D. None

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 raises SyntaxError during class creation
C. It returns 20 using both definitions
D. It returns 10 using the first definition

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 raises a runtime type-checking error
B. It raises NotImplementedError
C. It prints 4 as a string
D. It prints 4 as an integer

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. It raises KeyError
B. boolean
C. object
D. integer

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 5
C. 4 4
D. 5 1

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 raises AttributeError
B. It prints None
C. It raises TypeError
D. It prints x

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. 10
B. 30
C. An AttributeError
D. None

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', 'A']
C. ['B', 'C', '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. None
B. A
C. An AttributeError
D. B

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, 2]
B. [1] [1] [1, 2]
C. [1, 2] [1, 2] [1, 2]
D. [] [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. A TypeError is raised
C. result becomes 8
D. An AttributeError is raised