Deck 3: Using Classes and Objects

Full screen (f)
exit full mode
Question
Which of the following is a correct declaration of enumerated type for the suits of a deck of cards?

A) enumerated type Suit = { hearts, spades, diamonds, clubs };
B) enum Suit {hearts, spades, diamonds, clubs };
C) enum Suit {hearts, spades, diamonds, clubs }
D) enumerated type Suit = {hearts, spades, diamonds, clubs };
E) enum Suit = { hearts, spades, diamonds, clubs }
Use Space or
up arrow
down arrow
to flip the card.
Question
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?

A) String prefix = listing.front(5);
B) String prefix = listing.front(6);
C) String prefix = listing.substring(1,5);
D) String prefix = listing.substring(0,5);
E) String prefix = listing.firstChars(5);
Question
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?

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
Question
When there are no references to an object, the object is marked as garbage and is automatically removed from memory using Java's automatic garbage collection feature.
Question
Which of the following classes include the getCurrencyInstance() method?

A) String
B) NumberFormat
C) DecimalFormat
D) Math
E) none of the above
Question
When an object variable is declared but it is not assigned a value, it is called a ______________________.

A) null reference
B) empty reference
C) void reference
D) zero reference
E) static reference
Question
Which of the following expressions correctly compute 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
Question
String objects can be changed after instantiation.
Question
Multiple reference variables can refer to the same object.
Question
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
Question
Which of the following represents the proper way to create a NumberFormat object that formats numbers as percentages?

A) NumberFormat fmt = new NumberFormat(%);
B) NumberFormat fmt = new NumberFormat("%");
C) NumberFormat fmt = NumberFormat.getPercentInstance();
D) NumberFormat fmt = new PercentNumberFormat();
E) none of the above
Question
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.
Question
____________________ 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
Question
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
Question
The new operator is used to access an objects methods.
Question
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.
Question
The ________________ operator is used to instantiate an object.

A) static
B) new
C) +
D) -
E) none of the above
Question
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.
Question
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) randNum will hold a number between 1 and 20 inclusive.
B) randNum will hold a number between 0 and 20 inclusive.
C) randNum 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
Question
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.
Question
Write a short program that asks the user to input a string, and then outputs the number of characters in the string.
Question
Write a single statement that creates a DecimalFormat object that formats numbers to 2 decimal places.
Question
When called with integer parameter n, the nextInt() method of the Random class will return a randomly generated integer between 0 and n.
Question
The Math class is part of the java.lang package.
Question
The System.out.printf() method is an alternative way to output information in Java.
Question
All of the classes contained in the java.util package are automatically included in every Java program.
Question
Suppose a Random object has been created called generator. Write a single statement that generates a random number in the range 73 to 100.
Question
Write a declaration for an enumerated type that represents the months of the year.
Question
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.
Question
What is the range of integers that will be generated by the following expression?
generator.nextInt(15) + 5;
Question
Explain what it means for a String object to be immutable. Are there any workarounds for this?
Question
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.
Question
Explain how variables representing objects and variables representing primitive types are different.
Question
Write an expression that will compute the length of the tangent of a right triangle, given one of the non-right angles. You may assume that this value is stored in a variable called angle.
Question
What is output by the following code fragment?
String hello = new String("Hello World!");
hello = hello.replace('H', 'W');
hello = hello.replace('W', 'H');
System.out.println(hello);
Question
Write an expression that computes 12 raised to the power 4.3 and store the result in a double called result.
Question
Write a single line that creates a wrapper for an int variable num.
Question
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.
Question
Enumerated types allow a programmer to treat primitive data as objects.
Question
Write a statement that computes the square root of a variable called discriminant and stores the value in a variable called result.
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/40
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 3: Using Classes and Objects
1
Which of the following is a correct declaration of enumerated type for the suits of a deck of cards?

A) enumerated type Suit = { hearts, spades, diamonds, clubs };
B) enum Suit {hearts, spades, diamonds, clubs };
C) enum Suit {hearts, spades, diamonds, clubs }
D) enumerated type Suit = {hearts, spades, diamonds, clubs };
E) enum Suit = { hearts, spades, diamonds, clubs }
C
Explanation: Choice c represents the correct syntax for declaring an enumerated type called Suit that can take one of the values hearts, spades, diamonds or clubs.
2
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?

A) String prefix = listing.front(5);
B) String prefix = listing.front(6);
C) String prefix = listing.substring(1,5);
D) String prefix = listing.substring(0,5);
E) String prefix = listing.firstChars(5);
D
Explanation: Choices a, b, and e are wrong because the String class does not have methods called front or firstChars. Choice c is incorrect because the first character in the string is indexed by 1. Choice c is correct, since it will create a new string from the first 5 characters of listing.
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?

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
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.
4
When there are no references to an object, the object is marked as garbage and is automatically removed from memory using Java's automatic garbage collection feature.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
5
Which of the following classes include the getCurrencyInstance() method?

A) String
B) NumberFormat
C) DecimalFormat
D) Math
E) none of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
6
When an object variable is declared but it is not assigned a value, it is called a ______________________.

A) null reference
B) empty reference
C) void reference
D) zero reference
E) static reference
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
7
Which of the following expressions correctly compute 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
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
8
String objects can be changed after instantiation.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
9
Multiple reference variables can refer to the same object.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
10
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
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
11
Which of the following represents the proper way to create a NumberFormat object that formats numbers as percentages?

A) NumberFormat fmt = new NumberFormat(%);
B) NumberFormat fmt = new NumberFormat("%");
C) NumberFormat fmt = NumberFormat.getPercentInstance();
D) NumberFormat fmt = new PercentNumberFormat();
E) none of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
12
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.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
13
____________________ 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
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
14
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
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
15
The new operator is used to access an objects methods.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
16
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.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
17
The ________________ operator is used to instantiate an object.

A) static
B) new
C) +
D) -
E) none of the above
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
18
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.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
19
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) randNum will hold a number between 1 and 20 inclusive.
B) randNum will hold a number between 0 and 20 inclusive.
C) randNum 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
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
20
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.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
21
Write a short program that asks the user to input a string, and then outputs the number of characters in the string.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
22
Write a single statement that creates a DecimalFormat object that formats numbers to 2 decimal places.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
23
When called with integer parameter n, the nextInt() method of the Random class will return a randomly generated integer between 0 and n.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
24
The Math class is part of the java.lang package.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
25
The System.out.printf() method is an alternative way to output information in Java.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
26
All of the classes contained in the java.util package are automatically included in every Java program.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
27
Suppose a Random object has been created called generator. Write a single statement that generates a random number in the range 73 to 100.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
28
Write a declaration for an enumerated type that represents the months of the year.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
29
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.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
30
What is the range of integers that will be generated by the following expression?
generator.nextInt(15) + 5;
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
31
Explain what it means for a String object to be immutable. Are there any workarounds for this?
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
32
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.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
33
Explain how variables representing objects and variables representing primitive types are different.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
34
Write an expression that will compute the length of the tangent of a right triangle, given one of the non-right angles. You may assume that this value is stored in a variable called angle.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
35
What is output by the following code fragment?
String hello = new String("Hello World!");
hello = hello.replace('H', 'W');
hello = hello.replace('W', 'H');
System.out.println(hello);
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
36
Write an expression that computes 12 raised to the power 4.3 and store the result in a double called result.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
37
Write a single line that creates a wrapper for an int variable num.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
38
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.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
39
Enumerated types allow a programmer to treat primitive data as objects.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
40
Write a statement that computes the square root of a variable called discriminant and stores the value in a variable called result.
Unlock Deck
Unlock for access to all 40 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 40 flashcards in this deck.