Classes & Objects · Question 1 · Easy

A Student object is created and one of its attributes is updated. What does this code print?

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

s = Student("Riya", 82)
s.marks += 5
print(s.name, s.marks)
  1. Riya 82
  2. Riya 87
  3. name 87
  4. AttributeError
Answer and explanation

Answer: B. Riya 87

Explanation: __init__ stores the two values on the object. s.marks += 5 then updates that object's marks from 82 to 87.

Next: Inside a method defined in a class, what does the first parameter…

Practise classes & objects

Try a fresh set of classes & objects questions with an explanation after every answer, free and without signing up: Classes & Objects practice. Or sit a full placement mock test.

More classes & objects questions

More python practice

Exceptions & Files · Built-ins & Standard Library · Variables, Types & Operators · Python Strings · Lists & Tuples · Dictionaries & Sets · Conditions & Loops · Functions & Scope

LogicWiz Exams home · All practice topics · Placement mock tests · Daily 10 challenge