Skip to main content

Section 4.16 Assessment: String methods

Subgoals for using objects (creating instances).

  1. Declare variable of appropriate class datatype.
  2. Assign to variable: keyword new, followed by class name, followed by ().
  3. Determine whether parameter(s) are appropriate (API)
    1. Number of parameters
    2. Data types of the parameters

Exercises Exercises

    1.

      Q1: What is the output of the following code?
      String temp = "abcde";
      System.out.println(temp.length());
      
    • 0
    • 4
    • 5
    • 6
    • an error occurs

    2.

      Q2: What is the output of the following code?
      String temp = "abcdefghijklmnopqrstuvwxyz";
      System.out.println(temp.indexOf("fgk"));
      
    • 0
    • 5
    • 6
    • -1
    • an error occurs

    3.

      Q3: What is the output of the following code?
      String temp = "catcatcat";
      System.out.println(temp.indexOf("cat"));
      
    • 0
    • 3
    • 4
    • 6
    • 7

    4.

      Q4: What is the output of the following code?
      String temp = "abcdefghijklmnopqrstuvwxyz";
      System.out.println(charAt.indexOf(3));
      
    • c
    • d
    • e
    • f

    5.

      Q5: What is the output of the following code?
      String temp = "onetwothreefour";
      System.out.println(temp.substring(3,7));
      
    • one
    • two
    • three
    • four
    • an error occurs

    6.

      Q6: What is the output of the following code?
      String temp = "onetwothreefour";
      System.out.println(temp.substring(6));
      
    • three
    • four
    • threefour
    • twothreefour
    • an error occurs
You have attempted of activities on this page.