Arizona State University (ASU) CSE110 Principles of Programming Exam 1 Practice

Question: 1 / 400

How can you retrieve the fifth character from a string str?

char c = str.charAt(5);

char c = str.charAt(4);

To retrieve the fifth character from a string in programming languages like Java, you should use the method that accesses characters based on their index. Indexing typically starts from 0, which means that the first character is at index 0, the second character is at index 1, and so forth. As a result, the character at the fifth position is located at index 4.

The option that correctly uses this understanding is the one where `str.charAt(4)` is called. This approach directly fetches the character at the fifth position of the string by specifying the correct index.

In contrast, using `str.charAt(5)` is incorrect for retrieving the fifth character because it would access the sixth character instead. The syntax `str[5]` does not work in languages like Java since accessing characters with square brackets is not valid; it's a common method in languages like JavaScript or Python but does not apply here. Lastly, `str[4]` would provide the fifth character in languages that support such syntax, but in Java, it would lead to an error as Java does not allow array-like indexing for strings.

Get further explanation with Examzify DeepDiveBeta

char c = str[5];

char c = str[4];

Next Question

Report this question

Subscribe

Get the latest from Examzify

You can unsubscribe at any time. Read our privacy policy