Java OCA OCP Practice Question 729

Question

What is the output of the following?

1:   public class Main { 
2:      public static void main(String[] args) { 
3:         StringBuilder sb = new StringBuilder(); 
4:         sb.append("red"); 
5:         sb.deleteCharAt(0); 
6:         sb.delete(1, 2); 
7:         System.out.println(sb); 
8:      } 
9:   } 
  • A. e
  • B. d
  • C. ed
  • D. None of the above


A.

Note

Line 3 creates an empty StringBuilder.

Line 4 adds three characters to it.

Line 5 removes the first character, resulting in ed.

Line 6 deletes the characters starting at position 1 and ending right before position 2, which removes the character at index 1, which is d.

The only character left is e, so Option A is correct.




PreviousNext

Related