nerdexam
Oracle

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…

Working with Java Primitive Data Types and String APIs

Question

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); System.out.print(b.indexOf("Java")); What is the result?

Options

  • A81111
  • B8109
  • C777
  • D71010
  • E888
  • F7107

How the community answered

(55 responses)
  • A
    2% (1)
  • B
    2% (1)
  • C
    93% (51)
  • D
    4% (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

#String immutability#indexOf#StringBuilder#replace

Community Discussion

No community discussion yet for this question.

Full 1Z0-829 Practice