Strings are one of the most commonly used classes in Java programming, serving as the go-to data type for textual data. This blog post aims to test your fundamental understanding of the String class in Java with 10+ multiple-choice questions (MCQs). Each question is followed by the correct answer and an explanation to help reinforce your knowledge.
1. How to create a new String object in Java?
Answer:
Explanation:
The new String(); syntax is used to create a new String object in Java.
2. What is the output of “Java” == “Java” in Java?
Answer:
Explanation:
Both string literals point to the same object in the string pool, so == will return true.
3. What does the charAt() method do?
Answer:
Explanation:
The charAt(int index) method returns the character at the specified index in the string.
4. What is the result of statement: String str = null;?
Answer:
Explanation:
Declaring a String variable as null means it does not point to any memory location for an object.
5. How to convert a string to upper case in Java?
Answer:
Explanation:
The toUpperCase() method converts all the characters in a given string to upper case.
6. What does the length() method return?
Answer:
Explanation:
The length() method returns the number of characters in a string.
7. Which method is used to check the equality of the content of two strings in Java?
Answer:
Explanation:
The equals() method is used to check if two strings have the same content. The == operator checks for reference equality, compareTo() is used for lexicographical comparison, and there is no built-in method named equalStrings() in Java.
8. What is the result of “Java”.concat(“Script”)?
Answer:
Explanation:
The concat method appends the specified string to the end of another string.
9. Which method replaces a character in a string?
Answer:
Explanation:
The replace() method replaces a character or a sequence of characters in a string.
10. Which method removes whitespace from the beginning and end of a string?
Answer:
Explanation:
The trim() method removes whitespace from both the beginning and the end of the string.
11. Is the String class in Java thread-safe?
Answer:
Explanation:
Strings in Java are immutable, which means once a String object is created, its value cannot be modified. This inherent immutability makes them thread-safe, as multiple threads cannot change their value simultaneously.
12. What is the String Constant Pool in Java?
Answer:
Explanation:
The String Constant Pool is a specific area in the heap memory where Java stores string literals. The primary purpose of this is to save memory by reusing existing strings.
13. When does a string get added to the String Constant Pool?
Answer:
Explanation:
String literals automatically get a spot in the String Constant Pool. Using the new keyword, on the other hand, ensures that the string object is created in the heap memory outside of this pool.
14. How can you ensure that a string created using the new keyword gets placed in the String Constant Pool?
Answer:
Explanation:
The intern() method ensures that the string is placed in the String Constant Pool. If a string with the same content already exists there, it returns the reference to that string; otherwise, it places the string in the pool and returns its reference.
15. Why does Java utilize a String Constant Pool?
Answer:
Explanation:
The primary reason for the existence of the String Constant Pool is memory optimization. By reusing references for string literals with the same value, Java ensures that memory is used efficiently.