Exam 7: Characters, Strings, and the Stringbuilder

arrow
  • Select Tags
search iconSearch Question
flashcardsStudy Flashcards
  • Select Tags

Strings that cannot be changed are said to be immutable. What does this mean and how does it relate to values held in memory addresses?

Free
(Essay)
4.8/5
(40)
Correct Answer:
Verified

When you reference a String, you actually are accessing the address of the characters that make up the String. If you subsequently assign a new value to the String, the address held by the original String is altered . The String now holds a new address where the characters are stored. The original String is still in memory, but the String no longer holds its address. Eventually, a part of the Java system called the garbage collector discards the String characters. Strings, therefore, are never actually changed; instead, new Strings are created and String references hold the new addresses. Strings that can't be changed are called immutable.

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.

Free
(Essay)
4.8/5
(41)
Correct Answer:
Verified

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.

Match each term with the correct statement below. -Returns the lowercase equivalent of the argument

Free
(Multiple Choice)
4.9/5
(42)
Correct Answer:
Verified

B

System.out.println("Your name is " + yourName); The above statement is an example of ____, which is used to join Strings.

(Multiple Choice)
4.8/5
(34)

What is the purpose of the substring() method and how do you use it?

(Essay)
4.7/5
(49)

Match each term with the correct statement below. -Add characters to the end of a StringBuilder object

(Multiple Choice)
4.7/5
(34)

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?

(Essay)
4.9/5
(33)

In Java, to ____________________ a String means to break down its separate characters into a numeric format.

(Short Answer)
4.9/5
(45)

Strings and other objects that can't be changed are known as ____.

(Multiple Choice)
4.9/5
(46)

Which of the following correctly declares and initializes a String object?

(Multiple Choice)
4.8/5
(40)

A literal string is a(n) ____ object.

(Multiple Choice)
4.8/5
(35)

To convert a String to an integer, you use the valueOf() method of the ____ class.

(Multiple Choice)
4.8/5
(40)

When you compare Strings with the == operator, you are comparing their values, not their memory addresses.

(True/False)
5.0/5
(40)

The StringBuilder class is more efficient than the StringBuffer class because it can execute multiple threads during program execution.

(True/False)
4.9/5
(41)

You can tell that the equals() method takes a ____ argument because parentheses are used in the method call.

(Multiple Choice)
4.8/5
(33)

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.

(Essay)
4.8/5
(45)

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.

(Short Answer)
4.8/5
(31)

What is a wrapper and why would you use it?

(Essay)
4.8/5
(40)

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.

(Essay)
4.7/5
(35)

The ____________________ class contains standard methods for testing the values of characters.

(Short Answer)
4.9/5
(42)
Showing 1 - 20 of 73
close modal

Filters

  • Essay(0)
  • Multiple Choice(0)
  • Short Answer(0)
  • True False(0)
  • Matching(0)