Learn AP Comp Sci

Problem of the Day

Tuesday, April 21, 2026


Problem:

Consider the following code which uses a String variable value.

for (int i = 0; i < value.length() - 1; i++)
for (int j = i + 1; j < value.length(); j++)
System.out.println(value.substring(i, i+1) + value.substring(j, j+1));

What does this code do?

  1. Prints the sum of pairs of numbers in value
  2. Prints every possible pair of characters in value, including characters paired with themselves
  3. Prints each character in value paired with a different character in value
  4. Prints the first character twice, the second character twice, and so on for all the characters in value

Show solution:

The correct answer is c. The i-loop runs through characters at positions 0 through n-1 (exclusive), while the j-loop runs through characters i + 1 through n (exclusive). As a result, the String help would produce the output

he
hl
hp
el
ep
lp

... which is each character paired with a different character.