1. Multiple-Choice: indexOf(...) Outcomes.
Consider the method signature: public int indexOf(String target) Which statement best describes the method’s behavior for our MyString class?
- It returns the first index where target is found, or -1 if no match. Empty or null target returns 0, and any target longer than the string returns -1.
- Correct! We treat null as an empty string, return 0 when the target is empty, and -1 if the match is impossible.
- It throws an exception if target is null or if target is longer than the string.
- No. Our simplified approach returns -1 for impossible matches and treats null as "" instead of throwing.
- It searches only the first character of target and ignores the rest, returning 0 when found or 1 when not found.
- No. We check all characters in target, not just the first one.
- It ignores overlapping patterns, so indexOf("aaa") in "aaaa" returns -1.
- No. Overlapping patterns are allowed. indexOf("aaa") in "aaaa" should find a match at index 0.
