Java String Methods Quiz
23 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

Which method is used to convert a String to a float?

  • Float.parseFloat() (correct)
  • String.toFloat()
  • Float.valueOf()
  • StringParser.float()
  • What is the result of explicitly casting the double 5.99 to an int?

  • 6
  • 5.0 (correct)
  • 5.99
  • 5 (correct)
  • How do you convert an int to a char?

  • (char) intValue (correct)
  • String.valueOf(intValue)
  • Character.toChar()
  • Character.fromInt()
  • What is the output of the following code: String str = 'HELLO'; String lower = str.toLowerCase(); System.out.println(lower);

    <p>hello</p> Signup and view all the answers

    Which statement correctly converts a long to a String?

    <p>String.valueOf(longValue)</p> Signup and view all the answers

    What will be the output of 'char c = str.charAt(0);' if str = 'World'?

    <p>'W'</p> Signup and view all the answers

    Which method is used to remove leading and trailing whitespace from a string?

    <p>trim()</p> Signup and view all the answers

    Which of the following statements correctly describes the split(String regex) method?

    <p>It splits the string into an array based on a regular expression.</p> Signup and view all the answers

    Which method should be used to check if a String can be converted to a boolean?

    <p>Boolean.parseBoolean(stringValue)</p> Signup and view all the answers

    What does the startsWith(String prefix) method return when checking if the string 'Java Programming' starts with 'Java'?

    <p>true</p> Signup and view all the answers

    When converting a float to an int, what type of conversion is required?

    <p>Explicit conversion</p> Signup and view all the answers

    Which method is NOT used for String to Character conversion?

    <p>String.charValue()</p> Signup and view all the answers

    What does the endsWith(String suffix) method return when checking if 'Hello, World!' ends with 'World!'?

    <p>true</p> Signup and view all the answers

    Which output is generated by the following code: String str = 'hello'; String newStr = str.replace('l', 'p'); System.out.println(newStr);

    <p>heppo</p> Signup and view all the answers

    What is returned by the isEmpty() method when applied to a newly instantiated string object?

    <p>true</p> Signup and view all the answers

    What will be the output of comparing the strings 'apple' and 'banana' using the compareTo method?

    <p>Negative value</p> Signup and view all the answers

    How do you convert a string representing a number to an integer?

    <p>Integer.parseInt()</p> Signup and view all the answers

    Which method would you use to join multiple strings with a delimiter?

    <p>String.join()</p> Signup and view all the answers

    What does the toCharArray() method do to a string?

    <p>Converts it to a character array</p> Signup and view all the answers

    When using compareToIgnoreCase on 'apple' and 'Banana', what will the result be?

    <p>Negative value</p> Signup and view all the answers

    What type of casting is performed when converting a string to an integer using Integer.valueOf()?

    <p>Explicit casting</p> Signup and view all the answers

    Which method can be used to convert an int to a string?

    <p>String.valueOf()</p> Signup and view all the answers

    What is the correct way to convert a string '123.45' to a double?

    <p>Double.parseDouble('123.45')</p> Signup and view all the answers

    Study Notes

    Java String Methods

    • length(): Returns the length of the string.

      • Example: "Hello, World!".length() returns 13
    • charAt(int index): Returns the character at the specified index.

      • Example: "Hello".charAt(1) returns 'e'
    • substring(int beginIndex): Returns a substring starting from the specified index.

      • Example: "Hello, World!".substring(7) returns "World!"
    • substring(int beginIndex, int endIndex): Returns a substring from beginIndex to endIndex - 1.

      • Example: "Hello, World!".substring(0, 5) returns "Hello"
    • contains(CharSequence sequence): Checks if the string contains the specified sequence of characters.

      • Example: "Hello, World!".contains("World") returns true
    • equals(Object another): Compares the string to the specified object for equality (case sensitive).

      • Example: "Hello".equals("Hello") returns true
    • equalsIgnoreCase(String another): Compares two strings, ignoring case differences.

      • Example: "HELLO".equalsIgnoreCase("hello") returns true
    • indexOf(String str): Returns the index of the first occurrence of the substring.

      • Example: "Hello, World!".indexOf("World") returns 7
    • indexOf(String str, int fromIndex): Returns the index of the first occurrence of the substring starting from the specified index.

      • Example: "Hello, World! World!".indexOf("World", 8) returns 14
    • lastIndexOf(String str): Returns the index of the last occurrence of the substring.

      • Example: "Hello, World! World!".lastIndexOf("World") returns 14
    • toUpperCase(): Converts all characters in the string to uppercase.

      • Example: "hello".toUpperCase() returns "HELLO"
    • toLowerCase(): Converts all characters in the string to lowercase.

      • Example: "HELLO".toLowerCase() returns "hello"
    • trim(): Removes leading and trailing whitespace.

      • Example: " Hello, World! ".trim() returns "Hello, World!"
    • replace(char oldChar, char newChar): Replaces all occurrences of a specified character with a new character.

      • Example: "hello".replace('l', 'p') returns "heppo"
    • replace(CharSequence target, CharSequence replacement): Replaces all occurrences of a substring with another substring.

      • Example: "Hello, World!".replace("World", "Java") returns "Hello, Java!"
    • split(String regex): Splits the string into an array based on the given regular expression.

      • Example: "apple,orange,banana".split(",") returns an array containing "apple", "orange", and "banana"
    • split(String regex, int limit): Splits the string into an array with a maximum number of substrings.

      • Example: "apple,orange,banana".split(",",2) returns an array containing "apple" and "orange,banana".
    • startsWith(String prefix): Checks if the string starts with the specified prefix.

      • Example: "Hello, World!".startsWith("Hello") returns true
    • endsWith(String suffix): Checks if the string ends with the specified suffix.

      • Example: "Hello, World!".endsWith("World!") returns true
    • isEmpty(): Checks if the string is empty (length is 0).

      • Example: "".isEmpty() returns true
    • join(CharSequence delimiter, CharSequence... elements): Joins multiple strings with the specified delimiter.

      • Example: String.join("-", "2024", "10", "20") returns "2024-10-20"
    • compareTo(String anotherString): Compares two strings lexicographically.

      • Example: "apple".compareTo("banana") returns a negative value
    • compareToIgnoreCase(String anotherString): Compares two strings lexicographically, ignoring case differences.

      • Example: "apple".compareToIgnoreCase("Banana") returns a negative value (because apple < Banana)

    Java Type Casting

    • String to Integer (int):

      • Integer.parseInt(str)
      • Integer.valueOf(str)
    • Integer to String:

      • String.valueOf(int)
      • Integer.toString(int)
    • String to double/float/long:

      • Double.parseDouble(str)
      • Float.parseFloat(str)
      • Long.parseLong(str)
    • Other Type Conversions: String to boolean, boolean to string, char to int, int to char, Other conversions are implicit or explicit, depending on the context and data types' sizes.

    • Using Concatenation: You can convert an int to a String by concatenating it with an empty string (e.g., 123 + "").

    Additional Notes

    • Methods like toCharArray, valueOf handle various data types, converting them to Strings.
    • Java has implicit and explicit type conversion rules; implicitly, in some cases, conversion might occur based on data type compatibility. Explicitly, conversion might be required through casting to change data type.

    Studying That Suits You

    Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

    Quiz Team

    Related Documents

    Java String Methods PDF

    Description

    Test your knowledge on Java String methods with this quiz. You'll explore various methods such as length, charAt, and substring. Perfect for beginners and those brushing up on their Java programming skills.

    More Like This

    Character and String Class Methods
    34 questions
    Java String Methods Quiz
    48 questions

    Java String Methods Quiz

    CleanestClavichord avatar
    CleanestClavichord
    Java String Fundamentals Quiz
    45 questions
    Use Quizgecko on...
    Browser
    Browser