Java String MCQ

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?

a) new String();
b) String.new();
c) String{};
d) new Object(String);

Answer:

a) new String();

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?

a) true
b) false
c) Compilation error
d) Runtime error

Answer:

a) true

Explanation:

Both string literals point to the same object in the string pool, so == will return true.

3. What does the charAt method do?

a) Returns the character at a specific index
b) Changes the character at a specific index
c) Removes the character at a specific index
d) None of the above

Answer:

a) Returns the character at a specific index

Explanation:

The charAt(int index) method returns the character at the specified index in the string.

4. What will String str = null; do?

a) Creates a new String with no characters
b) Creates a String pointing to "null"
c) Does not create any String
d) Creates an empty String

Answer:

c) Does not create any String

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?

a) toUpperCase()
b) upperCase()
c) toUppercase()
d) Uppercase()

Answer:

a) toUpperCase()

Explanation:

The toUpperCase() method converts all the characters in a given string to upper case.

6. What does the length() method return?

a) ASCII value of first character
b) Number of characters
c) A new String
d) None of the above

Answer:

b) Number of characters

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?

a) ==
b) compareTo()
c) equals()
d) equalStrings()

Answer:

c) equals()

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")?

a) Java Script
b) JavaScript
c) JavaconcatScript
d) A compilation error

Answer:

b) JavaScript

Explanation:

The concat method appends the specified string to the end of another string.

9. Which method replaces a character in a string?

a) replace()
b) replaceChar()
c) setChar()
d) changeChar()

Answer:

a) replace()

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?

a) trim()
b) strip()
c) clean()
d) chop()

Answer:

a) trim()

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?

a) Yes
b) No
c) Only when synchronized
d) Depends on the method being called

Answer:

a) Yes

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?

a) A pool where all string methods are stored
b) A special area of the heap memory where string literals are stored and reused
c) A memory space where string objects are stored temporarily
d) A section of the Java library dedicated to string operations

Answer:

b) A special area of the heap memory where string literals are stored and reused

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?

a) Whenever the new keyword is used
b) Whenever a string is modified using string methods
c) When a string literal is defined in the code
d) Whenever a string is passed as an argument to a method

Answer:

c) When a string literal is defined in the code

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?

a) By appending another string to it
b) By using the intern() method of the String class
c) By converting it to a string literal
d) By calling the toString() method on it

Answer:

b) By using the intern() method of the String class

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?

a) To increase the complexity of string operations
b) To ensure that each string object has a unique memory address
c) To save memory by avoiding the storage of duplicate string values
d) To improve the speed of string concatenation operations

Answer:

c) To save memory by avoiding the storage of duplicate string values

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.



Comments