1Z0-829 · Question #4
Given the code fragment: String a = "Hello! Java"; System.out.print(a.indexOf("Java")); a.replace("Hello!", "Welcome!"); System.out.print(a.indexOf("Java")); StringBuilder b = new StringBuilder(a)…
The correct answer is C. 777. The code fragment is creating a string variable "a" with the value "Hello! Java". Then, it is printing the index of "Java" in "a". Next, it is replacing "Hello!" with "Welcome!" in "a". Then, it is printing the index of "Java" in "a". Finally, it is creating a new StringBuilder…
Question
Options
- A81111
- B8109
- C777
- D71010
- E888
- F7107
How the community answered
(55 responses)- A2% (1)
- B2% (1)
- C93% (51)
- D4% (2)
Explanation
The code fragment is creating a string variable "a" with the value "Hello! Java". Then, it is printing the index of "Java" in "a". Next, it is replacing "Hello!" with "Welcome!" in "a". Then, it is printing the index of "Java" in "a". Finally, it is creating a new StringBuilder object "b" with the value of "a" and printing the index of "Java" in "b". The output will be 8109 because the index of "Java" in "a" is 8, the index of "Java" in "a" after replacing "Hello!" with "Welcome!" is 10, and the index of "Java" in "b" is 9. Reference: Article Java SE 17 Developer source and documents: [String (Java SE 17 & JDK 17)], [StringBuilder (Java SE 17 & JDK 17)]
Topics
Community Discussion
No community discussion yet for this question.