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 (B)</p> Signup and view all the answers

Which statement correctly converts a long to a String?

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

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

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

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

<p>trim() (B)</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. (C)</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) (C)</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 (C)</p> Signup and view all the answers

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

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

Which method is NOT used for String to Character conversion?

<p>String.charValue() (C)</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 (A)</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 (D)</p> Signup and view all the answers

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

<p>true (C)</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 (C)</p> Signup and view all the answers

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

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

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

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

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

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

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

<p>Negative value (A)</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 (C)</p> Signup and view all the answers

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

<p>String.valueOf() (D)</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') (D)</p> Signup and view all the answers

Flashcards

Converting double to String

Using String.valueOf() to convert a double to a String.

String to float conversion

Converting a string to a float using Float.parseFloat().

Implicit int to double

Java automatically converts an int to a double.

Explicit double to int

Converting a double to an int using casting.

Signup and view all the flashcards

Char to int conversion

Converting a character to its ASCII integer representation.

Signup and view all the flashcards

int to char

Converting an integer (ASCII value) to a character.

Signup and view all the flashcards

Char to String conversion

Converting a character into a string using Character.toString() or String.valueOf().

Signup and view all the flashcards

String to char

Extracting a single character from a string using charAt().

Signup and view all the flashcards

toUpperCase()

Converts all characters in a string to uppercase.

Signup and view all the flashcards

toLowerCase()

Converts all characters in a string to lowercase.

Signup and view all the flashcards

trim()

Removes leading and trailing whitespace from a string.

Signup and view all the flashcards

replace(char, char)

Replaces all occurrences of a specific character with another in a string.

Signup and view all the flashcards

replace(CharSequence, CharSequence)

Replaces all occurrences of a substring with another substring in a string.

Signup and view all the flashcards

split(String)

Splits a string into an array of substrings based on a given delimiter.

Signup and view all the flashcards

startsWith(String)

Checks if a string begins with a specified prefix.

Signup and view all the flashcards

endsWith(String)

Checks if a string ends with a specified suffix.

Signup and view all the flashcards

isEmpty() Method

Checks if a String is empty (has zero length).

Signup and view all the flashcards

join() Method

Combines multiple strings into one, using a specified delimiter.

Signup and view all the flashcards

compareTo() Method

Compares two strings lexicographically (alphabetical order).

Signup and view all the flashcards

compareToIgnoreCase() Method

Compares two strings lexicographically, ignoring case sensitivity.

Signup and view all the flashcards

valueOf() Method (String)

Converts various data types (int, long, float, boolean) to a String.

Signup and view all the flashcards

toCharArray() Method

Converts a String into a character array, where each element is a single character.

Signup and view all the flashcards

String to Integer

Converts a String representing a number to an int.

Signup and view all the flashcards

Integer to String

Converts an int to a String.

Signup and view all the flashcards

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
1 questions

Java String Methods Quiz

CelebratedRhodium5657 avatar
CelebratedRhodium5657
Java String Methods Quiz
48 questions

Java String Methods Quiz

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