Practice mode Java

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.

Report a problem

Answer checked How we write and check questions

Tip: press A, B, C or D to answer, and N for the next question.

Advertisement

Have a question about this answer?

Your email address will not be published. Required fields are marked *

Links and website addresses are not allowed in comments.