JAVA FOUNDATIONS 3rd Ed By JOHN LEWIS – Test Bank
Chapter 3: Using Classes and Objects
Multiple Choice Questions:
1) The ________________ operator is used to instantiate an object.
a) static
b) new
c) +
d) –
e) none of the above
Answer: b
Explanation: The new operator instantiates an object. There is no static operator, and the + and – operators are for arithmetic expressions (although the + operator can also be used for String concatenation).
2) A special method that is invoked to set up an object during instantiation is called a ___________________.
a) new method
b) dot operator
c) creator
d) constructor
e) destructor
Answer: d
Explanation: The constructor is called to set up an object during instantiation. The dot operator is used to access methods of an object. There is no “new” method – new is an operator. Java also does not have “creators” or “destructors.”
3) Which of the following is an invalid way to instantiate a String object?
a) String title = new String(“Java Software Solutions”);
b) String name = “John Lewis”;
c) String empty = “”;
d) String alsoEmpty = new String(“”);
e) all of the above are valid
Answer: e
Explanation: Choices a and d represent the standard approach to instantiating a String object. Choice d creates the empty string. Choices b and c use a shortcut notation that is only available for creating a String object.
4) Assume that we have a Random object referenced by a variable called a generator. Which of the following lines will generate a random number in the range 5-20 and store it in the int variable random?
a) randNum = generator.nextInt(15) + 5;
b) randNum = generator.nextInt(15) + 6;
c) randNum = generator.nextInt(16) + 5;
d) randNum = generator.nextInt(16) + 6;
e) none of the above
Answer: c
Explanation: When called with 16 as a parameter, the nextInt() method will return a number in the range 0 to 15. Adding 5 to this random number will generate a number in the range 5 to 20.
5) Which of the following classes includes the getCurrencyInstance() method?
a) String
b) NumberFormat
c) DecimalFormat
d) Math
e) none of the above
Answer: b
Explanation: The NumberFormat class includes the getCurrencyInstance() method.
6) Which of the following expressions correctly computes 5 + 26?
a) result = 5 + 2^6;
b) result = 5 + 2*exponent(6);
c) result = 5 + 2*Math.exponent(6);
d) result = 5 + Math.pow(2, 6);
e) none of the above
Answer: d
Explanation: Choice a is wrong because Java does not have an exponential operator for primitive types. Choices b and C are wrong because there are no methods named exponent in the java.lang package or the Math class. Choice c correctly uses the Math. pow method to compute the expression.
7) Consider the following snippet of code:
Random generator = new Random();
int randNum = generator.nextInt(20) + 1;
Which of the following will be true after these lines are executed?
a) random will hold a number between 1 and 20 inclusive.
b) random will hold a number between 0 and 20 inclusive.
c) random will hold a number between 1 and 21 inclusive.
d) these lines will not be executed because a compiler error will result.
e) none of the above
Answer: a
Explanation: When called with a parameter of 20, the next () method will return an integer between 0 and 19 inclusive. Adding one to this will result in a number between 1 and 20 inclusive.
Reviews
There are no reviews yet.