Quiz: Object-Oriented Programming¶
Test your understanding of classes, instances, constructors, inheritance, and Python's OOP model with these questions. Click "Show Answer" to reveal the correct answer and explanation.
1. What is a class in Python?¶
- A function that returns a list of objects
- A blueprint for creating objects, defining their attributes and methods
- A type of variable that stores multiple values
- A special loop used to process groups of items
Show Answer
The correct answer is B. A class is a blueprint or template for creating objects. It defines what data (attributes) and behaviors (methods) every instance of that class will have. You define a class with the class keyword, and then create individual objects (instances) by calling the class like a function.
Concept Tested: class Keyword
2. What is the purpose of __init__() in a class?¶
- To print the object's contents when it is created
- To import the class from another file
- To initialize an instance's attributes when the object is first created
- To delete the object from memory when it is no longer needed
Show Answer
The correct answer is C. __init__() is the constructor — a special method Python calls automatically each time you create a new instance. It sets up the object's initial state by assigning values to attributes with self.attribute_name = value. Without __init__(), the object would have no attributes until you set them manually.
Concept Tested: init() Constructor
3. What does self refer to inside a class method?¶
- The class definition itself
- The most recently created instance of the class
- The specific instance the method is being called on
- A required keyword meaning "this is a method, not a function"
Show Answer
The correct answer is C. self is a reference to the particular instance the method is being called on. When you write fido.bark(), Python passes fido as the self argument automatically. Inside bark(), self.name refers to fido.name. Every method in a class takes self as its first parameter, though Python fills it in automatically — you never pass it yourself.
Concept Tested: self
4. How do you create an instance of a class named Dog?¶
Dog.create("Fido", "Labrador")new Dog("Fido", "Labrador")Dog = instance("Fido", "Labrador")fido = Dog("Fido", "Labrador")
Show Answer
The correct answer is D. You create an instance by calling the class like a function with the arguments that __init__() expects (after self). Python automatically calls __init__() and returns a new instance. Unlike some other languages, Python does not use the new keyword — you just call the class name with parentheses.
Concept Tested: Creating Instances
5. What is dot notation used for with objects?¶
- To add two objects together with the
+operator - To access an object's attributes and call its methods
- To import a specific method from a class
- To check whether one object is equal to another
Show Answer
The correct answer is B. Dot notation (object.attribute or object.method()) is how you access data stored inside an object or call behaviors defined on it. For example, fido.name reads the name attribute, and fido.bark() calls the bark method. You have been using dot notation all along with strings ("hello".upper()), lists (items.append(x)), and the turtle (t.forward(100)).
Concept Tested: Dot Notation
6. What does __str__() control in a class?¶
- How the object is stored in memory
- What Python displays when you use
print(obj)orstr(obj)on the object - How the object compares to other objects with
== - How many instances of the class can be created
Show Answer
The correct answer is B. __str__() defines the human-readable string representation of an object. Without it, print(fido) shows something unhelpful like <__main__.Dog object at 0x10a3b2c>. With __str__() returning f"Dog({self.name}, {self.breed})", print(fido) shows Dog(Fido, Labrador) instead.
Concept Tested: str() Method
7. What is the difference between a class variable and an instance variable?¶
- Class variables are faster to access; instance variables are slower
- A class variable is shared by all instances; an instance variable belongs to one specific object
- Class variables are defined with
self; instance variables are defined withoutself - Class variables can only hold numbers; instance variables can hold any type
Show Answer
The correct answer is B. An instance variable (self.name = value) belongs to one specific object. Changing it on one instance does not affect any other. A class variable is defined in the class body (not inside a method) and is shared by all instances. A common use of class variables is counting how many instances have been created.
Concept Tested: Class vs Instance Variables
8. How is inheritance declared in Python?¶
class Puppy extends Dog:class Puppy inherits Dog:class Puppy(Dog):class Puppy from Dog:
Show Answer
The correct answer is C. Put the parent class name in parentheses after the subclass name: class Puppy(Dog):. The subclass automatically inherits all attributes and methods from the parent. Only Python uses the (ParentClass) syntax for inheritance; other languages use extends or similar keywords.
Concept Tested: Inheritance
9. What does super() do inside a subclass constructor?¶
- Creates an additional instance of the parent class
- Deletes the parent class so only the subclass remains
- Calls the parent class's
__init__()method to set up inherited attributes - Returns a reference to the class at the top of the inheritance chain
Show Answer
The correct answer is C. super().__init__(...) calls the parent class's constructor, so you can run its setup code without rewriting it. This is important because the subclass needs the parent's attributes too. For example, if Animal.__init__ sets self.name and self.sound, calling super().__init__(name, sound) in a subclass handles all of that automatically.
Concept Tested: super()
10. Which naming convention does Python use for class names?¶
snake_case(all lowercase with underscores)ALL_CAPS(all uppercase with underscores)camelCase(first word lowercase, rest capitalized)CamelCase(each word capitalized, no underscores)
Show Answer
The correct answer is D. Python class names use CamelCase (also called PascalCase) — each word starts with a capital letter and there are no underscores: Dog, Student, BankAccount, LinkedList. This is a convention from PEP 8 (Python's style guide). By contrast, variable and function names use snake_case, and constants use ALL_CAPS.
Concept Tested: CamelCase Naming Convention