QuestionQ24

Working with Selected Classes from the Java API

Given the following code fragment:

Question Image

What is the outcome?

  • A 5
  • B false6
  • C –15
  • D –1–1
Explanation

StringBuilder.indexOf(String str) returns the index of the first occurrence of str in the buffer's current character sequence, or -1 if str does not occur. Before the append call the buffer holds only "Java", which contains no occurrence of "the", so the call returns -1. After sObj.append(" the Great") executes, the buffer becomes "Java the Great", in which the substring "the" first begins at character index 5, so the second call to indexOf returns 5.

Learn more

Community Discussion

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