gpt4 book ai didi

java - charAt方法,试卷Q

转载 作者:行者123 更新时间:2023-11-29 05:26:15 25 4
gpt4 key购买 nike

Which of these statements concerning the charAt() method of the Stringclass are true?

Select 2 options

A. The charAt( ) method can take a char value as an argument.

B. The charAt( ) method returns a Character object.

C. expression char ch = "12345".charAt(3) will assign 3 to ch.

D. The expression char ch = str.charAt(str.length()) where str is "12345", will assign 3 to ch.

E. The index of the first character is 0.

F. It throws StringIndexOutOfBoundsException if passed an value higher than or equal to the length of the string (or less than 0).

G. It throws ArrayIndexOutOfBoundsException if passed an value higher than or equal to the length of the string (or less than 0).

问题的答案是 A,E 。我的答案是E,F

论文原因如下:ArrayIndexOutOfBoundsExceptionStringIndexOutOfBoundsException,扩展 IndexOutOfBoundsException 和尽管在实践中,charAt 方法会抛出StringIndexOutOfBoundsException,它不是一个有效的选项,因为实现可以自由抛出一些其他异常,只要它是一个IndexOutOfBoundsException

但这并没有真正意义,我很确定 charAt() 会抛出一个 StringIndexOutOfBoundsException。论文答案不正确吗?

最佳答案

您的第二个答案不正确:charAt 抛出 IndexOutOfBoundsException .

选项“A”也是正确的,因为 Java 编译器会隐式地将 char 转换为 int(尽管在这种情况下使用 是有意义的) char 作为索引看起来是人为的)。

关于选项“E”的正确性,您是正确的。

关于java - charAt方法,试卷Q,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22577071/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com