What is the primary responsibility of a class in object-oriented design?
A class is responsible for managing its own data and behavior. It encapsulates related properties (attributes) and functions (methods) that operate on that data.
Click to reveal answer
beginner
Explain the term 'behavior' in the context of a class.
Behavior refers to the actions or methods a class can perform. These methods define how the class interacts with data and other objects.
Click to reveal answer
intermediate
Why is it important for a class to have a single responsibility?
Having a single responsibility makes a class easier to understand, maintain, and test. It reduces complexity by focusing on one clear purpose.
Click to reveal answer
intermediate
How does encapsulation relate to class responsibilities?
Encapsulation means hiding the internal details of a class and exposing only what is necessary. This protects the class data and ensures it controls its own behavior.
Click to reveal answer
beginner
Give an example of a class responsibility in a real-life system.
In a banking system, an Account class might have the responsibility to manage the balance, process deposits, and handle withdrawals.
Click to reveal answer
What does a class primarily encapsulate?
AUser interface
BOnly data
COnly behavior
DData and behavior
✗ Incorrect
A class encapsulates both data (attributes) and behavior (methods) that operate on that data.
Why should a class have a single responsibility?
ATo make it easier to maintain and understand
BTo increase complexity
CTo handle multiple unrelated tasks
DTo avoid using methods
✗ Incorrect
Single responsibility helps keep the class focused, making it easier to maintain and understand.
Which of the following best describes 'behavior' of a class?
AThe class name
BThe data it stores
CThe methods it can perform
DThe file where it is saved
✗ Incorrect
Behavior refers to the methods or actions a class can perform.
Encapsulation in a class means:
AHiding internal details and exposing only necessary parts
BMaking all data public
CWriting all code in one method
DIgnoring data protection
✗ Incorrect
Encapsulation protects data by hiding internal details and exposing only what is needed.
Which is an example of a class responsibility?
ADesigning the website layout
BManaging user login in a User class
CSending emails from the server
DStoring images on disk
✗ Incorrect
Managing user login is a clear responsibility of a User class.
Describe what is meant by class responsibilities and how they relate to behavior.
Think about what a class 'owns' and what it 'does'.
You got /4 concepts.
Explain why single responsibility is important for class design and give a simple example.
Imagine a class trying to do too many unrelated things.
You got /3 concepts.
Practice
(1/5)
1. Which of the following best describes the main responsibility of a class in low-level design?
easy
A. To store only data without any methods
B. To group related data and behaviors together
C. To execute the entire program logic
D. To act as a database table
Solution
Step 1: Understand class purpose
A class is designed to bundle data (attributes) and behaviors (methods) that operate on that data.
Step 2: Evaluate options
To group related data and behaviors together correctly states this bundling. Options B, C, and D misunderstand class roles.
Final Answer:
To group related data and behaviors together -> Option B
Quick Check:
Class responsibility = Group data and behavior [OK]
Hint: Classes combine data and methods logically [OK]
Common Mistakes:
Thinking classes only hold data
Confusing classes with entire program logic
Assuming classes are database tables
2. Which of the following is the correct way to define a method inside a class in Python?
easy
A. def methodName():
B. function methodName():
C. method methodName(self):
D. def methodName(self):
Solution
Step 1: Recall Python method syntax
In Python, methods inside classes must include 'self' as the first parameter to access instance data.
Step 2: Check options
def methodName(self): uses 'def' and includes 'self', which is correct. Options B and C use incorrect keywords. def methodName(): misses 'self'.
Final Answer:
def methodName(self): -> Option D
Quick Check:
Python method syntax = def methodName(self): [OK]
Hint: Remember 'self' is first parameter in Python methods [OK]
Common Mistakes:
Omitting 'self' parameter
Using 'function' keyword instead of 'def'
Incorrect method declaration keywords
3. Given the following class definition, what will be the output of print(car.get_speed()) after creating car = Car(50)?
Creating car = Car(50) calls __init__, setting self.speed = 50.
Step 2: Analyze get_speed method
get_speed returns self.speed, which is 50.
Final Answer:
50 -> Option A
Quick Check:
car.get_speed() returns 50 [OK]
Hint: Constructor sets attributes, methods return them [OK]
Common Mistakes:
Confusing method return with attribute name
Expecting method to print instead of return
Forgetting to pass argument to constructor
4. Identify the error in the following class definition:
class Person:
def __init__(self, name):
name = name
def get_name(self):
return self.name
medium
A. The constructor does not assign to self.name
B. get_name method is missing self parameter
C. Class name should be lowercase
D. Constructor should not have parameters
Solution
Step 1: Check constructor assignment
The constructor assigns 'name = name' which only assigns local variable, not instance variable.
Step 2: Check get_name method
get_name returns self.name, but self.name was never set, causing error.
Final Answer:
The constructor does not assign to self.name -> Option A
Quick Check:
Instance variables need 'self.' prefix [OK]
Hint: Always assign instance variables with self. prefix [OK]
Common Mistakes:
Assigning to local variable instead of self attribute
Ignoring self parameter in methods
Misunderstanding naming conventions
5. You are designing a BankAccount class. Which of the following responsibilities should NOT be part of this class to keep it focused and maintainable?
hard
A. Providing methods to deposit and withdraw money
B. Storing account balance and updating it
C. Validating user identity during login
D. Generating account statements
Solution
Step 1: Identify core responsibilities of BankAccount
BankAccount should manage balance and transactions (deposit, withdraw) and possibly generate statements.
Step 2: Analyze user identity validation
User identity validation is a separate concern, better handled by an authentication class, not BankAccount.
Final Answer:
Validating user identity during login -> Option C
Quick Check:
Separate concerns keep classes focused [OK]
Hint: Keep authentication separate from account data [OK]
Common Mistakes:
Mixing unrelated responsibilities in one class
Overloading class with authentication logic
Ignoring single responsibility principle
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.