A-A+

下面程序段的输出结果为 public class MyClass { public stat

2022-08-06 05:17:32 问答库 阅读 176 次

问题详情

下面程序段的输出结果为 public class MyClass { public static void main(String args[]) { String s="Hello! How are you?"; System.out.println(s.LastIndexOf("o",16); } }
A.16
B.o
C.u
D.17请帮忙给出正确答案和分析,谢谢!

参考答案

正确答案:A
解析:本题考查字符串类中常用成员函数的用法。String类的成员函数lastIndexOf()的原型是:publicintlastIndexOf(Stringstr,intfromIndex)。它用于获得字符串str在给定字符串中从fromIndex位置往回搜索第一次出现的地方。需要注意的是,在字符串中,下标是从0开始的。所以对于字符串s,下标为16的字母正好是o,从这里往前寻找字符串“o”第一次出现的位置,正好就是字符串中。它本身所在的位置。故s.lastIndexOf(“o”,16)返回的结果就是16。

考点:结果,程序