Given that s is a String, what does the following loop do? for(int j = s.length( ); j > 0; j--) System.out.print(s.charAt(j-1));


Given that s is a String, what does the following loop do?


for(int j = s.length( ); j > 0; j--)

System.out.print(s.charAt(j-1));

a) it prints s out backwards

b) it prints s out forwards

c) it prints s out backwards after skipping the last character

d) it prints s out backwards but does not print the 0th character

e) it yields a run-time error because there is no character at s.charAt(j-1) for j = 0

Answer: a. Explanation: The variable j counts down from the length of the String to 1, each time printing out the character at position j-1. The character at length – 1 is the first character printed and this is the last character of the String. It continues until it reaches j = 1, and prints out the character at position j – 1, or the 0th character, so it prints the entire String backwards.