What is the output of the Java code String s = “cat”; s.concat(“fish”); System.out.println(s);?
Correct answer D. cat
Explanation
The concat method builds and returns a new String and leaves the receiver unchanged, because strings are immutable. The returned value is discarded here, so s still refers to "cat". To keep the joined text you must assign the result, as in s = s.concat("fish"), which would make s refer to catfish.
Answer checked How we write and check questions
Discussion