第八周测试总结分析

1、下列关于String类的说法,正确的是
A .String类为final类,不能拥有子类。
B .String常量对象是用双引号括起的字符序列。
C .在String s = new String(“Hello!”);中,对象变量s中存放的是实体。
D .System.out.println(s);可以输出String对象的引用。
答案:AB
解析:C项:对象变量存放的是引用。D项:输出的是对象的实体。

2、下列属于引用数据类型的是
A .String
B .char
C .用户自定义的Student类类型
D .int
答案:AC

3、实体相同但引用不同的两个字符串,使用“==”比较结果为false。
A .true
B .false
答案:A

4、String类中的length()方法用来获取一个String对象的字符序列的长度,单位为字节。
A .true
B .false
答案:B
解析:长度等于字符串中 Unicode 代码单元的数量。

5、下列通过测试的断言语句有几个?

①assertEquals(true, "123".matches("\d+"));

②assertEquals(true, "3.5".matches("\d\.\d"));

③assertEquals(true, "C:\Windows".matches("C:\Windows"));

④assertEquals(true, "hello hello".matches("\b(?\w+)\b\s+\k\b"));

⑤assertEquals("rplcmnt","replacement".replaceAll("[aeiou]","*"));

⑥assertEquals(true, ""Hello"".matches("(["'])[^"']*\1"));

⑦assertEquals("##","xfooxxxxxxfoo".replaceAll(".*?foo","#"));

⑧assertEquals("Let's meet at [TIME].","Let's meet at 12:38.".replaceAll("((1|0?)[0-9]|2[0-3])😦[0-5][0-9])","[TIME]"));
A .7个
B .6个
C .4个
D .3个
答案:B
解析:③:要查找\本身,需要用\。⑥:注意字符转义,应为""Hello"".matches("(["'])[^"']*\1")。

6、下列关于正则表达式的说法,正确的是
A .\ba\w*\b匹配以字母a开头的单词
B .\d+匹配1个或更多连续的数字。
C .\b\w{6}\b 匹配6个及以上字符的单词。
D .[0-9]代表的含意与\d就是完全一致的:一位数字
E .\S+匹配不包含空白符的字符串。
F .(\d{1,3}.){3}\d{1,3}用来匹配 IP地址。
答案:ABDE
解析:C项:\b\w{6}\b 匹配刚好6个字符的单词。F项: IP地址中每个数字都不能大于255,该表达式忽略了这个约束条件。正确的是((2[0-4]\d|25[0-5]|[01]?\d\d?).){3}(2[0-4]\d|25[0-5]|[01]?\d\d?)。

7、下列关于substring方法的说法,正确的是
A .public String substring(int beginIndex)返回的子字符串从指定索引处的字符开始,直到此字符串末尾。
B .public String substring(int beginIndex, int endIndex) 子字符串从指定的 beginIndex 处开始,直到索引 endIndex处的字符。
C ."emptiness".substring(9)返回值为""。
D ."smiles".substring(1, 5)返回值为"mile"。
E .若beginIndex 大于 endIndex,则substring(int beginIndex, int endIndex)返回-1。
答案:ACD
解析:查询API。B项:直到索引 endIndex - 1 处的字符。E项:抛出IndexOutOfBoundsException异常。

8、以下两段程序的输出结果是完全一样的。

//【代码一】

StringTokenizer st = new StringTokenizer("this is a test");

     while (st.hasMoreTokens()) {

         System.out.println(st.nextToken());

     }

//【代码二】

String[] result = "this is a test".split("\\s");

     for (int x=0; x<result.length; x++)

         System.out.println(result[x]);

A .true
B .false
答案:A

9、What is the output of the following code?(下面代码的运行结果是?)

LocalDate date = LocalDate.of(2018, Month.APRIL, 30);

date.plusDays(2);

date.plusYears(3);

System.out.println(date.getYear() + " "

+ date.getMonth() + " "+ date.getDayOfMonth());

A .2018 APRIL 2
B .2018 APRIL 30
C .2018 MAY 2
D .2021 APRIL 2
E .2021 APRIL 30
F .2021 MAY 2
G .A runtime exception is thrown.
答案:B
解析:The date starts out as April 30, 2018. Since dates are immutable and the plus methods have their return values ignored, the result is unchanged. Therefore, Option B is correct.

10、What is the output of the following code?(下面代码的运行结果是?)

LocalDate date = LocalDate.of(2018, Month.APRIL, 40);

System.out.println(date.getYear() + " " + date.getMonth()

+ " "+ date.getDayOfMonth());

A .2018 APRIL 4
B .2018 APRIL 30
C .2018 MAY 10
D .Another date
E .The code does not compile.
F .A runtime exception is thrown.
答案:F
解析:Java throws an exception if invalid date values are passed. There is no 40th day in April—or any other month for that matter.

11、调用 new Random(seed) 等效于:Random rnd = new Random(); rnd.setSeed(seed);
A .true
B .false
答案:A
解析:查询API。

12、对于如下代码,下列哪个叙述是正确的?

public class E{ 

   public static void main(String[] args){ 

      String strOne="bird"; 

      String strTwo=strOne; 

      strOne="fly"; 

      System.out.println(strTwo); 

  } 

}

A .程序编译出现错误。
B .程序标注的【代码】的输出结果是bird。
C .程序标注的【代码】的输出结果是fly。
D .程序标注的【代码】的输出结果是null。
答案:B

13、对于如下代码,下列哪个叙述是正确的?

public class E {

   public static void main (String args[]) {

     String s1 = args[1];

     String s2 = args[2];

     String s3 = args[3];

     System.out.println(s3); 

   }

}

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

14、下列哪个叙述是错误的?
A ."9dog".matches("\ddog")的值是true。
B ."12hello567".replaceAll("[123456789]+","@")返回的字符串是@hello@。
C .new Date(1000)对象含有的时间是公元后1000小时的时间
D ."\hello\n"是正确的字符串常量。
答案:C
(说明:P是书上页码,详情请看书)

posted @ 2019-05-07 09:14  20175218陈敬勇  阅读(476)  评论(0编辑  收藏  举报