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);
  }
}

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

  zero

  1

  minus 1

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

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

  stringcon()

  concatenate()

  con()

  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, _____________________________.

  you will receive a runtime error.

  a value of -1 will be returned

  a 0 will be returned

  a single 1 will be returned

 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);
  }
}

  Space

  E

  Error

  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);
  }
}

  An error

  The entire string "World!"

  The first six characters, in this case "Hello "

  The character 'W"

 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

  immutable

  objects

  class operators

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

  FALSE

  TRUE

 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 ____________.

  an error is thrown

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

  it finds a string that ends with an integer

  it finds the first instance of a difference.

 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

  FALSE

  TRUE

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

  FALSE

  TRUE

 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

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

  I and III

  II only

  None of the statements would evaluate to 'true'

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

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

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

  converts the 'Hello World!' into lowercase

  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);
  }
}

  const

  var

  string

  int

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

  TRUE

  FALSE

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

  TRUE

  FALSE

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

  any user input (only text input)

  any single digit integer

  almost any object

  any character (letter or integer)

 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 );
   }
}

  TRUE

  FALSE

 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());
   }
}

  Welcome

  Return Value: TestandTrack

  Return Value :Welcome to TestandTrack

  Return Value: Welcome

 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"));
   }
}

  False

  True

 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

  False

  True

 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 lastIndexOf(String str)

  int indexOf(String str, int fromIndex)

  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 first and the last character trimmed (removed)

  the last character removed

  leading and trailing white space removed

  x amount of characters removed from the end of the string