9.12.1. Easier Multiple Choice Questions

These problems are easier than most of those that you will usually see on the AP CS A exam.

    9-12-1: What best describes the purpose of a class’s constructor?

  • Initialize the fields in the object.
  • A constructor is often used to initialize the fields to their default values or in the case of a parameterized constructor, to the values passed in to the constructor.
  • Determines the amount of space needed for an object and creates the object.
  • The object is already created before the constructor is called.
  • Names the new object.
  • Constructors do not name the object.

    9-12-2: Under which of these conditions is it appropriate to overload a method (ie: the class will contain two methods with the same name)?

  • The methods do different things.
  • Methods that do different things should be named differently.
  • The methods have different parameter names.
  • There is no reason the parameter names ought to be different if the two methods are performing the same action.
  • The methods have different post-conditions.
  • If the methods have different post-conditions, they are performing different functions, and should be named differently.
  • Two methods with the same name can never be included in the same class.
  • If two methods perform the same function, they can be named the same. However, the number of parameters, type of parameters, or order of parameter types must be different.
  • The methods have different numbers of parameters
  • Overloading occurs when two methods perform the same essential operation, but take a different number and/or type of parameters.

    9-12-3: A car dealership needs a program to store information about the cars for sale. For each car, they want to keep track of the following information: number of doors (2 or 4), whether the car has air conditioning, and its average number of miles per gallon. Which of the following is the best design?

  • Use four unrelated classes: Car, Doors, AirConditioning, and MilesPerGallon.
  • Only Car should be a class. The number of doors, flag if it has air conditioning, and the average number of miles per gallon are attributes of a car so they belong in a Car class.
  • Use a class Car with three subclasses: Doors, AirConditioning, and MilesPerGallon.
  • Doors, air conditioning, and miles per gallon are not a kind of car. Child classes need to be able to be substituted for the parent class.
  • Use a class Car, with fields: numDoors, hasAir, and milesPerGallon.
  • The number of doors, flag if it has air conditioning, and the average number of miles per gallon are attributes of a car. Each of these is a simple value so they can just be fields of a Car class.
  • Use a class Car, with subclasses of Doors, AirConditioning, and MilesPerGallon.
  • A door is not a type of car. A flag for air conditioning is not a type of door, and a miles per gallon is not a type of air conditioning flag. Child classes need to be able to be substituted for the parent class.
  • Use classes: Doors, AirConditioning, and MilesPerGallon, each with a subclass Car.
  • A class Car can't be a subclass of three different classes. Each class can only have one parent class. Also a car is not a type of door, air conditioning flag, or miles per gallon. Child classes need to be able to be substituted for the parent class.

    9-12-4: A program is being written by a team of programmers. One programmer is implementing a class called Employee; another programmer is writing code that will use the Employee class. Which of the following aspects of the public methods and fields of the Employee class does not need to be known by both programmers?

  • How the methods are implemented.
  • Only the programmer of the Employee class must know how the public methods work. The programmer that is using the Employee class can just use the public methods and not worry about how they are implemented.
  • The method names.
  • The programmer who writes the methods will need to know what the names are. The programmer who will use the public methods will also need to know the names of the methods in order to invoke them.
  • The method return types.
  • In order to use the public methods of the Employee class, a programmer must know the method return types.
  • Constants
  • Constants are public fields and are meant to be used by people using a class.
  • The number and types of the method parameters.
  • In order to use the public methods of the Employee class, a programmer must know the number of parameters and the type for each parameter.

    9-12-5: A bookstore is working on an on-line ordering system. For each type of published material (books, movies, audio tapes) they need to track the id, title, author(s), date published, and price. Which of the following would be the best design?

  • Create one class PublishedMaterial with the requested fields plus type.
  • This will complicate the process of retrieving objects based on their type. Also if we need to add information that is specific to Book or Movie or AudioTape it would be best if these were subclasses of PublishedMaterial.
  • Create classes Book, Movie, and AudioTape with the requested fields.
  • This involves writing more code than is necessary (usually people copy and paste the shared code) and makes it harder to fix errors. It would be better to put common fields and methods in the superclass PublishedMaterial and have Book, Movie, and AudioTape be subclasses.
  • Create one class BookStore with the requested fields plus type.
  • The class name, BookStore, seems to imply the thing that keeps track of the store. This would be an appropriate class name for an object that handles the items in the Bookstore. However, for the published material, it would be better to use a superclass PublishedMaterial and subclasses for Book, Movie and AudioTape.
  • Create classes for each.
  • This is more classes than is necessary. Items such as Title, Price, ID, Author and DatePublished are simple variables that do not need a class of their own but should be fields in a PublishedMaterial superclass, with Movie, AudioTape and Book as subclasses.
  • Create the class PublishedMaterial with children classes of Book, Movie, and AudioTape.
  • We will need to get objects based on their type so we should create classes for Book, Movie, and AudioTape. They have common fields so we should put these in a common superclass PublishedMaterial.
You have attempted of activities on this page