Object-Oriented Programming Basics Flashcards
7 cards from real HACKERRANK practice questions. Tap to flip, then mark Knew It or Still Learning โ missed cards come back until you master them.
Read the first 7 Object-Oriented Programming Basics flashcards as text
Which method is automatically called when an object is created from a class?
Answer: __init__
__init__ is the initializer method called automatically after object creation to set up instance attributes.
What does the 'self' parameter in an instance method refer to?
Answer: The current instance of the class
'self' refers to the current instance of the class, allowing access to instance attributes and methods.
What will the following code output? class Dog: def __init__(self, name): self.name = name d = Dog('Rex') print(d.name)
Answer: Rex
d.name accesses the instance attribute 'name' set during __init__, which was given the value 'Rex'.
Which keyword is used to inherit from a parent class in Python?
Answer: The parent class is passed in parentheses
Python uses class ChildClass(ParentClass): syntax โ the parent class name is placed in parentheses after the child class name.
What is a class attribute vs an instance attribute?
Answer: Class attributes are shared by all instances; instance attributes are unique per object
Class attributes are defined at the class level and shared across all instances, while instance attributes are set per object via self.
Which built-in function returns the class (type) of an object?
Answer: type()
type(obj) returns the class of obj; e.g., type(42) returns .
What does 'encapsulation' mean in OOP?
Answer: Data and methods are bundled together, with controlled access
Encapsulation bundles data and the methods that operate on it inside a class, hiding internal details from outside code.