Questions: Methods and Attributes

5 questions to test your understanding

Score: 0 / 5
Question 1 Multiple Choice

A student writes this method inside a BankAccount class in Python: `def deposit(amount): balance = balance + amount`. When they call account.deposit(100), they get an error. What are the two problems?

A`deposit` is not a valid method name — it should be called `add_funds` to follow conventions
B`self` is missing from the parameter list, and `balance` should be accessed as `self.balance` to reach the instance attribute
CThe method needs a `return` statement to update the account balance
D`deposit` must be defined outside the class to receive the object as an argument
Question 2 Multiple Choice

Why is calling `account.withdraw(50)` preferable to writing `account.balance -= 50` directly from outside the class?

AThe dot notation syntax requires method calls — direct attribute modification raises a syntax error
BMethods execute faster than direct attribute access due to Python's internal optimizations
CThe method can enforce rules — such as checking for sufficient funds — before modifying the attribute, preventing invalid state
DDirect attribute modification is not allowed in Python; attributes are always read-only from outside the class
Question 3 True / False

A method is fundamentally a function — the key difference is that a method automatically receives a reference to the object it was called on as its first argument.

TTrue
FFalse
Question 4 True / False

In Python, if you define a method without the `self` parameter, the method will still work correctly as long as it doesn't read or write any instance attributes inside it.

TTrue
FFalse
Question 5 Short Answer

What is encapsulation, and how do attributes and methods work together to implement it in a class?

Think about your answer, then reveal below.