2.2 String Methods

Cards (72)

  • Characters in a String can be accessed using indexing
  • In the code `String greeting = "Hello"; char firstChar = greeting.charAt(0);`, the variable `firstChar` is assigned the value 'H'
  • The `charAt()` method returns a character at a specific index in the string.
    True
  • In the code `String str = "Hello World"; boolean isEqual = str.equals("Hello World");`, the variable `isEqual` is assigned the value true
  • Calling `“Hello World”.toUpperCase()` returns the string “HELLO WORLD”
  • The `length()` method returns the number of characters
  • The `substring()` method includes the character at the `endIndex` in the extracted substring
    False
  • What is the value of `charAtIndex` after executing `str.charAt(0)` in the example code?
    'H'
  • What is the purpose of the `toUpperCase()` method?
    Converts to uppercase
  • What is the value of `lowerCaseText` after executing `text.toLowerCase()` in the example code?
    hello world
  • The `substring()` method includes the character at the `endIndex`.
    False
  • The `indexOf()` method returns the index of the first occurrence of the specified substring
  • Strings in Java are immutable.

    True
  • The Integer class in Java is immutable.
    False
  • Strings in Java are mutable
    False
  • The `charAt(int index)` method returns the character at the specified index
  • What does the `equals(String other)` method compare in Java strings?
    Case-sensitive equality
  • What are the parameters of the `substring()` method in Java?
    beginIndex, endIndex
  • Concatenation in Java creates a new string because strings are immutable
  • The `concat()` method can be used for method chaining
  • What advantage does the `concat()` method offer over the `+` operator in Java?
    Method chaining
  • Strings in Java are mutable.
    False
  • Strings in Java can be changed after they are created.
    False
  • In the code `String str = "Hello World"; int len = str.length();`, the variable `len` is assigned the value 11
  • What is the value of `sub` in the code `String str = "Hello World"; String sub = str.substring(0, 5);`?
    "Hello"
  • The `toUpperCase()` method converts all characters in a string to lowercase.
    False
  • What does the `equalsIgnoreCase()` method do in Java?
    Compares strings case-insensitively
  • What is the return value of `str.length()` in the example code?
    11
  • The `isEqual` variable is assigned `true` because `str.equals("Hello World")` returns a case-sensitive comparison.

    True
  • What is the value of `upperCaseText` after executing `text.toUpperCase()` in the example code?
    HELLO WORLD
  • What is the return value of the `length()` method?
    Number of characters
  • What is the value of `subtext` after executing `text.substring(0, 5)` in the example code?
    Hello
  • What is the value of `lastIndex` after executing `text.lastIndexOf("Hello")` in the example code?
    12
  • What is the value of `fullName` after executing `firstName + " " + lastName` in the example code?
    John Doe
  • How can characters in a string be accessed in Java?
    Using indexing
  • What does the String class in Java represent?
    Sequences of characters
  • What does the `length()` method of the String class return?
    Number of characters
  • The `substring(int beginIndex, int endIndex)` method includes the character at the `endIndex`
    False
  • Match the method with its purpose:
    `toUpperCase()` ↔️ Converts to uppercase
    `toLowerCase()` ↔️ Converts to lowercase
  • What does the `indexOf(String str)` method return if the substring is not found?
    -1