OOP – Methods

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

OOP – Methods

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What is a method?
A A standalone function
B A function defined inside a class
2. What must be the first parameter of a method?
A self
B this
3. dog.bark() do you pass self?
A No Python passes it automatically
B Yes
4. Can methods access self.name?
A No
B Yes that's how they use object data
5. Can methods return values?
A Yes just like regular functions
B No methods can't return

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1def bark(): AOk: Access instance attribute in a method
2dog.Bark() BValid: Method with required self
3def bark(self): COutput: HELLO
4self.name DError: Method names are case-sensitive
5'hello'.upper() EOutput: 2
6[1,2,2].count(2) FError: Missing self parameter

Definitions

Method

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
class Dog: def bark(____): return 'Woof!'
2.
class Dog: def bark(self): return ____.name + ' says Woof!'
3.
dog = Dog('Rex') print(dog.____( ))
4.
class Counter: def ________(self): self.count = 0 def increment(self): self.count __ 1
5.
class Circle: def area(self): ______ 3.14 * self.radius ** 2

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A class Dog:
B def bark():
C return 'Woof!'
Question 2
A class Dog:
B def __init__(self, name):
C self.name = name
D def bark(self):
E return name + ' says Woof!'
Question 3
A dog = Dog('Rex')
B dog.bark
Question 4
A class Dog:
B def bark(self):
C return 'Woof!'
D
E Dog.bark()
Question 5
A class Dog:
B def bark(self):
C return 'Woof!'
D
E dog = Dog()
F print(dog.bark(self))

Answer Key

OOP – Methods
Section A: Quick Questions
Q1
B — A function defined inside a class
Q2
A — self
Q3
A — No Python passes it automatically
Q4
B — Yes that's how they use object data
Q5
A — Yes just like regular functions
Section B: Match the Pairs
Q1
def bark(): → Error: Missing self parameter
Q2
dog.Bark() → Error: Method names are case-sensitive
Q3
def bark(self): → Valid: Method with required self
Q4
self.name → Ok: Access instance attribute in a method
Q5
'hello'.upper() → Output: HELLO
Q6
[1,2,2].count(2) → Output: 2
Section C: Definitions
Q1
Method — A function defined inside a class.~~self
Section D: Fill in the Blanks
Q1
class Dog: def bark(self): return 'Woof!'
Q2
class Dog: def bark(self): return self.name + ' says Woof!'
Q3
dog = Dog('Rex') print(dog.bark( ))
Q4
class Counter: def __init__(self): self.count = 0 def increment(self): self.count += 1
Q5
class Circle: def area(self): return 3.14 * self.radius ** 2
Section E: Spot the Error
Q1
B — Missing self parameter every method needs self as first parameter
Q2
E — Use self.name inside methods name alone is not defined here
Q3
B — Missing parentheses bark is a method, call it with ()
Q4
E — Call methods on objects, not the class. Create an object first.
Q5
F — Don't pass self when calling Python fills it in automatically