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?
- Prints the sum of pairs of numbers in
value - Prints every possible pair of characters in
value, including characters paired with themselves - Prints each character in
valuepaired with a different character invalue - Prints the first character twice, the second character twice, and so on for all the characters in
value
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.