Back to Student Worksheet
CS Grade 9-12 Answer Key

Object-Oriented Programming: Classes and Objects

Practice identifying classes, objects, attributes, and methods

Answer Key
Name:
Date:
Score: / 12

Object-Oriented Programming: Classes and Objects

Practice identifying classes, objects, attributes, and methods

CS - Grade 9-12

Instructions: Read each problem carefully. Write clear answers using complete sentences. When asked to write code, use readable pseudocode or a programming language you know.
  1. 1

    In object-oriented programming, explain the difference between a class and an object. Use the example of a Car class in your explanation.

    Think of a class as a plan and an object as something built from the plan.

    A class is a blueprint or template that describes what data and behaviors something can have. An object is a specific instance created from that class. For example, Car is a class, while a red 2022 Honda Civic with a specific mileage is an object of the Car class.
  2. 2

    A Student class has the attributes name, gradeLevel, and gpa. Write two different Student objects with realistic values for each attribute.

    One possible Student object is name = "Maya", gradeLevel = 10, and gpa = 3.7. Another possible Student object is name = "Jordan", gradeLevel = 12, and gpa = 3.2. Both objects use the same attributes but store different values.
  3. 3

    Identify the class, object, attributes, and methods in this situation: A video game has a Player class. One player is named Alex. Alex has 80 health points and 150 coins. Alex can jump, attack, and collect coins.

    Attributes describe data. Methods describe actions or behavior.

    The class is Player. The object is the specific player named Alex. The attributes include name, health points, and coins. The methods include jump, attack, and collect coins.
  4. 4

    Design a Book class for a library program. List at least four attributes and at least three methods that would make sense for the class.

    A Book class could have the attributes title, author, pageCount, genre, and isCheckedOut. It could have the methods checkOut(), returnBook(), displayInfo(), and updateGenre(). These attributes describe the book, and the methods describe actions related to the book.
  5. 5

    A class called BankAccount has attributes accountHolder and balance. It has methods deposit(amount) and withdraw(amount). Explain what should happen when deposit(50) is called on an object with a current balance of 125.

    A method can change the data stored inside an object.

    When deposit(50) is called, the BankAccount object should add 50 to its current balance. The new balance should be 175 because 125 plus 50 equals 175.
  6. 6

    Write simple pseudocode for a Dog class with the attributes name and age and a method bark() that prints a short message using the dog's name.

    One possible answer is: class Dog has attributes name and age. The method bark() prints name + " says woof!". This design lets each Dog object store its own name and use that name when it barks.
  7. 7

    A programmer creates three objects from a Phone class: phone1, phone2, and phone3. Explain why each object can have a different batteryLevel even though all three objects come from the same class.

    Objects made from the same class share a structure, but not necessarily the same data values.

    Each object has its own copy of the attributes defined by the class. The Phone class defines that phones have a batteryLevel, but phone1, phone2, and phone3 can store different batteryLevel values because they are separate objects.
  8. 8

    Read this pseudocode: class Rectangle has attributes width and height. It has a method area() that returns width times height. If rect1 has width 6 and height 4, what should rect1.area() return? Explain your answer.

    rect1.area() should return 24. The method multiplies the object's width by its height, so 6 times 4 equals 24.
  9. 9

    Explain why constructors are useful when creating objects. Use an example involving a Movie class.

    A constructor helps initialize a new object.

    Constructors are useful because they set up an object with starting values when the object is created. For example, a Movie constructor could receive a title, director, and rating so that a new Movie object begins with those values already stored.
  10. 10

    A Temperature class stores a temperature in Celsius using an attribute called celsius. It has a method toFahrenheit() that returns celsius times 9 divided by 5 plus 32. If temp1.celsius is 20, what should temp1.toFahrenheit() return? Show the calculation.

    temp1.toFahrenheit() should return 68. The calculation is 20 times 9 divided by 5 plus 32, which is 36 plus 32, so the result is 68 degrees Fahrenheit.
  11. 11

    A class called Playlist has an attribute songs, which stores a list of song titles. It has a method addSong(title). Explain what the method should do and how it changes a Playlist object.

    A method can update a list stored as an attribute.

    The addSong(title) method should add the given song title to the songs list. It changes the Playlist object by increasing the number of songs stored in that specific playlist.
  12. 12

    Choose a real-world object that could be modeled in software. Name the class, describe one possible object, list three attributes, and list two methods.

    One possible answer is a Bicycle class. A possible object is myBike. Three attributes are color, speed, and gear. Two methods are pedal() and brake(). This works because the class describes the general type, while the object represents one specific bicycle.
LivePhysics™.com CS - Grade 9-12 - Answer Key