新人请求高人讲解
1. Consider the following java code:
public static void modifier(String s, char c){
int i = ;
while(i < s.lenght())
{
if(s.charAt(i) == c)
System.out.print(c);
i++;
}
Given that: s = “excellent” c =’e’
What would be the output of the method modifier when excecuted?
a) ccc
b) eee
c) 3
d) 4
e) Generates run-time error StringIndexOutofBoundsException when i reaches s.length() and s.charAt(i) tries to access an element that doesn’t exist at this position in s.
2. "abc".compareTo("aba") returns ___________.a) 1
b) 2
c) 0
d) -2
e) -1
1. Consider the following java code:
public static void modifier(String s, char c){
int i = ;
while(i < s.lenght())
{
if(s.charAt(i) == c)
System.out.print(c);
i++;
}
Given that: s = “excellent” c =’e’
What would be the output of the method modifier when excecuted?
a) ccc
b) eee
c) 3
d) 4
e) Generates run-time error StringIndexOutofBoundsException when i reaches s.length() and s.charAt(i) tries to access an element that doesn’t exist at this position in s.
2. "abc".compareTo("aba") returns ___________.a) 1
b) 2
c) 0
d) -2
e) -1
2.应该选择b
该题的关键是就是compareTo方法,
举个例子:
s1.compareTo(s2),它就是从开始扫描s1和s2,compareTo 返回这两个字符串在位置 k 处的两个不同的 char 值,即是s1.charAt(k)-s2.charAt(k)
如果它们没有不同的索引位置,即是两个字符串中,其中一个是另一个的一部分,
则返回值为:s1.length()-s2.length()