What does substring() do when copying a whole String?

Prepare for the Computer Programming Test. Study with flashcards and multiple choice questions, each question has hints and explanations. Get ready for your exam!

Multiple Choice

What does substring() do when copying a whole String?

Explanation:
substring creates a new String that contains just the characters from the original string within the specified range. It doesn’t change the original string, since strings are immutable. If you copy the whole string, you’d use the full range (start at 0 and go to the string’s length), and you’d get a new String containing all of its characters. More generally, you can pick a smaller range to get just part of it. This behavior is why it’s described as returning a new String containing part or all of the original. It isn’t designed to duplicate the string or to return only the first character unless you request a range that yields exactly that.

substring creates a new String that contains just the characters from the original string within the specified range. It doesn’t change the original string, since strings are immutable. If you copy the whole string, you’d use the full range (start at 0 and go to the string’s length), and you’d get a new String containing all of its characters. More generally, you can pick a smaller range to get just part of it. This behavior is why it’s described as returning a new String containing part or all of the original. It isn’t designed to duplicate the string or to return only the first character unless you request a range that yields exactly that.

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy