79.6k views
5 votes
Examine this code:

String str = "Hello World!" ;
System.out.println( str.substring(6) );
What will it write?

1 Answer

2 votes

Final answer:

The code will write "World!".

Step-by-step explanation:

The code will write "World!". In the given code, the str.substring(6) method call will return a substring starting from index 6 to the end of the string. In the original string "Hello World!", the characters starting from index 6 are "World!" so that is what will be printed to the console. The code in question is using the String object in Java and is designed to write part of the string to the console using the System.out.println method. The substring method is called on the String object str with the argument 6. This means that the method will return a new String starting from index 6 of the original String str. Since the String "Hello World!" starts indexing at 0, index 6 corresponds to the letter 'W' in "World". Hence, the output of this line of code will be "World!" when it is run.

User Jeff Demanche
by
8.0k points