Java OCA OCP Practice Question 817

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, 1); 
7:         System.out.println(sb); 
8:      } 
9:   } 
  • A. r
  • B. e
  • C. ed
  • D. red
  • E. The code does not compile.
  • F. The code compiles but throws an exception at runtime.


C.

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 1.

Since there are no indexes that meet that description, the line has no effect. Therefore, Option C is correct.




PreviousNext

Related