Ask Question
26 September, 12:16

Consider the following method substringFound, which is intended to return true if a substring, key, is located at a specific index of the string phrase. Otherwise, it should return false.

public boolean substringFound (String phrase, String key, int index)

{

String part = phrase. substring (index, index + key. length ());

return part. equals (key);

}

Which of the following is the best precondition for index so that the method will return the appropriate result in all cases and a runtime error can be avoided?

A. 0 < = index < phrase. length ()

B. 0 < = index < key. length ()

C. 0 < = index < phrase. length () + key. length ()

D. 0 < = index < phrase. length () - key. length ()

E. 0 < = index < phrase. length () - index

+1
Answers (1)
  1. 26 September, 14:14
    0
    Option D 0 < = index < phrase. length () - key. length ()

    Explanation:

    The index has to be between the range of 0 < = index < phrase length - key length to prevent index out of bound error. This is because the substring method will have to extract part of the string with a certain length from the original string starting from the index-position. If the key length is longer than the string area between phrase[index] and phase. length (), an index out of bound runtime error will be thrown.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “Consider the following method substringFound, which is intended to return true if a substring, key, is located at a specific index of the ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers