Why programmers use object-oriented programming

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

Why programmers use object-oriented programming

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What does object-oriented programming bundle together inside an object?
A Related data and the code that uses it
B Only a list of numbers
2. Which is a good way to model a real-world pet in OOP?
A Separate unrelated variables scattered across the file
B An object with name and age properties and a speak() method
3. Why do programmers write a class?
A To reuse one blueprint to make many objects
B To make each object from scratch every time
4. A game has 50 enemies that all behave the same way. What is the OOP approach?
A Copy and paste the same code 50 times
B Write one Enemy class and create 50 objects
5. How does keeping related code together help in a large program?
A It is easier to find, understand and fix the code for one thing
B It makes the program run faster on every computer

Match the Pairs

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

Left columnRight column
1class Player { } AA blueprint that can be reused to make many player objects
2const hero = new Player('Kai', 100); Bclass Pet { constructor(name) { this.name = name; this.energy = 5; } eat() { this.energy++; } }
3Modelling a real-world thing: a pet has data (name, energy) and actions (eat) kept together in one object CCreates one object from the Player blueprint with its own data
4Reusing a blueprint: one class makes many objects, so you do not rewrite the same code DThe same code is copied for each kind of thing instead of reusing a blueprint. Put the behaviour in a class method so every object can use it.
5let petName = 'Rex'; let petEnergy = 5; let petName2 = 'Mia'; let petEnergy2 = 8; ERelated data is stored in loose separate variables, so it is not kept together and gets messy as the program grows. Use a Pet class and make one object per pet.
6function feedPet(name, energy) { return energy + 1; } // copied again as feedDog, feedCat... Fconst a = new Pet('Rex'); const b = new Pet('Mia');

Definitions

OOP

Fill in the Blanks

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

1.
_____ Player { constructor(name) { this.name = name; } }
2.
const a = ___ Player("Ava"); const b = new Player("Liam");
3.
class Car { constructor(speed) { ____.speed = speed; } }
4.
class Pet { ___________(name) { this.name = name; } }
5.
const ava = new Player("Ava"); ava.points = 0; console.log(ava.______);

Spot the Error

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

Question 1
A class Dog {
B constructer(name) {
C this.name = name;
D }
E }
Question 2
A class Player {
B constructor(name) {
C name = name;
D this.score = 0;
E }
F }
Question 3
A class Car {
B speedUp() {
C this.speed + 10;
D }
E }
Question 4
A Class Student {
B constructor(name) {
C this.name = name;
D }
E }
Question 5
A class Pet {}
B const a = new Pet();
C const b = new Pet();
D const c = Pet();

Answer Key

Why programmers use object-oriented programming
Section A: Quick Questions
Q1
A — Related data and the code that uses it
Q2
B — An object with name and age properties and a speak() method
Q3
A — To reuse one blueprint to make many objects
Q4
B — Write one Enemy class and create 50 objects
Q5
A — It is easier to find, understand and fix the code for one thing
Section B: Match the Pairs
Q1
class Player { } → A blueprint that can be reused to make many player objects
Q2
const hero = new Player('Kai', 100); → Creates one object from the Player blueprint with its own data
Q3
Modelling a real-world thing: a pet has data (name, energy) and actions (eat) kept together in one object → class Pet { constructor(name) { this.name = name; this.energy = 5; } eat() { this.energy++; } }
Q4
Reusing a blueprint: one class makes many objects, so you do not rewrite the same code → const a = new Pet('Rex'); const b = new Pet('Mia');
Q5
let petName = 'Rex'; let petEnergy = 5; let petName2 = 'Mia'; let petEnergy2 = 8; → Related data is stored in loose separate variables, so it is not kept together and gets messy as the program grows. Use a Pet class and make one object per pet.
Q6
function feedPet(name, energy) { return energy + 1; } // copied again as feedDog, feedCat... → The same code is copied for each kind of thing instead of reusing a blueprint. Put the behaviour in a class method so every object can use it.
Section C: Definitions
Q1
OOP — Object-oriented programming: building a program from objects that bundle related data with the code that uses it, modelling real things and reusing a blueprint.
Section D: Fill in the Blanks
Q1
class Player { constructor(name) { this.name = name; } }
Q2
const a = new Player("Ava"); const b = new Player("Liam");
Q3
class Car { constructor(speed) { this.speed = speed; } }
Q4
class Pet { constructor(name) { this.name = name; } }
Q5
const ava = new Player("Ava"); ava.points = 0; console.log(ava.points);
Section E: Spot the Error
Q1
B — The special setup method must be spelled constructor. A misspelling means it is just an ordinary method, so it never runs when a Dog is created. Correct line: constructor(name) {
Q2
C — Without this, the line just assigns the parameter to itself and the object stores nothing. To keep data inside the object, write this.name = name;
Q3
C — A plain + calculates a value but does not store it. To change the car's own data inside its method, use +=, like this.speed += 10;
Q4
A — The keyword class must be all lowercase because JavaScript is case-sensitive. A class is the blueprint, so it must start with class Student {
Q5
D — Each object made from the blueprint needs the new keyword. Without it, JavaScript throws an error. Correct line: const c = new Pet();