Strings Practice Problems


  1. Give an expression that can be used to check whether or not the Strings references by two variables (Strings x and y) are identical.

  2. What is string concatenation and what operator do you use to concatenate two Strings?

  3. What is the difference between System.out.print and System.out.println?

  4. What expression will return the number of characters in a String referenced by a variable called str?

  5. What escape sequence would you use in a String to indicate the new line character?

  6. Write a single statement that will assign to a String the word "Hello" followed by the sum of the values in two ints i and j followed by the word "There".

  7. Write three statements which will (1)create a reference to a new String containing the word "Hello", (2)use the += operator to append the text "There" to the existing String, and (3)print it using the reference.

  8. Write three statements which will (1)create a reference to a new StringBuffer containing the word "Hello", (2)use the append method to append the text "There" to this StringBuffer, and (3)print it using the reference.

  9. Create StringBuffer object that contain each of the following words and use the capacity() method to see how much room has been allocated behind the scenes by Java. (1)Hello, (2)HelloThere, (3)ABCDEFGHIJKLMNOPQRSTUVWXYZ