Learn AP Comp Sci

Problem of the Day

Monday, December 29, 2025


Problem:

The code snippet below produces which output?

String phrase = "Too tricky!";
String excerpt = phrase.substring(2, 7);
System.out.println(excerpt);
  1. oo tri
  2. oo tr
  3. o tric
  4. o tri

Show solution:

The correct answer is d. Positions in the string are counted starting from the first letter which is at position 0, and the substring(a, b) method indicates the start (at index a, inclusive) and end (at index b, exclusive) of the substring. Thus, we're looking for the characters at positions 2 through 6, ie. o tri.