Section3.13Multiple-Choice Exercises - Objects and Classes
Activity3.13.1.
The Liquid class will contain two double attributes for a liquidβs boiling point temperature and freezing point temperature. The class will also contain a constructor.
public class Liquid
{
/* missing code */
}
Which of the following replacements for /* missing code */ is the most appropriate
implementation of the class?
private double boilingPoint;
private double freezingPoint;
public Liquid(double boilingPoint, double freezingPoint)
{ /* implementation not shown */ }
Correct! The instance variables should be private and the constructor and methods should be public.
The Cat class below will contain two String attributes and one int attribute for name, color, and age; a constructor; and an adoptCat method. The adoptCat method is intended to be accessed outside the class.
public class Cat
{
/* missing code */
}
Which of the following replacements for /* missing code */ is the most appropriate
implementation of the class?
private String name;
private String color;
private int age;
public Cat()
{ /* implementation not shown */ }
private void adoptCat(String n, String c, int a)
{ /* implementation not shown */ }
Method adoptCat() should be public.
public String name;
public String color;
public int age;
private Cat()
{ /* implementation not shown */ }
private void adoptCat(String n, String c, int a)
{ /* implementation not shown */ }
Method and constructor should be public, and instance variables should be private.
private String name;
private String color;
private int age;
public Cat()
{ /* implementation not shown */ }
public void adoptCat(String n, String c, int a)
{ /* implementation not shown */ }
Correct! Method and constructor should be public, and instance variables should be private.
public String name;
public String color;
public int age;
public Cat()
{ /* implementation not shown */ }
public void adoptCat(String n, String c, int a)
{ /* implementation not shown */ }
Instance variables should be private.
public String name;
public String color;
public int age;
private Cat()
{ /* implementation not shown */ }
public void adoptCat(String n, String c, int a)
{ /* implementation not shown */ }
Instance variables should be private, and the constructor should be public.
public class Party
{
private int numOfPeople;
private String partyHost;
public Party (String name, int people)
{
partyHost = name;
numOfPeople = people;
}
}
Which of the following statements will create a Party object
that represents a party that has three people at it?
Consider the following class definition. Each object of the class Party will store the party hostβs name as partyHost, the number of people as numOfPeople, and the capacity that the event can hold as capacity. Which of the following code segments, found in a class other than Party, can be used to create a party hosted by Charlie without anyone there initially, but the place can hold 78 people ?
public class Party
{
private String partyHost;
private int numOfPeople;
private int capacity;
public Party(String name, int num, int cap)
{
partyHost = name;
numOfPeople = num;
capacity = cap;
}
public Party (String name, int cap)
{
partyHost = name;
numOfPeople = 0;
capacity = cap;
}
/* Other methods not shown */
}
I. Party b = new Party("Charlie", 78);
II. Party b = new Party("Charlie", 0, 70+8);
III. Party b = new Party("Charlie", 0, 78);
public class Party
{
private int numOfPeople;
private double volumeOfMusic;
/* missing constructor */
}
The following statement appears in a method in a class other than Party. It is intended to create a new Party object p with its attributes set to 10 and 5.0.
Consider the following class definition that defines a Liquid class with a boilingPoint, a currentTemperature, and a freezingPoint. For example, Liquid water = new Liquid(100, 50, 0); defines a water object with a boiling point of 100, a current temperature of 50, and a freezing temperature of 0.
public class Liquid
{
private int boilingPoint;
private int currentTemp;
private int freezingPoint;
public Liquid(int bp, int ct, int fp)
{
boilingPoint = bp;
currentTemp = ct;
freezingPoint = fp;
}
/* Other methods not shown */
}
Which of the following preconditions is reasonable for the Liquid constructor?
Consider the following Cat class, with the catβs age stored in the methodβs int attribute. The getAge method is intended to allow methods in other classes to access a Cat objectβs age value; however, it does not work as intended. Which of the following best explains why the getAge method does NOT work as intended?
Consider the following Liquid class. The currentTemperature is stored in the methodβs int attribute. The getCurrentTemp method is intended to allow methods in other classes to access a Liquid objectβs currentTemperature value; however, it does not work as intended. Which of the following best explains why the getCurrentTemperature method does NOT work as intended?
public class Liquid
{
private int currentTemperature;
public Liquid(int ct)
{
currentTemperature = ct;
}
public void getCurrentTemperature()
{
return currentTemperature;
}
}
The getCurrentTemperature method should be declared as private.
public class Liquid
{
private int currentTemp;
public Liquid(int temp)
{
currentTemp = temp;
}
public int getTemp()
{
return currentTemp;
}
public void resetTemp(int new_temp)
{
currentTemp = new_temp;
}
}
Consider the following code segment, which appears in a method in a class other than Liquid. The code segment does not compile.
Correct! The currentTemp instance variable is private and cannot be accessed outside of the class but the public accessor method getTemp() can be used instead.
The getTemp method cannot be called from outside the Liquid class.
In the Liquid class below, the raiseTemperature method is intended to increase the value of the instance variable currentTemp by the value of the parameter increase. The method does not work as intended.
public class Liquid
{
private int currentTemp;
public Liquid(int ct)
{
currentTemp = ct;
}
public void raiseTemperature(int increase) // Line 10
{
return currentTemp + increase; // Line 12
}
}
Which of the following changes should be made so that the class definition compiles without error and the method raiseTemperature works as intended?
Consider the following class definition. The calculatePizzaCostPerPerson method is intended to calculate the amount each person at the party must pay for pizza. The amount is equal to the total price of all the pizza boxes divided by the number of people at the party. Which of the following code segments should replace missing code so that the calculatePizzaCostPerPerson method will work as intended?
public class Party
{
private int numOfPeople; // number of people at the party
public Party(int people)
{
numOfPeople = people;
}
public double calculatePizzaCostPerPerson(
int numOfBoxes, double priceOfOnePizzaBox)
{
/* missing code */
}
}
Assume you have 5 boxes at $10 each. You would need to multiply them to get a total cost of $50. If you had 10 people at the party, you would need to divide $50 by 10 to get $5 per person.
Assume you have 5 boxes at $10 each for a total cost of $50. If you had 10 people at the party, you would need to divide $50 by 10 to get $5 per person.
Assume you have 5 boxes at $10 each. You would need to multiply them to get a total cost of $50. If you had 10 people at the party, you would need to divide $50 by 10 to get $5 per person.
Assume you have 5 boxes at $10 each for a total cost of $50. If you had 10 people at the party, you would need to divide $50 by 10 to get $5 per person.
Assume you have 5 boxes at $10 each for a total cost of $50. If you had 10 people at the party, you would need to divide $50 by 10 to get $5 per person.
public class Party
{
private int numOfPeople; // number of people at the party
private int capacity; // total capacity of people at party
public Party(int people, int cap)
{
numOfPeople = people;
capacity = cap;
}
public boolean updateNumOfPeople(int additionalPeople)
{
/* missing code */
}
}
The class contains the updateNumOfPeople method, which is intended to update the instance variable numOfPeople under certain conditions and return a value indicating whether the update was successful. If adding additionalPeople to the current number of people would lead to the number going over the capacity, then the update would be unsuccessful. Otherwise, if adding the number of additional people is still below or at the capacity, the update is successful. Which of the following code segments can replace missing code to ensure that the updateNumOfPeople method works as intended?