23-24 APCS Unit Five MC - A - ANSWERS
23-24 APCS Unit Five MC - A - ANSWERS
1. The Car class will contain two string attributes for a car’s make and model.
The class will also contain a constructor.
public class Car
{
/* missing code */
}
Which of the following replacements for /* missing code */ is the most appropriate
implementation of the class?
(A)
public String make; not private
public String model; not private
public Car(String myMake, String myModel)
{ /* implementation not shown */ }
(B)
public String make; not private
public String model; not private
private Car(String myMake, String myModel) not public
{ /* implementation not shown */ }
(C)
private String make;
private String model;
public Car(String myMake, String myModel)
{ /* implementation not shown */ }
(D)
public String make; not private
private String model;
private Car(String myMake, String myModel) not public
( /* implementation not shown */ }
(E)
private String make;
private String model;
private Car(String myMake, String myModel) not public
{ /* implementation not shown */ }
2. The Date class below will contain three int attributes for day, month, and
year, a constructor, and a setDate method. The setDate method is intended to
be accessed outside the class.
public class Date
{
/* missing code */
}
Which of the following replacements for /* missing code */ is the most appropriate
implementation of the class?
(A)
private int day;
private int month;
private int year;
private Date() should be public
{ /* implementation not shown */ } check the comment location
private void setDate(int d, int m, int y) should be public
{ /* implementation not shown */ }
(B)
private int day;
private int month;
private int year;
public Date()
{ /* implementation not shown */ }
private void setDate(int d, int m, int y)
{ /* implementation not shown */ } should be public
(C)
private int day;
private int month;
private int year;
public Date()
{ /* implementation not shown */ }
public void setDate(int d, int m, int y)
{ /* implementation not shown */ }
(D)
public int day; should be private
public int month; should be private
public int year; should be private
private Date() should be public
{ /* implementation not shown */ }
private void setDate(int d, int m, int y) should be public
{ /* implementation not shown */ }
(E)
public int day; should be private
public int month; should be private
public int year; should be private
public Date()
{ /* implementation not shown */ }
public void setDate(int d, int m, int y) should be private
{ /* implementation not shown */ }
3. The Player class below will contain two int attributes and a constructor. The
class will also contain a method getScore that can be accessed from outside
the class.
public class Player
{
/* missing code */
}
Which of the following replacements for /* missing code */ is the most appropriate
implementation of the class?
(A)
private int score;
private int id;
private Player(int playerScore, int playerID) should be public
{ /* implementation not shown */ }
private int getScore() should be public
{ /* implementation not shown */ }
(B)
private int score;
private int id;
public Player(int playerScore, int playerID)
{ /* implementation not shown */ }
private int getScore() should be public
{ /* implementation not shown */ }
(C)
private int score;
private int id;
public Player(int playerScore, int playerID)
{ /* implementation not shown */ }
public int getScore()
{ /* implementation not shown */ }
(D)
public int score; should be private
public int id; should be private
public Player(int playerScore, int playerID)
{ /* implementation not shown */ }
private int getScore() should be public
{ /* implementation not shown */ }
(E)
public int score; should be private
public int id; should be private
public Player(int playerScore, int playerID)
{ /* implementation not shown */ }
public int getScore()
{ /* implementation not shown */ }
4. Consider the following class definition.
public class Element
{
public static int max_value = 0; part of the class, not one object
private int value;
public Element (int v) v will contain k from the if statement
{
value = v; the constructor sets the value or v
if (value > max_value) max_value already is set to zero
{ for each object created
max_value = value; shared by all objects of the class
}
}
}
Which of the following best describes the behavior of the code segment?
(A) Exactly 5 Element objects are created.
(B) Exactly 10 Element objects are created.
(C) Between 0 and 5 Element objects are created, and Element.max_value is increased
only for the first object created.
(D) Between 1 and 5 Element objects are created, and Element.max_value is increased
for every object created.
(E) Between 1 and 5 Element objects are created, and Element.max_value is increased
for at least one object created.
5. Consider the following class definition.
public class WordClass
{
private final String word; cannot change one initialized
private static String max_word = ""; shared by all objects
public WordClass(String s)
{
word = s;
if (word.length() > max_word.length()) max_word.length() = 0
{
max_word = word;
}
}
}
Which of the following is a true statement about the behavior of WordClass objects?
(A) A WordClass object can change the value of the variable word more than once.
False – word is final; once initialized cannot be changed
(B) Every time a WordClass object is created, the max_word variable is referenced.
(C) Every time a WordClass object is created, the value of the max_word variable
changes.
False – only when the new word length is greater
(D) No two WordClass objects can have their word length equal to the length of
max_word.
False – max_word only changes when word.length is greater
(E) The value of the max_word variable cannot be changed once it has been initialized.
False – max_word is static, not final
6. Consider the following class definition.
public class RentalCar
{
private double dailyRate; // the fee per rental day
private double mileageRate; // the fee per mile driven
public RentalCar(double daily, double mileage)
{
dailyRate = daily;
mileageRate = mileage;
}
public double calculateFee(int days, int miles)
{
/* missing code */
}
}
The calculateFee method is intended to calculate the total fee for renting a car. The total
fee is equal to the number of days of the rental, days, times the daily rental rate plus the
number of miles driven, miles, times the per mile rate.
Which of the following code segments should replace /* missing code */ so that the
calculateFee method will work as intended?
(A) return dailyRate + mileageRate;
(B) return (daily * dailyRate) + (mileage * mileageRate);
(C) return (daily * days) + (mileage * mile*s);
(D) return (days * dailyRate) + (miles * mileageRate);do the math
(number of days of the rental * daily rental rate) + (number of miles driven * per mile
rate)
(E) return (days + miles) * (dailyRate + mileageRate);
7. 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 object-oriented
program design?
(A) Use one class, Car, with three instance variables: int numDoors, boolean hasAir,
and double milesPerGallon.
(B) Use four unrelated classes: Car, Doors, AirConditioning, and MilesPerGallon.
All classes need instance variables, constructors, and methods;
(C) Use a class Car with three subclasses: Doors, AirConditioning, and MilesPerGallon.
Subclasses are related to the superclass using “is-a”
(D) Use a class Car, with a subclass Doors, with a subclass AirConditioning, with a
subclass MilesPerGallon.
Multiple subclasses use “is-a”
(E) Use three classes: Doors, AirConditioning, and MilesPerGallon, each with a subclass
Car.
Multiple subclasses use “is-a”
8. Consider the definition of the Person class below. The class uses the
instance variable adult to indicate whether a person is an adult or not.
public class Person
{
private String name;
private int age;
private boolean adult;
public Person (String n, int a)
{
name = n;
age = a;
if (age >= 18)
{
adult = true;
}
else
{
adult = false;
}
}
}
Which of the following statements will create a Person object that represents an adult
person?
(A) Person p = new Person ("Homer", "adult"); second parameter must be an
integer
(B) Person p = new Person ("Homer", 23);
(C) Person p = new Person ("Homer", "23"); second parameter must be an
integer
(D) Person p = new Person ("Homer", true); second parameter must be an
integer
(E) Person p = new Person ("Homer", 17); 17 is not old enough to be an adult
9. Consider the following class definition. Each object of the class Item will
store the item’s name as itemName, the item’s regular price, in dollars, as
regPrice, and the discount that is applied to the regular price when the item is
on sale as discountPercent. For example, a discount of 15% is stored in
discountPercent as 0.15.
public class Item
{
private String itemName;
private double regPrice;
private double discountPercent;
public Item (String name, double price, double discount)
{
itemName = name;
regPrice = price;
discountPercent = discount;
}
public Item (String name, double price)
{
itemName = name;
regPrice = price;
discountPercent = 0.25;
}
/* Other methods not shown */
}
Which of the following code segments, found in a class other than Item, can be used to
create an item with a regular price of $10 and a discount of 25% ?
I. Item b = new Item("blanket", 10.0, 0.25); string, double, double
II. Item b = new Item("blanket", 10.0); string, double – second constructor
III. Item b = new Item("blanket", 0.25, 10.0); string, double ($.25), double
(1000%)
(A) I only
(B) II only
(C) III only
(D) I and II only
(E) I, II, and III
10. Consider the following class, which uses the instance variable balance to
represent a bank account balance.
public class BankAccount
{
private double balance;
public double deposit(double amount)
{
/* missing code */
}
}
The deposit method is intended to increase the account balance by the deposit amount
and then return the updated balance. Which of the following code segments should
replace /* missing code */ so that the deposit method will work as intended?
(A)
amount = balance + amount; returns what the user deposited only
return amount; amount
(B)
balance = amount; changes the balance, does not increase
return amount; amount
(C)
balance = amount; same as (B)
return balance; balance
(D)
balance = balance + amount; returns what the user deposited only
return amount; amount
(E)
balance = balance + amount;
return balance; balance
11. Consider the following class definition.
public class Password
{
private String password;
public Password(String pwd)
{
password = pwd;
}
public void reset(String new_pwd) return type void
{ (setter method)
password = new_pwd;
}
}
Consider the following code segment, which appears in a method in a class other than
Password. The code segment does not compile.
Password p = new Password("password");
System.out.println("The new password is " + p.reset("password"));
Must be able to print the parameter
Which of the following best identifies the reason the code segment does not compile?
(A) The code segment attempts to access the private variable password from outside the
Password class. method is inside the same class, so OK
(B) The new password cannot be the same as the old password. maybe in the real world
(C) The Password class constructor is invoked incorrectly. nope
(D) The reset method cannot be called from outside the Password class. it is public
(E) The reset method does not return a value that can be printed.
12. Consider the following class declaration.
public class Circle
{
private double radius;
public double computeArea()
{
private double pi = 3.14159;
public double area = pi * radius * radius;
return area;
}
// Constructor not shown.
}
Which of the following best explains why the computeArea method will cause a
compilation error?
(D) Local variables used inside a method must be declared at the end of the method.
anywhere
(E) Local variables used inside a method must be declared before the method header.
outside?
13. Consider the following class definition.
public class Info
{
private String name;
private int number;
public Info(String n, int num) n and num are parameters
{ n and num are destroyed after the
name = n; object is created
number = num;
}
public void changeName(String newName)
{
name = newName;
}
public int addNum(int n)
{
num += n;
return num;
}
}
Which of the following best explains why the class will not compile?
Which of the following best identifies the reason the class does not compile?
(A) The constructor header is missing a return type. No return type for constructors
(B) The updateItems method is missing a return type.
(C) The constructor should not have a parameter. Default does not
(D) The updateItems method should not have a parameter. This is a setter method
(E) The instance variable numItems should be public instead of private. private
15. Consider the following Bugs class, which is intended to simulate variations
in a population of bugs. The population is stored in the method’s int attribute.
The getPopulation method is intended to allow methods in other classes to
access a Bugs object’s population value; however, it does not work as
intended.
public class Bugs
{
private int population;
public Bugs(int p)
{
population = p;
}
public int getPopulation()
{
return p;
}
}
Which of the following best explains why the getPopulation method does NOT work as
intended?