Java OCA OCP Practice Question 2697

Question

Given:

public class Main {
   public static void main(String[] args) {
      String s = "1234";
      StringBuilder sb = new StringBuilder(s.substring(2).concat("56").replace("7", "6"));
      System.out.println(sb.append("89").insert(3, "x"));

   }
}

What is the result?

  • A. 34x5689
  • B. 345x689
  • C. 345x789
  • D. 23x45689
  • E. 23x45789
  • F. Compilation fails.


B is correct.

Note

The keys to remember are that indexes are 0-based, and that chained methods work from left to right.

Also, the replace() method's first argument is the character to be replaced, so in this case the replace() invocation has no effect on the StringBuilder.




PreviousNext

Related