Concatenation MCQ
Which operator is used for string concatenation in Java?
a) +
b) -
c) *
d) /
What is the result of the following operation: "Hello" + 5 + 3?
a) "Hello53"
b) "Hello8"
c) "Hello 8"
d) Compilation error
Which method is specifically designed for concatenating multiple strings efficiently in Java?
a) concat()
b) append()
c) concatenate()
d) join()
Which of the following statements is true regarding string concatenation in Java?
a) It's only possible with the concat() method.
b) It's not possible to concatenate strings in Java.
c) String concatenation is done using the append() method.
d) Strings can be concatenated using the + operator.
What happens if you try to concatenate a string with null in Java using the + operator?
a) It throws a NullPointerException.
b) It treats null as an empty string and concatenates successfully.
c) It produces a compilation error.
d) It results in a runtime exception.
Which of the following expressions correctly concatenates two strings?
a) str1.concat(str2)
b) str1 + str2
c) str1.append(str2)
d) str1.join(str2)
When using the StringBuilder class for string concatenation, which method is used to obtain the final concatenated string?
a) toString()
b) getString()
c) finalize()
d) getValue()
Which statement accurately describes the performance difference between using the + operator for string concatenation versus StringBuilder in Java?
a) The + operator is faster for concatenating multiple strings.
b) StringBuilder is always faster than the + operator.
c) StringBuilder is more memory-efficient than the + operator for frequent concatenation.
d) There is no performance difference between the two methods.
Can integers or other non-string types be directly concatenated with strings in Java using the + operator?
a) Yes, it automatically converts non-string types to strings.
b) No, it results in a compilation error.
c) Yes, but only with explicit casting.
d) Yes, but only with the toString() method.
What does the following code snippet do: String result = String.join("-", "One", "Two", "Three")?
a) Concatenates the strings without any separator.
b) Throws a compilation error.
c) Joins the strings with a hyphen (-) separator.
d) Performs a multiplication operation on the strings.
Answers:
a) +
a) "Hello53"
b) append()
d) Strings can be concatenated using the + operator.
b) It treats null as an empty string and concatenates successfully.
b) str1 + str2
a) toString()
c) StringBuilder is more memory-efficient than the + operator for frequent concatenation.
a) Yes, it automatically converts non-string types to strings.
c) Joins the strings with a hyphen (-) separator.
No comments:
Post a Comment