部分习题解析
(选择一项)
A: mid(2, s)
B: charat(2)
C: s. indexOf(v');
D: indexOf (s, 'w')
本题考查String类的常用方法。Java的String类代表字符串。Java程序中的所有字符串字面值(如"abc")都作为此类的实例实现。String对象 是不可改变的。String类提供有 很多方法,这些方法都是非静态方法,需要通过String对象调用执行。其中: charAt()方法是返回指定索引处的char值; indexOf()方法是返回指定字符在此字符串中第一次出现处的索引。本题选项A的代码mid(2. s)方法不是String的方法,所以选项A代码是错误的。选项B和选项D的代码中没有指定方法的调用者,也不正确。本题应选择C。
2.可用做Java标识符的是( )。
A: try
B: _ isYour
c: 2time
D:my name
if(i == 1) I
System. out. print("* ") ;
i++;
continue ;
lelse if(i == 2)
System. out. print("** ") ;
i++ ;
break;
]else if(i == 3) I
System. out. print("** ) ;
i =1;
c ontinue ;
(选择一项)
k:输出* **
B:输出* ** ***
C:死循环
D:输出*
public static String str=”welcome
public static void main(String[] args) {
Test test = new Test() ;
test. chang() ;
System. out. print(str) ;}
public void chang() {
str = welcome bdqn!}
(选择一项)
A: welcome
B:编译错误
C: welcome bdqn !
D:编译通过,运行时报错
str为静态变量,作为全局变量,修改在离开函数后依然会生效,str的初始值为"welcome",调用change()函数后就变成了"welcome bdqn!",因此答案为C。
public static void main(stringl ares)
int i=10;dol
System. out. print(i);
Iwhile(i>0);
(选择一项 )
A: 109876543210
3: 10987654321
C: 9876543210
D: 987654321
6.在下列Java代码片段中,email表示邮箱地址,若要获取邮箱地址的用户名,下列选择正确的是( )
String : email= sus an@prd. com'
(选择一项)
A: String name=email. substring(0, email. indexOf ("@"));
B: String name=email. substring(1, email. indexOf ("@")) ;
C: String name=email. substring (0, email. indexOf ("@")+1) ;
D: String name=email. substring (email. indexOf(" @")+1, email. indexOf('.')) ;
本题考查String类常用方法操纵字符串,public String substring(int beginindex, int endIndex)第一个int为开始的索引,对应String数字中的开始位置,第二个是截止的索引位置,对应String中的结束位置1、取得的字符串长度为: endindex - beginindex;2、从beginIndex开始取,到endIndex结束,从0开始数,其中不包括endIndex位置的字符。indexOf ( String str )返回指定字符串在此字符串中第次出现处的索引 ,所以选择A
String str1= ”Hello" ;
String str2=new String() ;
str2=" Hello" ;
if (str1. equals(str2)) {
System. out. println("两个字符串值相等") ;
if (str1==str2) {
System. out. println(两个字符串指向同一个地址”) ;
(选择一项)
A:两个字符串值相等
B:两个字符串指向同一个地址
C:两个字符串值相等
两个字符串指向同一个地址
D:编译通过,但运行时抛出异常
equals操作表示的两个变量是否是对同一个对象的引用,即堆中的内容是否相同 所以选c。