Preview

14 - String Methods

 1. Analyse the following code. An empty string has a length of _______.
public class StringLengthExample {

  public static String exampleVariableOne = "Hello World!";
  public static String exampleVariableTwo = "";
  // returns the length of exampleVariableOne and exampleVariableTwo
  // and assigns it to lengthOne and lengthTwo
  public static int lengthOne = exampleVariableOne.length();
  public static int lengthTwo = exampleVariableTwo.length();

  public static void main(String[] args) {
    System.out.println(lengthOne);
    System.out.println(lengthTwo);
  }
}

  1

  Null (as you cannot find the length of an empty string)

  minus 1

  zero

 2. What is the length of the String "Hello World!"?

 3. The '+' operator can be used to concatenate strings as well as the _________ method.

  con()

  stringcon()

  concatenate()

  concat()

 4. What is the output of the following code? (note the indexOf method)
String varOne = "Hello World!";
System.out.println(varOne.indexOf('W'));

 5. While using the charAt method, in a case where the index is negative or greater than stringname.length()-1, _____________________________.

  a value of -1 will be returned

  a 0 will be returned

  a single 1 will be returned

  you will receive a runtime error.

 6. Refer to the following code. What is printed when the statement exampleVariableOne.charAt(0); is called?
public class CharAtMethodExample {

  public static String exampleVariableOne = "Hello World!";
  // returns the character at a specific index
  // and assigns it to char variables
  public static char exampleVariableTwo = exampleVariableOne.charAt(0);
  public static char exampleVariableThree = exampleVariableOne.charAt(11);
  public static char exampleVariableFour = exampleVariableOne.charAt(6);

  public static void main(String[] args) {
    System.out.println(exampleVariableTwo);
    System.out.println(exampleVariableThree);
    System.out.println(exampleVariableFour);
  }
}

  Error

  E

  Space

  H

 7. Refer to the code below. What does the statement: exampleVariableOne.substring(6) return?
public class SubStringExample {

  public static String exampleVariableOne = "Hello World!";
  // returns a String between the given indices and assigns
  // it to String variables
  public static String exampleVariableTwo = exampleVariableOne.substring(0, 5);
  public static String exampleVariableThree = exampleVariableOne.substring(6);

  public static void main(String[] args) {
    System.out.println(exampleVariableTwo);
    System.out.println(exampleVariableThree);
  }
}

  The character 'W"

  The entire string "World!"

  An error

  The first six characters, in this case "Hello "

 8. Referring to the above code, fill in the blanks. Strings are ___________, so a new String is created every time you call either of the substring() methods.

  variable in nature

  objects

  immutable

  class operators

 9. The following code will print the string "Hello". True or False?
String varOne = "   Hello   ";
System.out.println(varOne.trim());

  TRUE

  FALSE

 10. In the compareTo() method, if the first two letters of the strings are the same, the method will go on to compare the next letters, and the next until ____________.

  it finds the first instance of a difference.

  it finds a string that ends with an integer

  it finds the letter 'Z' (or the equivalent ASCII value)

  an error is thrown

 11. The compareTo() method returns 1 if the two strings are equal, a number less than 10 if the first String is larger, and a number greater than -1 if the second String is larger.

  TRUE

  FALSE

 12. When using the compareTo() method, uppercase letters come before lowercase letters when the method compares strings

  TRUE

  FALSE

 13. When the following code is executed, '2' is printed.
String varOne = "Hello World!";
String varTwo = "Java";
System.out.println(varOne.compareTo(varTwo));

  TRUE

  FALSE

 14. After this code is executed, which of the following statements will evaluate to true?
Code Snippet
========================
String varOne = "abc";
String varTwo = varOne;
String varThree = varTwo;

Select from these options
=========================
I. varOne.equals(varThree) 
II. varOne == varTwo 
III. varOne == varThree

  II only

  None of the statements would evaluate to 'true'

  I and III

  All of the statements i.e I, II and III would all evaluate to 'true'

 15. The following code simply takes the string and _______________.
String varOne = "Hello World!";
System.out.println(varOne.toLowerCase());

  converts the 'Hello World!' into lowercase

  converts the string into a lower base integer (to reduce size)

  adds one to the lower value (i.e the first character)

  converts the string into a Case Class string

 16. Analyse the following code. The parseInt() method returns an _____, not an object from the Integer class.
public class ParseIntExample {

  public static String exampleVariableOne = "5666";
  public static String exampleVariableTwo = "423";
  public static int exampleVariableThree = Integer.parseInt(exampleVariableOne);
  public static int exampleVariableFour = Integer.parseInt(exampleVariableTwo);

  public static void main(String[] args) {
    System.out.println("Integer exampleVariable of exampleVariableOne is " + exampleVariableThree);
    System.out.println("Integer exampleVariable of exampleVariableTwo is " + exampleVariableFour);
  }
}

  string

  var

  const

  int

 17. The parseInt() function has many uses but would NOT be useful when dealing with user input or calculating numbers from text files.

  FALSE

  TRUE

 18. The following statement would be considered correct.
int varOne = parseInt("222" + "A");

  FALSE

  TRUE

 19. The toString() method takes ____________ in Java and converts it to a String.

  any single digit integer

  any character (letter or integer)

  almost any object

  any user input (only text input)

 20. Read the following and decide whether the output listed at the start is correct. Select 'True' if correct, and 'False' if incorrect.
Decide whether the below code will produce the following result:
================================================================

Returned Value = false
Returned Value = true

Code Snippet
=============
public class Test {

   public static void main(String args[]) {
      String Str = new String("This is really not immutable!!");
      boolean retVal;

      retVal = Str.endsWith( "immutable!!" );
      System.out.println("Returned Value = " + retVal );

      retVal = Str.endsWith( "immu" );
      System.out.println("Returned Value = " + retVal );
   }
}

  FALSE

  TRUE

 21. What is the output of the following code?
import java.io.*;
public class Test {

   public static void main(String args[]) {
      String Str = new String("Welcome to TestandTrack");

      System.out.print("Return Value :");
      System.out.println(Str.toString());
   }
}

  Return Value: Welcome

  Return Value: TestandTrack

  Welcome

  Return Value :Welcome to TestandTrack

 22. The following code will produce the output: Return Value :MOOSE
import java.io.*;
public class Test {

   public static void main(String args[]) {
      String Str = new String("Welcome to TestandTrack");

      System.out.print("Return Value :" );
      System.out.println(Str.replaceAll("(.*)Tutorials(.*)", "MOOSE"));
   }
}

  True

  False

 23. The following code snippet compares strings to find out if they are equal, ignoring case differences.
String myStr1 = "Hello";
String myStr2 = "HELLO";
String myStr3 = "Another String";
System.out.println(myStr1.equalsIgnoreCase(myStr2)); // true
System.out.println(myStr1.equalsIgnoreCase(myStr3)); // false

  True

  False

 24. Which of the following methods returns the index within this string of the first occurrence of the specified substring.

   int lastIndexOf(int ch)

  int indexOf(String str, int fromIndex)

   int lastIndexOf(String str)

  int indexOf(String str)

 25. Analyse the following method (see code below). This essentially returns a copy of the string with __________________.
public String trim()

  the last character removed

  x amount of characters removed from the end of the string

  the first and the last character trimmed (removed)

  leading and trailing white space removed