Bird
0
0

Identify the error in this code snippet:

medium📝 Debug Q6 of 15
Java - Strings and String Handling
Identify the error in this code snippet:
StringBuilder sb = new StringBuilder();
sb.append("Java");
sb.deleteCharAt(4);
System.out.println(sb);
AdeleteCharAt index is out of bounds
Bappend method is incorrect
CStringBuilder cannot be empty
DSystem.out.println cannot print StringBuilder
Step-by-Step Solution
Solution:
  1. Step 1: Check string length after append

    After append, sb = "Java" which has length 4 (indices 0-3).
  2. Step 2: deleteCharAt(4) is invalid

    Index 4 is out of bounds, causing StringIndexOutOfBoundsException.
  3. Final Answer:

    deleteCharAt index is out of bounds -> Option A
  4. Quick Check:

    Index bounds error = deleteCharAt index is out of bounds [OK]
Quick Trick: Indexes start at 0; max index = length-1 [OK]
Common Mistakes:
  • Assuming deleteCharAt can delete at length index
  • Thinking append is wrong
  • Believing StringBuilder can't be printed

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes