AP Computer Science A · Topic 2.7

String Methods Practice

Part of Using Objects.

Practice questions

37

Want a predicted score for the whole AP CSA exam? Take the 20-question diagnostic and Lumi will plan the rest.

Sample questions

5 of 37 — sign in to practice the rest with adaptive difficulty and mastery tracking.

  1. Sample 1difficulty 1/5

    What does <code>"".length()</code> return?

    • A

      Throws exception

    • B

      0

      check_circle
    • C

      1

    • D

      null

    Why

    Empty string has length 0.

  2. Sample 2difficulty 1/5

    <code>"hello".length()</code> returns

    • A

      4

    • B

      Compile error

    • C

      5

      check_circle
    • D

      6

    Why

    Five characters.

  3. Sample 3difficulty 2/5

    String s = "abcdef";
    System.out.println(s.indexOf("cd"));

    What is printed?

    • A

      2

      check_circle
    • B

      3

    • C

      1

    • D

      -1

    Why

    The substring "cd" begins at index 2 in "abcdef".

  4. Sample 4difficulty 2/5

    String a = "Cat";
    String b = "cat";
    System.out.println(a.equalsIgnoreCase(b));

    What is printed?

    • A

      1

    • B

      0

    • C

      true

      check_circle
    • D

      false

    Why

    equalsIgnoreCase compares without regard to case, so "Cat" and "cat" are considered equal.

  5. Sample 5difficulty 2/5

    String s = "";
    System.out.println(s.length());

    What is printed?

    • A

      null

    • B

      Error

    • C

      0

      check_circle
    • D

      1

    Why

    The empty string contains zero characters, so length() returns 0.