2.1k views
1 vote
After the execution of the following lines of code, what will be output onto the console?

String letters = "ABCde";
String name = "Karel the Dog";
String letter = "D";
System.out.println(name.indexOf(letter));
System.out.println(letters.indexOf(name.substring(3,4)));
System.out.println(letters.indexOf(letter));

User Summit
by
7.7k points

1 Answer

4 votes

Final answer:

The output onto the console will be: -1, 3, 3.

Step-by-step explanation:

The output onto the console will be:


  • -1

  • 3

  • 3

Step-by-step explanation:

- In the first System.out.println statement name.indexOf(letter), the variable letter is assigned the value 'D'. However, there is no 'D' in the string name, so the output will be -1.

- In the second System.out.println statement letters.indexOf(name.substring(3,4)), the substring name.substring(3,4) is 'e'. The string letters has 'e' at index 3, so the output will be 3.

- In the third System.out.println statement letters.indexOf(letter), the variable letter is 'D'. The string letters has 'D' at index 3, so the output will be 3.

User DanCue
by
7.6k points