QuestionQ41

Working with Selected Classes from the Java API

Given this code fragment:

Question Image

What is the output?

  • A An exception is thrown at runtime.
  • B -1
  • C 5
  • D 10
Explanation

Java String objects are immutable, so trim() returns a new string rather than changing myStr. Because that returned value is not assigned, the original text remains unchanged, and its first space is at zero-based index 5.

Community Discussion

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