Using Classes And Objects Exam Prep Ch.3 4th Edition - Java Foundations 4th Edition | Test Bank with Answer Key by John Lewis by John Lewis, Peter DePasquale, Joe Chase. DOCX document preview.

Using Classes And Objects Exam Prep Ch.3 4th Edition

Chapter 3: Using Classes and Objects

Multiple Choice Questions:

1) 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

2) Which of the following is an invalid way to instantiate a String object?

  1. String title = new String("Java Software Solutions");
  2. String name = "John Lewis";
  3. String empty = "";
  4. String alsoEmpty = new String("");
  5. all of the above are valid

3) Assume that we have a Random object referenced by a variable called generator. Which of the following lines will generate a random number in the range 5-20 and store it in the int variable randNum?

  1. randNum = generator.nextInt(15) + 5;
  2. randNum = generator.nextInt(15) + 6;
  3. randNum = generator.nextInt(16) + 5;
  4. randNum = generator.nextInt(16) + 6;
  5. none of the above

4) Which of the following classes include the getCurrencyInstance() method?

  1. String
  2. NumberFormat
  3. DecimalFormat
  4. Math
  5. none of the above

5) Which of the following expressions correctly computes the value of the mathematical expression 5 + 26?

  1. result = 5 + 2^6;
  2. result = 5 + 2*exponent(6);
  3. result = 5 + 2*Math.exponent(6);
  4. result = 5 + Math.pow(2, 6);
  5. none of the above

6) 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?

  1. randNum will hold a number between 1 and 20 inclusive.
  2. randNum will hold a number between 0 and 20 inclusive.
  3. randNum will hold a number between 1 and 21 inclusive.
  4. these lines will not be executed because a compiler error will result.
  5. none of the above

7) Which of the following represents the proper way to create a NumberFormat object that formats numbers as percentages?

  1. NumberFormat fmt = new NumberFormat(%);
  2. NumberFormat fmt = new NumberFormat("%");
  3. NumberFormat fmt = NumberFormat.getPercentInstance();
  4. NumberFormat fmt = new PercentNumberFormat();
  5. none of the above

8) Which of the following is a correct declaration of enumerated type for the suits of a deck of cards?

  1. enumerated type Suit = { hearts, spades, diamonds, clubs };
  2. enum Suit {hearts, spades, diamonds, clubs };
  3. enum Suit {hearts, spades, diamonds, clubs }
  4. enumerated type Suit = {hearts, spades, diamonds, clubs };
  5. enum Suit = { hearts, spades, diamonds, clubs }

9) Fruit is an enumerated type with values apple, banana, grape, and orange, in that order. Which of the following statements assigns the value orange to the Fruit variable snack?

  1. snack = orange;
  2. Fruit.snack = orange;
  3. snack = Fruit.orange;
  4. snack =Fruit.last();
  5. snack = Fruit(4);

10) newNum is an Integer object that holds an int value. Which expression below returns the value in newNum as a double value?

  1. newNum.doubleValue()
  2. newNum.double();
  3. newNum.toDouble();
  4. Double.newNum();
  5. There is no way to return the value as a double.

11) ____________________ is the automatic conversion between a primitive value and a corresponding wrapper object.

a) Generating

b) Aliasing

c) Number formatting

d) Static invocation

e) Autoboxing

12) Which of the following best describes what happens when an object no longer has any references pointing to it?

a) The object is overwritten the next time the new operator is called.

b) The object is overwritten the next time the new operator is called using the same class.

c) The object is immediately deleted from memory.

d) The object is marked as garbage and its associated memory is freed when the garbage collector runs.

e) The object stays in memory for the remainder of the programs execution.

13) Suppose we have a String object referenced by a variable called listing. Suppose we want a new String object that consists of the first 5 characters in listing. Which of the following lines of code will achieve this?

  1. String prefix = listing.front(5);
  2. String prefix = listing.front(6);
  3. String prefix = listing.substring(1,5);
  4. String prefix = listing.substring(0,5);
  5. String prefix = listing.firstChars(5);

14) When two references point to the same object, ________________________________ .

a) a run-time error will occur.

b) a compiler error will occur.

c) the references are called aliases of each other.

d) the object will be marked for garbage collection.

e) the references are called null references.

15) The String class _______________________________ .

a) is part of the java.lang package.

b) is part of the java.util package.

c) is a wrapper class.

d) none of the above.

e) all of the above.

1) Multiple reference variables can refer to the same object.

2) The new operator is used to access an object's methods.

3) A variable that is declared as a primitive type stores the actual value of the associated data. A variable that is declared as an object type stores a pointer to the associated object.

4) The value of a primitive datatype variable may be assigned to an object of the corresponding wrapper class, and vice-versa.

5) String objects can be changed after instantiation.

6) All of the classes contained in the java.util package are automatically included in every Java program.

7) When called with integer parameter n, the nextInt() method of the Random class will return a randomly generated integer between 0 and n.

8) The Math class is part of the java.lang package.

9) Enumerated types allow a programmer to treat primitive data as objects.

10) The System.out.printf() method is an alternative way to output information in Java.

1) Explain how variables representing objects and variables representing primitive types are different.

2) Suppose we have a String object called myString. Write a single line of Java code that will output myString in such a way that all of its characters are uppercase.

3) Explain what it means for a String object to be immutable. Are there any workarounds for this?

4) Write a short program that allows the user to input a positive integer and then outputs a randomly generated integer between 1 and the input number.

5) Write a statement that computes the square root of a variable called discriminant and stores the value in a variable called result.

6) After an object is instantiated and associated with a reference variable, how are the object's methods accessed? Give an example.

7) generator is an object of the Random class. Write a single statement that generates a random number in the range 73 to 100 and assigns it to int variable randNum.

8) What is the range of integers that will be generated by the following expression?

generator.nextInt(15) + 5

9) Write an expression that will compute the tangent of an angle stored in a variable named angle, and put the resulting value in a variable named tangent.

10) Write a declaration for an enumerated type that represents the months of the year.

11) Write a single statement that creates a DecimalFormat object that formats numbers to 2 decimal places.

12) Write a short program that allows the user to enter the base and height of a triangle and outputs the hypotenuse, formatted to three decimal places.

13) A program will use a Scanner object from java.util.Scanner and a Random object from java.util.Random. Write a single import statement that will support the program.

14) Write a single line that creates a wrapper object named numWrapper for an int variable named num.

15) Write an expression that computes 12 raised to the power 4.3 and store the result in a double called result.

Document Information

Document Type:
DOCX
Chapter Number:
3
Created Date:
Aug 21, 2025
Chapter Name:
Chapter 3 Using Classes And Objects
Author:
John Lewis, Peter DePasquale, Joe Chase

Connected Book

Java Foundations 4th Edition | Test Bank with Answer Key by John Lewis

By John Lewis, Peter DePasquale, Joe Chase

Test Bank General
View Product →

$24.99

100% satisfaction guarantee

Buy Full Test Bank

Benefits

Immediately available after payment
Answers are available after payment
ZIP file includes all related files
Files are in Word format (DOCX)
Check the description to see the contents of each ZIP file
We do not share your information with any third party