📢 Too many exams? Don’t know which one suits you best? Book Your Free Expert 👉 call Now!

  • google app store apple app store
  • ✖

      Question

      A Java method attempts to extract a substring.

      public class StringProcessor {     public String extractPart(String text, int start, int length) {         // Assume text is "HelloWorld"         // Assume start = 5, length = 6         return text.substring(start, start + length); // Potential bug here     } } If text = "HelloWorld", start = 5, and length = 6, what will be the outcome of calling extractPart(text, start, length)?
      A Returns "World". Correct Answer Incorrect Answer
      B Returns "WorldD". Correct Answer Incorrect Answer
      C Returns "oWorld". Correct Answer Incorrect Answer
      D Throws a StringIndexOutOfBoundsException. Correct Answer Incorrect Answer
      E Returns null. Correct Answer Incorrect Answer

      Solution

      Correct Answer: D (The string "HelloWorld" has length 10. start = 5, start + length = 5 + 6 = 11. substring(5, 11) attempts to access index 10, which is out of bounds as valid indices are 0-9.)

      Practice Next
      More IT Operating System Questions
      ask-question