习题八
一、问答题
1."\hello"是正确的字符串常量吗?
不是("\\hello"是)。
2."你好KU".length()和"\n\t\t".length()的值分别是多少?
4和3。
3."Hello".equals("hello")和"java".equals("java")的值分别是多少?
false和true
4."Bird".compareTo("Bird fly")的值是正数还是负数?
负数
5."I love this game".contains("love")的值是true吗?
是true。
6."RedBird".indexOf("Bird")的值是多少?"RedBird".indexOf("Cat")的值是多少?
3和-1
7.执行Integer.parseInt("12.9");会发生异常吗?
会发生NumberFormatException异常。
--视频讲解--
二、选择题
1.下列哪个叙述是正确的?
A. String 类是final 类,不可以有子类。
B. String 类在java.util包中。
C. "abc"=="abc"的值是false .
D. "abc".equals("Abc")的值是true
A。
--视频讲解--
2.下列哪个是正确的(无编译错误)?
A. int m =Float.parseFloat("567");
B. int m =Short.parseShort("567");
C. byte m =Integer.parseInt("2");
D. float m =Float.parseDouble("2.9");
C。
3.对于如下代码,下列哪个叙述是正确的?
A. 程序编译出现错误。
B. 程序标注的【代码】的输出结果是bird。
C. 程序标注的【代码】的输出结果是fly。
D. 程序标注的【代码】的输出结果是null。
B。
--视频讲解--
4.对于如下代码,下列哪个叙述是正确的?
A. 程序出现编译错误。
B.无编译错误,在命令行执行程序:“java E I love this game”,程序输出this。
C.无编译错误,在命令行执行程序:“java E let us go”,程序无运行异常。
D.无编译错误,在命令行执行程序:“java E 0 1 2 3 4 5 6 7 8 9”程序输出3。
D。
--视频讲解--
5.下列哪个叙述是错误的?
A. "9dog".matches("\\ddog")的值是true。
B."12hello567".replaceAll("[123456789]+","@")返回的字符串是@hello@。
C.new Date(1000)对象含有的时间是公元后1000小时的时间
D. "\\hello\n"是正确的字符串常量。
C。
--视频--
三、阅读程序
1.请说出E类中标注的【代码】的输出结果。
【代码】:苹果
--视频讲解--
2.请说出E类中标注的【代码】的输出结果。
【代码】:Love:Game
--视频讲解--
3.请说出E类中标注的【代码1】,【代码2】的输出结果。
【代码1】:15。【代码2】:abc我们
--阅读讲解--
4.请说出E类中标注的【代码】的输出结果。
【代码】:13579
--视频讲解--
5.请说出E类中标注的【代码】,的输出结果。
【代码】:9javaHello
--视频讲解--
import java.util.*;
public class RollDayInMonth {
public static void main(String args[]) {
Calendar calendar=Calendar.getInstance();
calendar.setTime(new Date());
String s = String.format("%tF(%<tA)",calendar);
System.out.println(s);
int n = 25;
System.out.println("向后滚动(在月内)"+n+"天");
calendar.roll(calendar.DAY_OF_MONTH,n);
s = String.format("%tF(%<ta)",calendar);
System.out.println(s);
System.out.println("再向后滚动(在年内)"+n+"天");
calendar.roll(calendar.DAY_OF_YEAR,n);
s = String.format("%tF(%<ta)",calendar);
System.out.println(s);
}
}
7.上机实习下列程序(学习Runtime类),注意观察程序的输出结果。
public class Test{
public static void main(String args[]) {
Runtime runtime = Runtime.getRuntime();
long free = runtime.freeMemory();
System.out.println("Java虚拟机可用空闲内存 "+free+" bytes");
long total = runtime.totalMemory();
System.out.println("Java虚拟机占用总内存 "+total+" bytes");
long n1 = System.currentTimeMillis();
for(int i=1;i<=100;i++){
int j = 2;
for(;j<=i/2;j++){
if(i%j==0) break;
}
if(j>i/2) System.out.print(" "+i);
}
long n2 = System.currentTimeMillis();
System.out.printf("\n循环用时:"+(n2-n1)+"毫秒\n");
free = runtime.freeMemory();
System.out.println("Java虚拟机可用空闲内存 "+free+" bytes");
total=runtime.totalMemory();
System.out.println("Java虚拟机占用总内存 "+total+" bytes");
}
}

