Practice Test for Objects (2.1-2.5)¶
- 2-16-1: A student has created a Liquid class. The class contains variables to represent the following.
A double variable called boilingPoint to represent the boiling point of the liquid.
A double variable called freezingPoint to represent at what temperature the liquid will freeze.
A boolean variable called hasFrozen to indicate whether the liquid is now frozen.
The object liquid1 will be declared as type Liquid. Which of the following statements is accurate?
- An attribute of boilingPoint object is double.
- double is a data type, not an attribute.
- An instance of the liquid1 class is Liquid.
- The classname is Liquid and the object instance is liquid1.
- An attribute of the liquid1 object is freezingPoint.
- Correct!
- An instance of a Liquid object is hasFrozen.
- The classname is Liquid and the object instance is liquid1.
- An attribute of freezingPoint is liquid1.
- An attribute of liquid1 is freezingPoint.
- 2-16-2: A student has created an Artwork class. The class contains the following variables.
A String variable called artist to represent the artist’s name
A String variable called title to represent the artwork’s title
A String variable called gallery to represent the gallery title
The object painting1 will be declared as type Artwork. Which of the following statements is true?
- painting1 is an instance of three String objects.
- The String variables are attributes of painting1.
- artist, title, and gallery are instances of the Artwork class.
- These are attributes.
- Artwork is an instance of the painting1 object.
- painting1 is an instance of Artwork.
- painting1 is an instance of the Artwork class.
- Correct!
- Artwork is an instance of three String objects.
- Artwork is the class name.
- I only
- I needs to initialize the object variable with a call to new Party().
- I and II
- I needs to initialize the object variable with a call to new Party().
- II only
- Correct!
- II and III
- III calls the Party constructor with a double parameter instead of an int.
- I, II, and III
- I needs to initialize the object and III needs to use an int instead of a double as the parameter for the constructor.
- Liquid l = new Liquid(98.6);
- Correct
- new Liquid l = 98.6;
- new is incorrectly placed.
- Liquid l = new Liquid();
- This creates an object but it does not set its boiling point to 98.6.
- Liquid l = 98.6;
- The call to the constructor is missing.
- Liquid l = Liquid(98.6);
- The keyword new is missing.
- liquid.freeze(80);
- Method freeze() does not have parameters.
- liquid.freeze();
- Correct
- liquid.increaseTemp();
- There is no method increaseTemp() in the Liquid class definition.
- liquidfreeze();
- The dot operator is required between the object name and the method name.
- liquid.freeze;
- Parentheses are required after a method name.
1Dog a = new Dog(); 2a.bark(); 3a.wag();
-
This would print “Woof Wag Tail “
Dog a = new Dog(); Dog.happy();
-
You must use the object a, not the class name Dog, to call its methods.
Dog a = new Dog(); a.happy();
-
This would print out “Wag Tail Woof “.
1Dog a = new Dog(); 2a.wag(); 3a.happy();
-
This would print out “Wag Tail Wag Tail Woof “;
Dog a = new Dog(); a.wag();
-
This would just print “Wag Tail “.
5.0 –> 77.0
-
(5 * 9)/5 + 32 = 41
41 –> 5
-
Notice the order of parameters in printTemperature.
celsius –> fahrenheit
-
These are variables and their values would be printed.
5 –> 33.8
-
(5 * 9)/5 + 32 = 41
5.0 –> 41.0
-
Correct! (5 * 9)/5 + 32 = 41 and doubles print out with .0 at the end.
printOrder(numOfPizzas);
-
Correct! If you had 8 people who want to eat 2 pizza slices each, numOfPizzas would be 8*2/8 = 2 pizzas, and printOrder would print out “Order 2 pizzas”.
printOrder(numOfPeople);
-
This would always print out an order of how many people you have instead of how many calculated pizzas.
printOrder(2);
-
This would always print out “Order 2 pizzas” instead of the calculated number of pizzas.
printOrder(slicesPerPerson);
-
This would always print out an order of how many slices per person instead of how many calculated pizzas.
calculatePizzaOrder(numOfPizzas);
-
This would not call the printOrder method.
int result = calculateMovieRating(234, null);
-
The method returns a double which cannot be saved in an int variable.
double result = calculateMovieRating(100.0, 3.0);
-
The first parameter must be an int.
int result = calculateMovieRating(455, false);
-
The method returns a double which cannot be saved in an int variable.
double result = calculateMovieRating(10, 4.0);
-
Correct.
double result = calculateMovieRating(10);
-
The method has 2 parameters.
- int x = oneThing(2, 10) + anotherThing(5, 2);
- oneThing(2,10) returns 2*10 = 20 and anotherThing(5,2) returns 5/2 = 2.5 truncated to 2 with integer division, which adds up to 22.
- int x = oneThing(10, 2) + anotherThing(2, 5);
- This would return 20 + 0 (which is 0.4 truncated) = 20.
- int x = oneThing(2, 10) + anotherThing(3, 2);
- This would return 20 + 1 (which is 1.5 truncated) = 21.
- int x = oneThing(6, 3) + anotherThing(2, 10);
- This would return 18 + 0 = 18.
- int x = oneThing(0, 2) + anotherThing(20, 1);
- This would return (0 * 2 = 0) + (20/1 = 20) = 20.
2-16-3: Which of the following code segments correctly creates an instance of a new Party object?
1public class Party 2{ 3 private int numInvited; 4 private boolean partyCancelled; 5 6 public Party() 7 { 8 numInvited = 1; 9 partyCancelled = false; 10 } 11 12 public Party(int invites) 13 { 14 numInvited = invites; 15 partyCancelled = false; 16 } 17} 18I. Party myParty; 19II. int classSize = 20; 20 Party ourParty = new Party(classSize); 21III. int numOfFriends = 6; 22 Party yourParty = new Party(numOfFriends + 3.0);
2-16-4: Consider the following class. Which of the following code segments, when placed in a method in a class other than Liquid, will construct a Liquid object l with a boilingPoint of 98.6 ?
1public class Liquid 2{ 3 private double boilingPoint; 4 private double freezingPoint; 5 6 public Liquid() 7 { 8 boilingPoint = 0.0; 9 } 10 11 public Liquid(double b) 12 { 13 boilingPoint = b; 14 } 15}
2-16-5: Consider the following class. Assume that the Liquid object liquid has been properly declared and initialized in a method in a class other than Liquid. Which of the following statements are valid?
1public class Liquid 2{ 3 private double boilingPoint; 4 private double freezingPoint; 5 private double currentTemp; 6 7 public Liquid(double b) 8 { 9 boilingPoint = b; 10 } 11 12 void lowerTemp() 13 { 14 currentTemp -= 10; 15 } 16 17 void raiseTemp() 18 { 19 currentTemp += 10; 20 } 21 22 void freeze() 23 { 24 currentTemp = freezingPoint; 25 } 26}
2-16-6: Consider the following class definition.
1public class Dog
2{
3 public void bark()
4 {
5 System.out.print("Woof ");
6 }
7
8 public void wag()
9 {
10 System.out.print("Wag Tail ");
11 }
12
13 public void happy()
14 {
15 wag();
16 bark();
17 }
18 /* Constructors not shown */
19}
Which of the following code segments, if located in a method in a class other than Dog, will cause the message “Wag Tail Wag Tail Woof “ to be printed?
2-16-7: Consider the following methods, which appear in the same class.
1public void celsiusToFahrenheit(double cTemp)
2{
3 double fTemp = (cTemp * 9)/5 + 32;
4 printTemperature(cTemp, fTemp);
5}
6
7public void printTemperature(double celsius, double fahrenheit)
8{
9 System.out.print(celsius + "-->" + fahrenheit);
10}
Assume that the method call celsiusToFahrenheit(5)
appears in a method in the same class. What is printed as a result of the method call?
2-16-8: Consider the following methods, which appear in the same class.
1public void calculatePizzaOrder(int numOfPeople, double slicesPerPerson)
2{
3 int numOfPizzas = (numOfPeople * slicesPerPerson)/8;
4 /* INSERT CODE HERE */
5}
6
7public void printOrder(int number)
8{
9 System.out.println("Order " + number + " pizzas ");
10}
What of the following lines would go into /* INSERT CODE HERE */
in line 4 in order to call the printOrder
method to print the number of pizzas to order correctly?
Before you keep reading...
Runestone Academy can only continue if we get support from individuals like you. As a student you are well aware of the high cost of textbooks. Our mission is to provide great books to you for free, but we ask that you consider a $10 donation, more if you can or less if $10 is a burden.
2-16-9: Consider the following method in the Movie class.
public double calculateMovieRating(int numOfPeople, double rating)
{ /*implementation not shown */}
Which of the following lines of code, if located in a method in the same class as calculateMovieRating, will compile without an error?
2-16-10: Consider the following methods, which appear in the same class.
1public int oneThing(int i, int j)
2{
3 return i * j;
4}
5
6public int anotherThing(int i, int j)
7{
8 return i / j;
9}
Which of the following statements, if located in a method in the same class, will initialize the variable x to 22?