跳转到内容

第四五章测验

一、单选题(每题2分)

1.Object obj=new Object();,执行String str=(String)obj;,将会抛出__________异常。

A. StringIndexOutOfBoundsException B. NegativeArraySizeException C. ArrayIndexOutOfBoundsException D. ClassCastException

2. 调用java.lang.Integer.parseInt("123x");,将会抛出__________异常。

A. UnsupportedOperationException B. ArithmeticException C. NullPointerException D. NumberFormatException

3.String str="",执行char ch=str.charAt(0);语句,将会抛出________________异常。

A. NegativeArraySizeException B. ArrayIndexOutOfBoundsException C. StringIndexOutOfBoundsException D. ClassCastException

4. 已知java.lang.Math类声明以下成员方法:

public static double sqrt(double x)              //返回x的平方根值

以下Math.sqrt(x)调用的参数x错误的是_______________。

A. double x=-1; B. long x=3; C. byte x=3; D. int x=3;

5.int i=20,j=0;,执行i/j将会抛出_________异常。

A. NegativeArraySizeException B. StringIndexOutOfBoundsException C. ClassCastException D. ArithmeticException

6.int[] y=null;,执行y[0]=1;,将会抛出________异常。

A. ClassCastException B. NullPointerException C. NegativeArraySizeException D. ArithmeticException

7. 执行Object[] objs=new Object[-10];,将会抛出_________异常。

A. ArrayIndexOutOfBoundsException B. ClassCastException C. NegativeArraySizeException D. StringIndexOutOfBoundsException

8.MyDate[] objs=new MyDate[1];,执行objs[1]=1;,将会抛出___________异常。

A. StringIndexOutOfBoundsException B. ArrayIndexOutOfBoundsException C. ClassCastException D. NegativeArraySizeException

9. 调用java.lang.Double.parseDouble("123x.45");,将会抛出__________异常。

A. ArithmeticException B. NumberFormatException C. ArrayIndexOutOfBoundsException D. NullPointerException


二、多选题(每题3分)

10. 以下获得当前日期时间,正确的有____________。【多选题】

A. new java.util.Calendar() B. System.currentTimeMillis() C. java.util.Calendar.getInstance() D. new java.util.Date()

11. 以下接口声明正确的有_____________________。【多选题】

A. private interface Area B. public interface Area C. final interface Area D. interface Area

12.public interface Area,其中方法声明正确的有______________。【多选题】

A. double area(); B. protected abstract double area(); C. public abstract double area(); D. private abstract double area();

13. 已知Area接口和ClosedFigure、Globe等类声明如下:

public interface Area
public abstract class ClosedFigure
public class Ellipse extends ClosedFigure
public class Globe implements Area

下列声明中,正确的有_______________。【多选题】

A. Area ar = new ClosedFigure(); B. Area ar = new Globe(…); C. Area ar = new Ellipse(…); D. Area ar = new Area();

14. 已知声明接口Area、Volume,以下声明正确的有______________。【多选题】

A. public final class Globe extends Area,Volume B. public abstract class ClosedFigure implements Area C. public interface Solid extends Area, Volume D. public class Cylinder extends Object implements Area,Volume

15. Pixel类的内部类/接口声明如下,正确的有_______________。【多选题】

A. public static class Color implements ColorConstant B. public static interface ColorConstant C. private class Color implements ColorConstant D. protected abstract class Color

16. 已知

public static <T extends Comparable<? super T>> T min(T[] objs)

调用min(T[])方法,objs的实际参数可以是以下_______________类的对象数组。【多选题】

A. public abstract class Figure B. public class MyDate implements Comparable&lt;MyDate&gt; C. public class Rectangle extends ClosedFigure D. public class Person implements Comparable&lt;Person&gt;

17. 【第6版实验题4-5】比较器的参数匹配。

已知

public static <T> T min(T[] objs, Comparator<? super T> comp)

调用min(T[], comp)方法,comp的实际参数可以是以下_______________类的实例。【多选题】

A. public class NameComparator implements Comparator&lt;Person&gt; B. public class VolumeComparator implements Comparator&lt;Volume&gt; C. public class BirthdateComparator implements Comparator&lt;Person&gt; D. public class AreaComparator implements Comparator&lt;Area&gt;


三、填空题(每题2分)

18. 【第6版实验4-1】已知Area接口声明area()方法,程序填空。

public static double average(Area[] areas)    //返回Area接口对象数组按面积计算的平均值
{
    double sum=0;
    for(int i=0; i<areas.length; i++)
        sum+=_______________________;
    return areas.length>0 ? sum/areas.length: 0;
}

【约定答案格式】表达式中没有空格。

19. 已知java.lang.Math类声明以下常量,该常量的引用形式是___________。

public static final double PI = 3.14159265358979323846;

20. 已知java.lang.Math类声明以下方法;返回0~99之间int随机数的方法调用是______________。

public static double random()            //返回一个0.0~1.0之间的随机数

21.java.lang.Integer.parseInt(String s)方法的返回值类型是___________。

22.java.lang.SystemcurrentTimeMillis()方法的返回值类型是_______________。

23.new java.util.Date().getClass().getSuperClass().getName()的执行结果是(包含包名)__________。

24.new java.util.Date().getClass().getSuperClass().getPackage().getName()的执行结果是(包含包名)____________。

25. 以下声明,源程序文件名是__________________,编译后生成的文件名是___________________。

public interface Solid extends Area,Volume

【约定答案格式】多个答案之间用英文逗号","或中文逗号","其中的一种进行分隔。

26. 【第6版实验4-1】已知Area接口声明area()方法,程序填空。

public static Area max(Area[] areas)  //返回最大值对象
{
    int max=0;         //记载最大值对象的下标
    for(int i=1; i<areas.length; i++)
        if(___________________)  //发现更大者
            max = i;
    return areas[max];
}

【约定答案格式】表达式中没有空格。

27. 【第6版实验题4-2】youngest(Person[] pers)方法程序填空。

已知Person类声明如下:

public class Person implements Comparable<Person>
{
    public int compareTo(Person per)  //按出生日期比较对象大小
}

声明以下方法,程序填空。

//返回pers对象数组元素年龄最小者(出生日期最大)
public static Person youngest(Person[] pers)
{
    int min=0;                 //记载最小值对象的下标
    for(int i=1; i<pers.length; i++)
        //发现年龄更小者,即出生日期更大者,委托模型
        if(_____________________)
            min = i;
    return pers[min];
}

【约定答案格式】表达式中没有空格。

28. 【第6版实验题4-3】声明比较器类如下,按出生日期比较Person对象大小。

import java.util.Comparator;
public class BirthdateComparator implements Comparator<Person>
{
    public int compare(Person per1, Person per2)
    {
        if(per1==per2)
            return 0;
        return ___________________________;  //按出生日期比较Person对象大小
    }
}

【约定答案格式】表达式中没有空格。

29. 【第6版实验题4-3】Person类的比较器接口及其应用。

//返回pers年龄最小对象,委托comp比较器接口对象比较Person对象大小
public static Person youngest(Person[] pers, Comparator<Person> comp)
{
    int min=0;
    for(int i=1; i<pers.length; i++)
        if(___________________)  //发现年龄更小者,即出生日期更大者
              min = i;
     return pers[min];
}

【约定答案格式】表达式中没有空格。

30. 【第6版实验题4-3】声明学号比较器类如下,按学号比较Student对象大小。

public class NumberComparator implements Comparator<Student>
{
    public int compare(Student str1, Student str2)
    {
        return ___________________; //按学号比较Student对象大小
    }
}

【约定答案格式】表达式中没有空格。

31. 【第6版实验题4-1】已知Area接口声明area()方法,比较器接口的应用。

//返回Area接口对象数组的面积最小值对象,委托comp比较器比较Area对象大小
public static Area min(Area[] areas, Comparator<Area> comp)
{
    int min=0;
    for(int i=1; i<areas.length; i++)
        if(___________________)  //发现更小者
            min = i;
    return areas[min];
}

【约定答案格式】表达式中没有空格。

32. 已知Integer类声明以下方法:

public static int parseInt(String str) throws NumberFormatException  //将str按十进制转换为整数

调用语句如下。

String str="123a";
try
{   int i = _____________________;
}
catch(NumberFormatException ex)
{   System.out.println("\""+str+"\"不能转换成整数,请重新输入!");
}

33. main()方法声明如下,填空。

public static void main(String args[]) ______________ IOException

34. MyDate类声明以下方法。

public void set(int year, int month, int day) ____________   DateFormatException
{
    if (month<1 || month>12)
       ___________ new DateFormatException(month+"月,月份错误");
}

35. Area等接口和类声明如下,填空。

public interface Area
public interface Perimeter
public abstract class ClosedFigure ____________  //实现上述两接口

【约定答案格式】声明采用一个空格作为单词的分隔符,不要加多余空格。

36. 已知Area接口和ClosedFigure、Globe等类声明如下:

public interface Area
{
    public abstract double area();
}
public abstract class ClosedFigure
public class Ellipse extends ClosedFigure
public class Globe implements Area

调用语句如下,回答问题。

Area ar = new Ellipse(…);
ar.area()               //执行_____________类实现的area()方法
ar = new Globe(…);
ar.area()               //执行_____________类实现的area()方法

【约定答案格式】多个答案之间用英文逗号","或中文逗号","其中一种进行分隔。

37. MyDate类声明如下,实现可比较接口。

public class MyDate _____________________//实现可比较接口

38. 已知MyDate、Person类声明如下,实现可比较接口。

public class MyDate implements Comparable<MyDate>
{
    public int compareTo(MyDate date)  //比较日期对象大小
}
public class Person implements Comparable<Person>
{
    public int compareTo(Person per)  //按出生日期比较对象大小
    {
        if(this==per)
            return 0;
        return _____________________;
    }
}

【约定答案格式】调用中皆为英文半角符号,其中不加空格。

39. Rectangle矩形类比较对象大小,填空。

public class Rectangle implements Comparable<Rectangle>
{
    public double area()                  //计算面积,方法体省略
    public int compareTo(Rectangle rect)   //按面积比较对象大小
    {
        if(this==rect)
            return 0;
        double x=_____________________;
        return x==0 ? 0 :(x>0 ? 1 : -1);
    }
}

【约定答案格式】调用中皆为英文半角符号,其中不加空格。

40. 【第6版实验题4-3】声明比较器类如下,比较Person对象大小。

import java.util.Comparator;
public class BirthdateComparator __________________
{
    public int compare(Person per1, Person per2)
}

41. 【第6版实验题4-5】min(T[])方法算法设计,填空。

//返回objs数组元素最小值
public static <T extends Comparable<? super T>> T min(T[] objs)
{
    int min=0;
    for(int i=1; i<pers.length; i++)
        if(_______________)    //发现更小者
            min = i;
    return objs[min];
}

42. 【第6版实验题4-5】min(T[], comp)方法算法设计,填空。

//返回objs数组元素最小值,委托comp比较器提供比较T类对象大小的compare(T,T)方法
public static <T> T min(T[] objs, Comparator<? super T> comp)
{
    int min=0;
    for(int i=1; i<objs.length; i++)
        if(______________________)    //发现更小者
            min = i;
    return objs[min];
}

43. 已知声明方法如下:

public static int[] getInt(String[] str)
{
    if(str==null || str.length==0)
        return null;
    int x[]=new int[str.length],  n=0, i=0;
    while(i<str.length)
    {   try
        {   x[n]=Integer.parseInt(str[i]);
            n++;
        }
        catch(NumberFormatException ex)
        {   System.out.println("\""+str[i]+"\"字符串不能按十进制转换为整数。");
        }
        catch(Exception ex)
        {   ex.printStackTrace();
        }
        ________________
        {   i++;
        }
    }
    ……
}

44. 声明日期格式异常类如下。

public class DateFormatException _______________ IllegalArgumentException

45. Rectangle矩形类声明实现可比较接口,填空。

public class Rectangle implements Comparable<_____________ >

46. Person类声明如下,实现可比较接口。

public class Person implements Comparable<Person>
{
    public int _____________(Person per)
}