Bird
0
0

Identify the error in this Java code snippet:

medium📝 Debug Q14 of 15
Java - Strings and String Handling
Identify the error in this Java code snippet:
String s = "Java Programming";
String part = s.substring(5, 3);
System.out.println(part);
Asubstring() cannot be used on String objects
BMissing semicolon after substring() call
Csubstring() arguments are reversed; start index must be <= end index
DThe variable 'part' is not declared
Step-by-Step Solution
Solution:
  1. Step 1: Check substring() method argument rules

    substring(start, end) requires start index less than or equal to end index; here 5 > 3, which is invalid.
  2. Step 2: Confirm other syntax correctness

    Semicolons are present, substring() is valid on String, and 'part' is declared properly.
  3. Final Answer:

    substring() arguments are reversed; start index must be <= end index -> Option C
  4. Quick Check:

    substring(start, end) needs start ≤ end [OK]
Quick Trick: Start index must not be greater than end index in substring() [OK]
Common Mistakes:
  • Swapping start and end indexes
  • Thinking substring() changes original string
  • Ignoring exception thrown at runtime

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Java Quizzes