Unit 2 Topic Questions - Using objects
Unit 2 Topic Questions - Using objects
public void slope(int x1, int y1, int x2, int y2)
{
int xChange = x2 - x1;
int yChange = y2 - y1;
printFraction(yChange, xChange);
}
Assume that the method call slope(1, 2, 5, 10) appears in a method in the same class. What is printed as
a result of the method call?
(A) 8/4
(B) 5/1
(C) 4/8
(D) 2/1
(E) 1/5
3.
Consider the following method, which is intended to calculate and return the expression .
Which of the following can replace /* missing code */ so that the method works as intended?
(A) Math.sqrt(x ^ 2, y ^ 2, a - b)
(B) Math.sqrt((x + y) ^ 2) / Math.abs(a, b)
(C) Math.sqrt((x + y) ^ 2 / Math.abs(a - b))
(D) Math.sqrt(Math.pow(x + y, 2) / Math.abs(a, b))
(E) Math.sqrt(Math.pow(x + y, 2) / Math.abs(a - b))
Which of the following lines of code, if located in a method in the same class as myMethod, will compile without
error?
(A) int result = myMethod(2, false);
(B) int result = myMethod(2.5, true);
(C) double result = myMethod(0, false);
(D) double result = myMethod(true, 10);
(E) double result = myMethod(2.5, true);
(A) 3
(B) 4
(C) 5
(D) 6
(E) 7
int one = 1;
int two = 2;
String zee = "Z";
System.out.println(one + two + zee);
9. The Student class has been defined to store and manipulate grades for an individual student. The following
methods have been defined for the class.
Which of the following statements, if located in a method in the Student class, will determine the average of all
of the student’s grades except for the lowest grade and store the result in the double variable newAverage ?
(A) newAverage = sumOfGrades() / numberOfGrades() - 1;
(B) newAverage = sumOfGrades() / (numberOfGrades() - 1);
(C) newAverage = sumOfGrades() - lowestGrade() / (numberOfGrades() - 1);
(D) newAverage = (sumOfGrades() - lowestGrade()) / numberOfGrades() - 1;
(E) newAverage = (sumOfGrades() - lowestGrade()) / (numberOfGrades() - 1);
Each of the following statements appears in a method in the same class as doSomething. Which of the
following statements are valid uses of the method doSomething ?
I. doSomething();
II. String output = doSomething();
III. System.out.println(doSomething());
(A) I only
(B) II only
(C) I and II only
(D) I and III only
(E) I, II, and III
double d1 = 10.0;
Double d2 = 20.0;
Double d3 = new Double(30.0);
double d4 = new Double(40.0);
Assume that the following code segment appears in a class other than ExamScore.
(A) 4.0
(B) 5.0
(C) 80.0
(D) 84.0
(E) 85.0
13. Consider the following methods, which appear in the same class.
Which of the following statements, if located in a method in the same class, will initialize the variable x to 11?
(A) int x = function2(4, 5) + function1(1, 3);
(B) int x = function1(4, 5) + function2(1, 3);
(C) int x = function1(4, 5) + function2(3, 1);
(D) int x = function1(3, 1) + function2(4, 5);
(E) int x = function2(3, 1) + function1(4, 5);
public Game()
{
numPlayers = 1;
gameOver = false;
}
Assume that the GameClass object game has been properly declared and initialized in a method in a class
other than GameClass. Which of the following statements are valid?
I. game.numPlayers++;
II. game.addPlayer();
III. game.gameOver();
IV. game.endGame();
(A) IV only
(B) I and III only
(C) I and IV only
(D) II and IV only
(E) II, III, and IV only
15. A student has created a Car class. The class contains variables to represent the following.
int a = 1988;
int b = 1990;
System.out.println(s);
17. A student has created an OrderedPair class to represent points on an -plane. The class contains the
following.
Which of the following code segments, appearing in a class other than Point2D, will correctly create an instance
of a Point2D object?
(A) Point2D p = (3.0, 4.0);
(B) Point2D p = Point2D(3.0, 4.0);
(C) new p = Point2D(3.0, 4.0);
(D) new Point2D = p(3.0, 4.0);
(E) Point2D p = new Point2D(3.0, 4.0);
19. Consider the following methods, which appear in the same class.
Consider the following code segment, which appears in a method in the same class as printSum and
printProduct.
int num1 = 5;
double num2 = 10.0;
printSum(num1, num2);
printProduct(num1, num2);
15
(A) 50
15
(B) 50.0
15.0
(C) 50
15.0
(D) 50.0
return y.doubleValue();
}
Assume that the method call puzzle(3) appears in a method in the same class as puzzle. What value is
returned as a result of the method call?
(A) 0.0
(B) 0.5
(C) 0.75
(D) 1.0
(E) 1.5
21. Which of the following statements assigns a random integer between 1 and 10, inclusive, to rn ?
(A) int rn = (int) (Math.random()) * 10;
(B) int rn = (int) (Math.random()) * 10 + 1;
(C) int rn = (int) (Math.random() * 10);
(D) int rn = (int) (Math.random() * 10) + 1;
(E) int rn = (int) (Math.random() + 1) * 10;
22. Consider the following code segment, which is intended to assign to num a random integer value between min
and max, inclusive. Assume that min and max are integer variables and that the value of max is greater than
the value of min.
double rn = Math.random();
int num = /* missing code */;
Which of the following could be used to replace /* missing code */ so that the code segment works as
intended?
23. A student has created a Song class. The class contains the following variables.
public Student(int s)
{
studentID = s;
gradeLevel = 9;
honorRoll = false;
}
}
Which of the following code segments would successfully create a new Student object?
25. SHOW ALL YOUR WORK. REMEMBER THAT PROGRAM SEGMENTS ARE TO BE WRITTEN IN JAVA.
Assume that the classes listed in the Java Quick Reference have been imported where appropriate.
Unless otherwise noted in the question, assume that parameters in method calls are not null and that methods are
called only when their preconditions are satisfied.
In writing solutions for each question, you may use any of the accessible methods that are listed in classes defined
in that question. Writing significant amounts of code that can be replaced by a call to one of these methods will
not receive full credit.
Consider the following Thing class. Each Thing object has a name attribute, which can be set in the constructor or by
using the setName method. The name of a Thing object can be returned by the getName method.
(a) Write a statement to create a new Thing object snack that has the name "potato chip".
(b) The Thing method printMessage prints a string consisting of the name of the object followed by
"_is_great".
Suppose the name of the Thing object favFood is "pizza". Write a statement that uses the printMessage
(c) Write a code segment to change the name of the Thing object something such that the new name consists of
the old name with one character removed at random. For example, if something has name "ABCD", its new name
could be set to "ACD".
Which of the following code segments, if located in a method in a class other than Thing, will cause the message
"Hello my friend" to be printed?
Thing a = new Thing();
(A) Thing.talk();
Thing.name();
Thing a = new Thing();
(B)
Thing.greet();
Thing a = new Thing();
(C)
a.talk();
Thing a = new Thing();
(D)
a.greet();
Thing a = new Thing();
(E) a.name();
a.talk();
The following code segment appears in a method in the same class as the timesTwo method.
Which of the following code segments, when placed in a method in a class other than WindTurbine, will
construct a WindTurbine object wt with an efficiencyRating of 0.25 ?