一、填空题
1.假设有
String s1 = "Welcome to Java";
String s2 = s1;
String s3 = new String("Welcome to Java");
那么下面表达式的结果是什么?
(1) s1 == s2 ___________true_______________
(2) s1 == s3 ___________false______________
(3) s1.equals(s2) ___________true ______________
(4) s2.equals(s3) ___________true ______________
(5) s1.compareTo(s2); ____________0_________________
(6) s2.compareTo(s3); ____________0_________________
(7) s1.charAt(0); ____________’W’_______________
(8) s1.indexOf('j'); ____________-1________________
(9) s1.indexOf("to"); ____________8_________________
(10) s1.lastIndexOf("o",15) ___________9_________________
(11) s1.substring(3, 11); ________” come to ” ___________
(12) s1.endsWith("Java") ______________true________________
(13) s1.startsWith("wel"); _________false_________________
(14) " We come ".trim(); ________”We come”______________
(15) s1.toUpperCase(); ________”WELCOME TO JAVA”______
(16) s1.replace('o', 'T'); ________”WelcTme tT Java”______
2. 如果有
StringBuffer s1 = new StringBuffer("Java");
StringBuffer s2 = new StringBuffer("HTML");
假设下列每个语句是独立的,每条语句结束后,写出相应结果:
(1) s1.append(" is fun"); s1为_____ Java is fun ___
(2) s1.append(s2); s1为_____ JavaHTML ______
(3) s1.insert(2, "is fun"); s1为______Jais funva_____
(4) s1.insert(1,s2); s1为______JHTMLava_______
(5) char c = s1.charAt(2); c为_______v______________
(6) int i = s1.length(); i为_______4_____________
(7) s1.deleteCharAt(3); s1为______Jav____________
(8) s1.delete(1,3); s1为______Ja______________
(9) s1.reverse(); s1为______avaJ____________
(10) s1.replace(1,3, "Computer"); s1为_____JComputera_______
(11) String s3 = s1.substring(1,3);
s3为____av_________________,s1为__________Java___________
(12) String s4 = s1.substring(2);
S4为____va______________,s1为__________Java_________________
3. 假设有
StringBuffer s = new StringBuffer("Welcome to JAVA");
将s的内容清空的语句是_______ s.delete(0, s.length());________。
4. 如果有
String s1 = "Welcome";
String s2 = new String("Welcome");
String s3 = s2.intern();
String s4 = "Wel" + "come";
String s5 = "Wel";
String s6 = "come";
String s7 = s5 + s6;
String s8 = "Wel" + new String("come");
那么下面表达式的结果为:
(1)s1 == s2 _____false_____
(2)s1 == s3 _____true______
(3)s1 == s4 _____true______
(4)s1 == s7 _____false_____
(5)s1 == s8 _____false_____
(6)s1.equals(s2) ____true________
(7)s1.equals(s3) ___true_________
(8)s1.equals(s4) ___true_________
(9)s1.equals(s7) ___true_________
(10)s1.equals(s8) ___true_________
二、单项选择题
1. 可以获取字符串s的最后一个字符的表达式是___C_____。
(A)s.length()
(B)s[s.length() - 1]
(C)s.charAt(s.length() - 1)
(D)charAt(s, length(s))
2. 下面程序
class C {
public static void main(String[] args) {
String s = “null”;
if(s == null)
System.out.print(“a”);
else if(s.length() == 0)
System.out.print(“b”);
else
System.out.print(“c”);
}
}
的输出为___C_____。
(A)a (B)b
(C)c (D)null
3. 下面程序
class C {
public static void main(String[] args) {
String s = “Welcome to ”;
concat(s);
System.out.print(s);
}
public static void concat(String s) {
s += “Java”;
}
}
的输出为___A___。
(A)Welcome to (B)Welcome to Java
(C)编译错误 (D)运行时异常
三、编程题
1. 编写程序,从控制台或对话框任意输入一个英文字符串,统计字符串中每个英文字母出现的次数并输出到控制台(大小写不敏感)。
import java.util.Scanner;
public class Main{
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String s = scanner.next();
s = s.toLowerCase();
int [] cnt = new int [26];
for(int i = 0; i < s.length(); i++){
if(Character.isLetter(s.charAt(i))){
cnt[s.charAt(i) - 'a']++;
}
}
for(char i = 'a'; i <= 'z'; i++){
System.out.print(i + ":"+cnt[i - 'a'] + " ");
}
}
}
运行结果:
2.假设一个车牌号码由三个大写字母和后面的四个数字组成。编写一个程序. 生
成5个不重复的车牌号码。
public class Main{
public static void main(String[] args) {
String [] licensePlate = new String [5];
int cnt = 0;
while(cnt < 5){
StringBuilder s = new StringBuilder();
for (int i = 0; i < 3; i++) {
char letter = (char) (26 * Math.random() + 'A');
s.append(letter);
}
for(int i = 0; i < 4; i++){
char num = (char)(10 * Math.random() + '0');
s.append(num);
}
boolean flag = false;
for(int i = 0; i < cnt; i++){
if(licensePlate[i].contentEquals(s)){
flag = true;
break;
}
}
if(!flag){
licensePlate[cnt] = String.valueOf(s);
cnt++;
}
}
for(int i = 0; i < 5; i++){
System.out.println(licensePlate[i]);
}
}
}
运行结果: