10.13.1. Easier Multiple Choice Questions¶
These problems are easier than most of those that you will usually see on the AP CSA exam.
- 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-1: What best describes the purpose of a class’s constructor?
- 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-2: Under which of these conditions is it appropriate to overload a method (ie: the class will contain two methods with the same name)?
- Use four unrelated classes:
Car
,Doors
,AirConditioning
, andMilesPerGallon
. - 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 aCar
class. - Use a class
Car
with three subclasses:Doors
,AirConditioning
, andMilesPerGallon
. - 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
, andmilesPerGallon
. - 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 ofDoors
,AirConditioning
, andMilesPerGallon
. - 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
, andMilesPerGallon
, each with a subclassCar
. - 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-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?
- How the methods are implemented.
- Only the programmer of the
Employee
class must know how the public methods work. The programmer that is using theEmployee
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-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?
- 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
orMovie
orAudioTape
it would be best if these were subclasses ofPublishedMaterial
. - Create classes
Book
,Movie
, andAudioTape
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 haveBook
,Movie
, andAudioTape
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 theBookstore
. However, for the published material, it would be better to use a superclassPublishedMaterial
and subclasses forBook
,Movie
andAudioTape
. - Create classes for each.
- This is more classes than is necessary. Items such as
Title
,Price
,ID
,Author
andDatePublished
are simple variables that do not need a class of their own but should be fields in aPublishedMaterial
superclass, withMovie
,AudioTape
andBook
as subclasses. - Create the class
PublishedMaterial
with children classes ofBook
,Movie
, andAudioTape
. - We will need to get objects based on their type so we should create classes for
Book
,Movie
, andAudioTape
. They have common fields so we should put these in a common superclassPublishedMaterial
.
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?