NEW
Font size
WorksheetsUnit 2 Java String Concepts
Total questions: 15
Worksheet time: 8mins
What is the purpose of the String class in Java?
The purpose of the String class in Java is to represent and manipulate sequences of characters.
To handle file input and output in Java.
To create user interfaces in Java.
To store numerical values in Java.
How do you create a String object in Java?
String str = "Hello, World!";
String str = 'Hello, World!';
String str = new String();
String str = new String('Hello, World!');
What method would you use to find the length of a String?
Count the characters manually.
Apply the 'getLength' function.
Use the 'size' method.
Use the 'length' property or method.
Explain the concept of String immutability in Java.
In Java, strings are mutable and can be altered at any time.
String immutability in Java means that once a string is created, it cannot be altered, and any changes result in a new string object.
Strings in Java can be modified directly after creation.
String immutability allows changing the original string without creating a new one.
Which method would you use to convert a String to uppercase?
Use the 'string.uppercase()' function
Use the 'capitalize()' method
Use the 'toLowerCase()' method
Use the 'toUpperCase()' method (Java) or 'upper()' method (Python) to convert a String to uppercase.
How can you compare two Strings for equality in Java?
Use str1.compareTo(str2) for equality check.
Use str1 == str2 for content equality.
Use str1.equals(str2) for content equality.
Use str1.equalsIgnoreCase(str2) for exact match.
What does the method substring(int beginIndex) do?
It returns the entire string.
It returns a substring from the start to the specified index.
It removes the specified index from the string.
It returns a substring from the specified index to the end of the string.
How do you concatenate two Strings in Java?
You can concatenate two strings using the '&' operator.
Strings can only be concatenated using the StringBuilder class.
You can concatenate two strings in Java using the '+' operator or the String.concat() method.
You must use the String.join() method to concatenate strings.
What is the result of calling the method indexOf(char ch) on a String?
The total number of characters in the string.
The index of the first occurrence of the character, or -1 if not found.
A boolean indicating if the character exists in the string.
The last occurrence of the character in the string.
Can you modify a String object after it has been created? Why or why not?
Yes, you can modify a String object directly.
No, you cannot modify a String object after it has been created.
String objects can be changed using methods.
Strings are mutable and can be altered after creation.
What method would you use to replace a character in a String?
String.replaceChar(oldChar, newChar)
String.replace(oldChar, newChar)
String.change(oldChar, newChar)
String.substitute(oldChar, newChar)
How do you check if a String contains a specific sequence of characters?
Use the 'indexOf' method to find the position of a character.
Convert the string to an array and check the length.
Use regular expressions to match the entire string.
Use the 'contains' method or 'in' operator to check for a substring in a string.
What is the difference between String, StringBuilder, and StringBuffer?
String is mutable; StringBuilder is immutable; StringBuffer is not synchronized.
String is synchronized; StringBuilder is immutable; StringBuffer is mutable.
String is mutable; StringBuilder is synchronized; StringBuffer is immutable.
String is immutable; StringBuilder is mutable and not synchronized; StringBuffer is mutable and synchronized.
How can you split a String into an array of substrings?
Use the join() method to split the string.
Use the concat() method to join strings.
Replace spaces with underscores to create an array.
Use the split() method with a specified delimiter.
What does the trim() method do in the context of Strings?
The trim() method adds whitespace to both ends of a string.
The trim() method removes whitespace from both ends of a string.
The trim() method splits a string into an array of characters.
The trim() method converts a string to uppercase.
