You are on page 1of 13

Java Programming 9th Edition Joyce

Farrell Test Bank


Visit to download the full and correct content document: https://testbankdeal.com/dow
nload/java-programming-9th-edition-joyce-farrell-test-bank/
Name: Class: Date:

Chapter 07: Characters, Strings, and the StringBuilder


True / False

1. A String variable name is not a simple data type.


a. True
b. False
ANSWER: True

2. The terms “literal string” and “string literal” have different meanings in Java programming.
a. True
b. False
ANSWER: False

3. When you compare Strings with the == operator, you are comparing their values, not their memory addresses.
a. True
b. False
ANSWER: False

4. When you must determine whether a String is empty, it is more efficient to compare its length to 0 than it is to use
the equals () method.
a. True
b. False
ANSWER: True

5. If you try to use an index that is greater than 0 or less than the index of the last position in the StringBuilder
object, you cause an error known as an exception and your program terminates.
a. True
b. False
ANSWER: False

6. The methods islowerCase() and toLowerCase() perform the same tasks.


a. True
b. False
ANSWER: False

7. In a compareTo() method, programmers often do not need specific return values but want to determine if a value is
positive or negative.
a. True
b. False
ANSWER: True

8. When working with Strings, it is necessary to explicitly set a String to null.


a. True
b. False
ANSWER: False

Copyright Cengage Learning. Powered by Cognero. Page 1


Name: Class: Date:

Chapter 07: Characters, Strings, and the StringBuilder


9. Any string value that is input into a program can be used in arithmetic operations.
a. True
b. False
ANSWER: False

10. The StringBuffer class is more efficient than the StringBuilder class because it can execute multiple threads
during program execution.
a. True
b. False
ANSWER: False

Multiple Choice

11. A(n) ____ is a variable that holds a memory address.


a. reference b. Character
c. buffer d. immutable
ANSWER: a

12. A ____ is a class for storing and manipulating changeable data that is composed of multiple characters.
a. Character b. String
c. StringVariable d. StringBuilder
ANSWER: d

13. The methods of the Character class that begin with ____ return a character that has been converted to the stated
format.
a. is b. to
c. for d. in
ANSWER: b

14. The Character class ____ from java.lang.Object.


a. calls b. inherits
c. refers d. spawns
ANSWER: b

15. A literal string is a(n) ____ object.


a. lost b. anonymous
c. class d. named
ANSWER: b

16. Strings and other objects that can’t be changed are known as ____.
a. string constants b. accessor methods
c. immutable d. garbage
ANSWER: c

17. When you declare a variable of a basic, primitive type, such as int x = 10;, the memory address where x is
Copyright Cengage Learning. Powered by Cognero. Page 2
Name: Class: Date:

Chapter 07: Characters, Strings, and the StringBuilder


located holds the ____.
a. location in memory of 10 b. memory address of 10
c. reference to 10 d. value of 10
ANSWER: d

18. The String class ____ method evaluates the contents of two String objects to determine if they are equivalent.
a. equals() b. toString()
c. charAt() d. replace()
ANSWER: a

19. A(n) ____ comparison is based on the integer Unicode values of the characters.
a. integer b. symbol
c. lexicographical d. character
ANSWER: c

20. The method header of the equals() method within the String class is ____.
a. public boolean equals(Strings)
b. private boolean equals(String s)
c. public boolean equals(String s)
d. public boolean equals(Character s)
ANSWER: c

21. When the String class ____ method is used to compare two Strings, it provides additional information to the user
in the form of an integer value.
a. toString() b. compareTo()
c. equals() d. equalsIgnoreCase()
ANSWER: b

22. The ____ method returns the length of a String.


a. getSize() b. getLength()
c. size() d. length()
ANSWER: d

23. The ____ method allows you to replace all occurrences of some character within a String.
a. substring() b. toString()
c. replaceCharacter() d. replace()
ANSWER: d

24. The ____ method converts any object to a String.


a. convertString() b. toString()
c. replace() d. substring()
ANSWER: b

25. To convert a String to an integer, you use the ____ class, which is part of java.lang and is automatically imported

Copyright Cengage Learning. Powered by Cognero. Page 3


Name: Class: Date:

Chapter 07: Characters, Strings, and the StringBuilder


into programs you write.
a. StringBuilder b. String
c. Integer d. Convert
ANSWER: c

26. The ____ method takes a String argument and returns its double value.
a. parseString() b. parseInt()
c. parseDouble() d. returnDouble()
ANSWER: c

27. Consider the statement:


int anInt = Integer.parseInt("649");

You can tell that parseInt() is a(n) ____ method because you use it with the class name and not with an object.
a. programmer-defined b. instance
c. static d. immutable
ANSWER: c

28. A StringBuilder object contains a memory block called a _____, which might or might not contain a string.
a. capacity b. buffer
c. reference d. thread
ANSWER: b

29. The creators of Java chose ____ characters as the “extra” length for a StringBuilder object.
a. 12 b. 16
c. 36 d. 48
ANSWER: b

30. The ____ method lets you add characters at a specific location within a StringBuilder object.
a. charAt() b. insert()
c. append() d. setCharAt()
ANSWER: b

31. To alter just one character in a StringBuilder, you can use the ____ method, which allows you to change a
character at a specified position within a StringBuilder object.
a. charAt() b. insert()
c. append() d. setCharAt()
ANSWER: d

32. String oneStr = "Welcome Jim"


String twoStr = "Welcome Joe"

Given the lines of code above, which of the following regionMatches() expressions will result in a value of true?
a. oneStr.regionMatches(8, twoStr, 8, b. oneStr.regionMatches(0, twoStr, 0,
3) 7)
c. oneStr.regionMatches(8, twoStr, 0, d. oneStr.regionMatches(0, twoStr, 8,
Copyright Cengage Learning. Powered by Cognero. Page 4
Name: Class: Date:

Chapter 07: Characters, Strings, and the StringBuilder


3) 3)
ANSWER: b

33. Assume that aName refers to Roger. What is returned by


aName.compareTo("Robert");
a. 0 b. positive number
c. negative number d. false
ANSWER: b

34. Which of the following correctly declares and initializes a String object?
a. new String = Hello b. String greeting == "Hello";
c. String greeting = "Hello"; d. String new = "Hello"
ANSWER: c

35. System.out.println(“Your name is " + yourName);

The above statement is an example of ____, which is used to join Strings.


a. concatenation b. referencing
c. parsing d. buffering
ANSWER: a

36. The ____ class contains standard methods for testing the values of characters.
a. char b. character
c. StringChar d. Character
ANSWER: d

37. The ____ method and the startsWith() method each take a String argument and return true or false if
a String object does or does not end or start with the specified argument, respectively.
a. beginsWith() b. strEnd()
c. endsWith() d. length()
ANSWER: b

38. The String class ____ method is similar to the equals() method. As its name implies, this method does not
consider case when determining if two Strings are equivalent.
a. equalsCase() b. equalsOmitCase()
c. equalsAllCase() d. equalsIgnoreCase()
ANSWER: a

39. A literal string is called a(n) ____.


a. anonymous object b. String variable
c. empty string d. wrapper
ANSWER: a

40. The ____ method requires an integer argument that indicates the position of the character at that position, starting at 0.
a. charAt() b. setCharAt()
Copyright Cengage Learning. Powered by Cognero. Page 5
Name: Class: Date:

Chapter 07: Characters, Strings, and the StringBuilder


c. strAt() d. posChar()
ANSWER: a

Matching

Match each term with the correct statement below.


a. parseDouble()
b. toLowerCase()
c. substring()
d. String variable
e. append()
f. buffer
g. setLength()
h. indexOf()
i. insert()
j. anonymous object
k. immutable
l. concatenation
m. wrapper
n. buffer
o. capacity

41. Returns the lowercase equivalent of the argument


ANSWER: b

42. An unnamed object


ANSWER: j

43. Appending strings


ANSWER: l

44. Determines whether a specific character occurs within a String


ANSWER: h

45. Memory block


ANSWER: n

46. Takes a String argument and returns its double value


ANSWER: a

47. Object that can't be changed


ANSWER: k

48. Add characters to the end of a StringBuilder object


ANSWER: e
Copyright Cengage Learning. Powered by Cognero. Page 6
Name: Class: Date:

Chapter 07: Characters, Strings, and the StringBuilder

49. Add characters at a specific location within a StringBuilder object


ANSWER: i

50. A named object of the String class


ANSWER: d

51. Class or object that wraps around a simpler element


ANSWER: m

52. A memory block


ANSWER: f

53. Length of a buffer


ANSWER: o

54. Change the length of a string in a StringBuilder object


ANSWER: g

55. Takes two integer arguments: a start position and an end position
ANSWER: c

Subjective Short Answer

56. Describe how a string is a reference and how a string comparison is done.
ANSWER: As an object, a String variable name is not a simple data type—it is a reference; that is, a variable that holds
a memory address. Therefore, when you compare two Strings using the == operator, you are not comparing
their values, but their computer memory locations.

57.

In the above code, two strings are evaluated using the equivalency operator. Why can this be a problem, and what other
methods are offered by Java for working with characters and strings?
ANSWER: The problem stems from the fact that in Java, String is a class, and each created String is an object. As an
object, a String variable name is not a simple data type. It is a reference; that is, a variable that holds a

Copyright Cengage Learning. Powered by Cognero. Page 7


Name: Class: Date:

Chapter 07: Characters, Strings, and the StringBuilder


memory address. Therefore, when you compare two String objects using the == operator, you are
comparing not their values but the computer memory locations.
Programmers want to compare the contents of memory locations (the values stored there)
more frequently than they want to compare the addresses of the locations. Fortunately, the creators of Java
have provided three classes that you can use when working with text data; these classes provide you with
many methods that make working with characters and strings easier:
Character - A class whose instances can hold a single character value and whose methods manipulate and
inspect single-character data
String - A class for working with fixed-string data; unchanging data composed of multiple characters

StringBuilder and StringBuffer - Classes for storing and manipulating changeable data composed
of multiple characters

58. Describe how a programmer would use the two types of Character class methods (those that begin with “is” and
those that begin with “to”) for testing the values of characters.
ANSWER: The methods that begin with “is”, such as isUpperCase(), return a Boolean value that can be used in
comparison statements; the methods that begin with “to”, such as toUpperCase(), return a character that
has been converted to the stated format.

59. Explain what is needed to declare a String variable and provide an example.
ANSWER: When you declare a String object, the String itself—that is, the series of characters contained in the
String—is distinct from the identifier you use to refer to it. You can create a String object by using the
keyword new and the String constructor, just as you would create an object of any other type. For example,
the following statement defines an object named aGreeting, declares it to be of type String, and assigns
an initial value of “Hello” to the String:
String aGreeting = new String("Hello");.

60. How is the String class equalsIgnoreCase() method like the equals() method and how would you use it?
Give an example.
ANSWER: The String class equalsIgnoreCase() method is similar to the equals() method. As its name
implies, this method ignores case when determining if two Strings are equivalent. Thus, if you declare a
String as String aName = "Carmen";, then aName.equals("caRMen") is false, but
aName.equalsIgnoreCase("caRMen") is true since the equalsIgnoreCase() method allows
you to test entered data without regard to capitalization. This method is useful when users type responses to
prompts in your programs. You cannot predict when a user might use the Shift key or the Caps Lock key
during data entry.

61. yourCounty = "perry";


yourCounty.equals("Perry");
yourCounty.equalsIgnoreCase("Perry");

Using the above statements, what value will be returned by the equals() method? What will be the value of the
equalsIgnoreCase() method? Explain how the equalsIgnoreCase() method can be useful when users type
responses to prompts in programs.
ANSWER: The String class equalsIgnoreCase() method is similar to the equals() method. As its name
implies, this method ignores case when determining if two Strings are equivalent. Thus, if you declare a
String as String yourCounty = "perry", then yourCounty.equals("Perry")is false,
Copyright Cengage Learning. Powered by Cognero. Page 8
Name: Class: Date:

Chapter 07: Characters, Strings, and the StringBuilder


but yourCounty.equalsIgnoreCase("Perry") is true. This method is useful when users type
responses to prompts in your programs. You cannot predict when a user might use the Shift key or the Caps
Lock key during data entry.

62. String example1 = "";


String example2 = null;
String example3;

There is an important difference between empty Strings and null Strings. Explain the differences and why it is
important when making String comparisons.
ANSWER: The empty String example1 references a memory address where no characters are stored. The null
String example2 uses the Java keyword null so that example2 does not yet hold a memory address.
The unassigned String example3 is also a null String by default. A significant difference between
these declarations is that example1 can be used with the String methods, but example2 and example3
cannot. For example, assuming a String named anotherExample has been assigned a value, then the
comparison string1.equals(anotherExample) is valid, but
example2.equals(anotherExample) causes an error.

63. How can you use String methods to change the case of a string? Provide an example.
ANSWER:
The methods toUpperCase() and toLowerCase() convert any String to its uppercase or
lowercase equivalent. For example, if you declare a String as String aWord = "something";, then
the string “something” is created in memory and its address is assigned to aWord. The statement aWord =
aWord.toUpperCase(); creates “SOMETHING” in memory and assigns its address to aWord. Because
aWord now refers to “SOMETHING,” aWord = aWord.toLowerCase(); alters aWord to refer to
“something”.

64. String greeting = "Welcome back";

Using the above statement, write the length() method that will return the length of the greeting String. Store
the length in an integer named greetingLength.
ANSWER: int greetingLength = greeting.length();

65. String greeting = "Welcome Home";

Using the above String, create the String method to convert the greeting String to all uppercase. Likewise,
create the String method to convert the greeting String to all lowercase.
ANSWER: To convert to uppercase:
greeting = greeting.toUpperCase();

To convert to lowercase:
greeting = greeting.toLowerCase();

66. String aName = "Michael"

Using the above statement, write the length() method that will return the length of the aName String. What value
will the length() method return when executed?
ANSWER: int len = aName.length();

Copyright Cengage Learning. Powered by Cognero. Page 9


Name: Class: Date:

Chapter 07: Characters, Strings, and the StringBuilder


The length() method will return a value of 7.

67. myCounty = "Clark Jackson Scioto"


myCounty.charAt(6)

Using the above code, what will be the value of the charAt() method once the code executes? Explain how the
charAt() method operates.
ANSWER: The charAt() method requires an integer argument that indicates the position of the character that the
method returns, starting with 0. In the above example, myCounty is a String that is examined by the
charAt() method. The value of myCounty.charAt(6) is “J”.

68. What is the purpose of the replace() method? Provide an example.


ANSWER: The replace() method allows you to replace all occurrences of some character within a String. For
example, if String yourName = "Annette";, then String goofyName =
yourName.replace('n', 'X'); assigns “AXXette” to goofyName.

69. yourCounty = "Clark"


yourCounty.startsWith("Cla")

What value will be returned by the startsWith() method? Explain how the startsWith() method deals with case
sensitivity.
ANSWER: The startsWith() method takes a String argument and returns true or false if the String object
does or does not end or start with the specified argument. In this example, String yourCounty =
"Clark" and yourCounty.startsWith("Cla") is true. These methods are case sensitive, so if
String yourCounty had been "clark", the value returned would have been false.

70. What is the purpose of the substring() method and how do you use it?
ANSWER: You can extract part of a String with the substring() method, and use it alone or concatenate it with
another String. The substring() method takes two integer arguments—a start position and an end
position—that are both based on the fact that a String’s first position is position zero. The length of the
extracted substring is the difference between the second integer and the first integer; if you write the method
without a second integer, the substring extends to the end of the original string.

71. import javax.swing.JOptionPane;


public class Practice
{
public static void main(String[] args)
{
String fullName;
char firstLetter;
____
____

}
}

Accepting a String from a user is common practice. Using the above code, write the statements to accept a String
response from a user. Prompt the user with “Enter your name”. Then use the charAt() method to extract the first
character of the String.
Copyright Cengage Learning. Powered by Cognero. Page 10
Name: Class: Date:

Chapter 07: Characters, Strings, and the StringBuilder


ANSWER: fullName = JOptionPane.showInputDialog(null, "Enter your name");
cha = fullName.charAt(0);

72. String firstString = "Oak Maple Pine"


String secondString = "spruce Maple elm"
firstCompare.regionMatches(4, secondString, 7, 5)

Using the above code, what will be the Boolean value after execution of the regionMatches statement? Explain
how the Strings are compared using the regionMatches() method.
ANSWER: The regionMatches() method can be used to test whether two String regions are the same. One
version of the regionMatches() method takes four arguments—the position at which to start in the
calling String, the other String being compared, the position to start in the other String, and the length of the
comparison.
The regionMatches() expression is true. The substring of firstString that starts at position 5 and
continues for five characters is “Maple”; the substring of secondString that starts at position 7 and
continues for five characters is also “Maple”.

73. What is a wrapper and why would you use it?


ANSWER:
A wrapper is a class or object that is “wrapped around” a simpler element; the Integer wrapper class
contains a simple integer and useful methods to manipulate it.

74. How can you change the length of a String in a StringBuilder object?
ANSWER: You can change the length of a string in a StringBuilder object with the setLength() method. The
length of a StringBuilder object equals the number of characters in the String contained in the
StringBuilder. When you increase a StringBuilder object’s length to be longer than the String it
holds, the extra characters contain ‘\u0000’. If you use the setLength() method to specify a length shorter
than its String, the string is truncated.

75. Why would you use the append() method? Provide an example.
ANSWER: The append() method lets you add characters to the end of a StringBuilder object. For example, the
following two statements together declare phrase to hold “Happy” and alter the phrase to hold “Happy
birthday”:
StringBuilder phrase = new StringBuilder("Happy");
phrase.append(" birthday");

76. Strings and other objects that can't be changed are said to be immutable. What does this mean and how does it
relate to values held in memory addresses?
ANSWER: In Java, the value of a String is fixed after the String is created; Strings are immutable, or unchangeable. When
you write someString = "Hello"; and follow it with someString = "Goodbye";, you have neither changed the
contents of computer memory at the address represented by someString nor eliminated the characters
“Hello”. Instead, you have stored “Goodbye” at a new computer memory location and stored
the new address in the someString variable.

77. StringBuilder greeting = new StringBuilder("Hello, John");


char initial = greeting.charAt(7);

Using the above code and the charAt() method, what value will be assigned to the variable initial?
Copyright Cengage Learning. Powered by Cognero. Page 11
Name: Class: Date:

Chapter 07: Characters, Strings, and the StringBuilder


ANSWER: The charAt() method will return a “J”, which is the seventh character in the String “Hello, John” since
the starting index value is 0.

78. StringBuilder greeting = new StringBuilder("Welcome home ");


phrase.insert(13, "Joe");

Using the above code and insert() method, describe how the phrase will appear after the code executes. How does the
insert() method work?
ANSWER: The String will appear as “Welcome home Joe”. The insert() method lets you add characters at a
specific location within a StringBuilder object. The first character in the StringBuilder object
occupies position zero.

79. StringBuilder greeting = new StringBuilder("Welcome");

Use the append() method to add the characters “home” to the end of the StringBuilder object created above.
ANSWER: greeting.append(" home");
(Be sure that the String in double quotes has a blank space at the start. Otherwise, the Strings
"Welcome" and "home" will be appended without a blank space.)

80. StringBuilder greeting = new StringBuilder("Day 1");

Using the above StringBuilder, create a setCharAt() method that will change the “1” to a “2” in the String
"Day 1". Explain how the setCharAt() method operates.
ANSWER: greeting.setCharAt(4, '2');
To alter just one character in a StringBuilder, you can use the setCharAt() method, which allows
you to change a character at a specified position within a StringBuilder object. This method requires two
arguments: an integer position and a character. In the phrase “Day 1”, the greeting.setCharAt(4,
'2'); changes the value 1 to 2.

81. Give a code example of using the equivalence operator ("==") to compare the addresses of two strings where the
comparison will result in the value true. The example should produce output indicating the strings have the same value.
ANSWER: string stringA="Hello";
string StringB="Hello";
if(stringA==stringB)
System.out.println("stringA has the same address value as stringB");

82. What happens when you increase a StringBuilder object's length to be longer than the string it holds? What
happens if you set the length of a string to be shorter than the string it contains?
ANSWER: When you increase a StringBuilder object’s length to be longer than the String it holds, the extra
characters contain ‘\u0000’. If you use the
setLength() method to specify a length shorter than its String, the string is truncated.

Copyright Cengage Learning. Powered by Cognero. Page 12

You might also like