国开《面向对象程序设计》形考任务3(预备知识:第5,6章;分值:25分)

此内容查看价格为3金币,请先
如有问题,请联系微信客服解决!

一、判断题(每题1分,共10分)

1.数组的大小自创建以后就固定了。如果需要在序列中存储不同类型的数据,或者需要动态改变其大小,就需要用集合类型,如Vector类。

2.所有集合类都位于java.util包下。

3.集合框架是为表示和操作集合而规定的一种统一的标准体系结构,包含三大块内容:接口、实现和算法。

4.Set 接口继承 Collection接口,“无序不可重复”,即Set是无序集合,集合中的元素不可以重复。List 接口也继承 Collection接口,“有序可重复”,允许重复,即List是有序集合,集合中的元素可以重复。

5.Map接口是键-值对象,即Map中保存Key-value对形式的元素,访问时只能根据每项元素的key来访问其value。key不能重复,value可以重复。

6.数组的长度不能够被改变,而向量类(Vector)对象的长度可以被改变。

7.向量类Vector中的add(x)方法能够把x元素加入到当前对象的末尾。

8.向量类Vector中的size()方法能够返回向量中当前保存的元素的个数。

9.向量类Vector中的get(i)方法不能够返回向量中下标为i的元素值。

10.向量类Vector中的set(i,x)方法不能够把向量中下标为i的元素值修改为x的值。

二、单项选择题(每题1分,共10分)

11.下列( )接口不是继承于Collection接口。

Set

List

Map

SortedSet

12.下列能表示栈(stack)s1长度的是( )。

s1.length()

s1.length

s1.size

s1.size()

13.有关Set说法错误的是( )。

Set是一个不能包含重复元素的集合

Set继承于Collection接口

Set里的元素排列是有序的,因此可以使用数字下标访问

Set接口是对数学的“集合”进行抽象建模

14.有关List说法错误的是( )

List的元素是无序的

List是一个有序集合,可以包含重复元素

List继承于Collection

可以通过它的索引来访问任何元素,List更像长度动态变换的数组

15.有关Map说法错误的是( )。

Map是一个将key映射到value的对象

一个Map不能包含重复的key

Map继承于Collection

每个key最多只能映射一个value,也就是说value可以相同,但key不能相同

16.下列哪个类不是异常类的父类?( )。

Error

Throwable

Exception

Object

17.下面的异常( )不属于非检查型异常。

数组越界

除零

空值操作

IO异常

18.下面的关键字( )与异常处理无关。

throw

void

throws

try

19.在Java语言中,捕获和处理异常的语句块为( )。

try…catch

switch…case

if…else

do…while

20.下面的异常( )为数组下标越界异常。

ArithmeticException

NullPointerException

ArrayIndexOutOfBoundsException

FileNotFoundException

三、简答题,选择下列每个主函数运行后的输出结果。(每题1分,共5分)

21.Public class Test {

Public static void main(String[] args) {

Vector teamList = new Vector();

teamList.add(\”Z\”);

teamList.add(\”L\”);

teamList.add(\”W\”);

teamList.remove(0);

teamList.remove(0);

System.out.println(teamList.size()+\”,\”+teamList.get(0));

 

}

       

}

2,W

1,W

22.public class XXK4 {

public static void main(String[] args) {

int [][]a={{2,5,8},{3,6,9},{4,5,6}};

int []b=new int[3];

int i,j;

for(i=0; i<a.length; i++)

for(j=0; j<a[i].length; j++)

b[i]+=a[i][j];

for(i=0; i<b.length; i++)

System.out.print(b[i]+\” \”);

}

}

16 18 16

15 18 15

23.class ABC {

int a,b;

public ABC(int a, int b) {this.a=a; this.b=b;}

public int compareTo(ABC x) {return a*b-x.a*x.b;}

}

public class XXK5 {

public static void main(String[] args) {

int [][]d={{3,8},{4,6},{5,6},{2,9},{6,7},{5,8}};

ABC []ar=new ABC[d.length];

int i,k=0;

for(i=0; i<ar.length; i++)

ar[i]=new ABC(d[i][0],d[i][1]);

for(i=1; i<ar.length; i++)

if(ar[i].compareTo(ar[k])>0) k=i;

System.out.println(\”k=\”+k);

}

}

k=4

k=10

24.class ABC {

String name;

double price;

public ABC(String na, double pr) {name=na; price=pr;}

public int compareTo(ABC x) {

if(name.compareTo(x.name)>0) return 1;

if(name.compareTo(x.name)<0) return -1;

else return 0;

}

}

public class XXK5 {

public static void main(String[] args) {

String []s={\”apple\”, \”pear\”, \”tangerme\”, \”banana\”, \”grape\”};

double []d={3.8, 2.5, 3.2, 4.3, 5.2};

ABC []ar=new ABC[s.length];

int i,k=0;

for(i=0; i<ar.length; i++)

ar[i]=new ABC(s[i],d[i]);

for(i=1; i<ar.length; i++)

if(ar[i].compareTo(ar[k])>0) k=i;

System.out.println(ar[k].name+\” \”+ar[k].price);

}

}

tangerme 3.2

tangerme 4.8

25.public class StackTest {

public static void main(String[] args) {

Stack<Integer> st = new Stack<Integer>();

st.push(new Integer(11));

st.push(new Integer(22));

st.push(new Integer(33));

 

System.out.println(\”size is-> \”+st.size());

System.out.println(\”Top is-> \”+st.peek());

st.pop();

System.out.println(\”new Top is-> \”+st.peek());

}

}

size is-> 4 Top is-> 33 new Top is-> 22

size is-> 3 Top is-> 33 new Top is-> 22

社交账号快速登录

微信扫一扫关注
如已关注,请回复“登录”二字获取验证码