Strings

  1. Without explicitly calling any constructor in the String class, create a String that holds “Grace” and store its reference in a variable named firstName.
  2. Using the String constructor that takes a string literal as an argument, create a String that holds “Bob” and store its reference in a variable named name.
  3. Suppose an array named array has length 10 and holds 10 characters. Write a single statement that uses the first 5 characters in the array to initialize a String and store a reference to the String in a variable named firstFive.
  4. Suppose a variable named fullName holds a reference to a String. Write a single statement that creates a new String using all of the characters in fullName except the first and the last and sets a variable named subStr equal to the reference of the new String.
  5. Suppose variables str1 and str2 hold references to Strings. Write a block of code that prints “Same” if they refer to the same String on the heap.
  6. Suppose variables str3 and str4 hold references to Strings. Write a block of code that prints “Equal” if they store the same sequence of characters.
  7. Write a statement that stores in a variable named len the length of the character sequence stored in a String referenced by a variable named
  8. Suppose a variable named age is of type Integer. Write a single statement that creates a variable of type String that references a String that holds the printable representation of the Integer referenced by age.

© 2017 – 2020, Eric. All rights reserved.