QuestionQ231

Working with Selected Classes from the Java API

Given the code fragment:

Question Image

What is the result?

  • A true true
  • B true false
  • C false false
  • D false true
Explanation

Java Strings are immutable, so calling trim() without reassigning its result has no effect on the original reference, which still holds a single space afterward. Comparing that string to "" with equals() checks character content, and a space is not the same as no characters, so it evaluates to false; isEmpty() checks only whether length() equals 0, and a string containing one space character also returns false for that check.

Community Discussion

No comments yet. Be the first to start the discussion!