现有: - list是一个合法的集合引用 - getCollection()返回一个合法集合的引用 哪两个是合法的?()
第1题:
现有:3.importjava.util.*;4.classForInTest{5.staticListlist=newArrayList();6.publicstaticvoidmain(String[]args){7.8.list.add("a");list.add("b");list.add("c");9.//insertcodehere10.System.out.print(o);}}哪一行插入到第9行将导致输出abc”?()
A.for(Objecto:list)
B.for(Iteratoro:list)
C.for(Objecto:list.iterator())
D.for(Iteratoro:list.iterator();o.hasNext();)
第2题:
A.for(Objecto;list)
B.for(Objecto:list.iterator())
C.for(Objecto:getCollection())
D.for(Iteratori=list.iterator();i.hasNext();)
第3题:
试题五(共 15分)
阅读以下说明和C++代码,将应填入 (n) 处的字句写在答题纸的对应栏内。
【说明】
已知类 LinkedList 表示列表类,该类具有四个方法:addElement()、lastElement()、umberOfElement()以及removeLastElement()。四个方法的含义分别为:
void addElement(Object): 在列表尾部添加一个对象;
Object lastElement(): 返回列表尾部对象;
int numberOfElement(): 返回列表中对象个数;
void removeLastElement(): 删除列表尾部的对象。
现需要借助LinkedList来实现一个Stack栈类,C++代码1和C++代码2分别采用继承和组合的方式实现。
【C++代码 1】
class Stack :public LinkedList{
public:
void push(Object o){ addElement(o); }; //压栈
Object peek(){ return (1) ; }; //获取栈顶元素
bool isEmpty(){ //判断栈是否为空
return numberOfElement() == 0;
};
Object pop(){ //弹栈
Object o = lastElement();
(2) ;
return o;
};
};
【C++代码 2】
class Stack {
private:
(3) ;
public:
void push(Object o){ //压栈
list.addElement(o);
};
Object peek(){ //获取栈顶元素
return list. (4) ;
};
bool isEmpty(){ //判断栈是否为空
return list.numberOfElement() == 0;
};
Object pop(){//弹栈
Object o = list.lastElement();
list.removeLastElement();
return o;
};
};
【问题】
若类LinkedList新增加了一个公有的方法removeElement(int index),用于删除列表中第index个元素,则在用继承和组合两种实现栈类Stack的方式中,哪种方式下Stack对象可访问方法removeElement(int index)? (5) (A. 继承 B. 组合)
第4题:
A.add(E e)
B.contains(Object o)
C.clear()
D.iterator()
第5题:
现有:3.importjava.util.*;4.classForInTest{5.staticListlist=newArrayList();6.7.publicstaticvoidmain(String[]args){8.list.add("a");list.add("b");list.add("c");9.//insertcodehere10.System.out.print(o);11.}12.}哪一行插入到第9行将导致输出abc”?()
A.for(Objecto:list)
B.for(Iteratoro:list)
C.for(Objecto:list.iterator())
D.for(Iteratoro:list.iterator();o.hasNext();)
第6题:
A.Filef2=newFile(f);
B.FileReaderfr2=newFileReader(f);
C.FileReaderfr2=newFileReader(fr);
D.BufferedReaderbr2=newBufferedReader(fr);
第7题:
3.importjava.util.*;4.classForInTest{5.staticListlist=newArrayList();6.7.publicstaticvoidmain(String[]args){8.list.add("a");list.add("b");list.add("c");9.//insertcodehere10.System.out.print(o);11.}12.}哪一行插入到第9行将导致输出abc”?()
A.for(Objecto:list)
B.for(Iteratoro:list)
C.for(Objecto:list.iterator())
D.for(Iteratoro:list.iterator();o.hasNext();)
第8题:
试题六(共 15分)
阅读以下说明和Java代码,将应填入 (n) 处的字句写在答题纸的对应栏内。
【说明】
已知类 LinkedList 表示列表类,该类具有四个方法:addElement()、lastElement()、umberOfElement()以及removeLastElement()。四个方法的含义分别为:
void addElement(Object): 在列表尾部添加一个对象;
Object lastElement(): 返回列表尾部对象;
int numberOfElement(): 返回列表中对象个数;
void removeLastElement(): 删除列表尾部的对象。
现需要借助LinkedList来实现一个Stack栈类, Java代码1和Java代码2分别采用继承和组合的方式实现。
【Java代码1】
public class Stack extends LinkedList{
public void push(Object o){ //压栈
addElement(o);
}
public Object peek(){ //获取栈顶元素
return (1) ;
}
public boolean isEmpty(){ //判断栈是否为空
return numberOfElement() == 0;
}
public Object pop(){ //弹栈
Object o = lastElement();
(2) ;
return o;
}
}
【Java代码2】
public class Stack {
private (3) ;
public Stack(){
list = new LinkedList();
}
public void push(Object o){
list.addElement(o);
}
public Object peek(){//获取栈顶元素
return list. (4) ;
}
public boolean isEmpty(){//判断栈是否为空
return list.numberOfElement() == 0;
}
public Object pop(){ //弹栈
Object o = list.lastElement();
list.removeLastElement();
return o;
}
}
【问题】
若类LinkedList新增加了一个公有的方法removeElement(int index),用于删除列表中第index个元素,则在用继承和组合两种实现栈类Stack的方式中,哪种方式下Stack对象可访问方法removeElement(int index)? (5) (A. 继承 B. 组合)
试题六参考答案
(1)lastElement() (3分)
(2)removeLastElement() (3分)
(3)LinkedList list (3分)
(4)lastElement() (3分)
(5)A,或继承 (3分)
第9题:
A.for(Objecto;list)
B.for(Objecto:getCollection()
C.for(Objecto:list.iterator()
D.for(lteratori;list.iterator();i.hasNext())
E.for(lteratori=list.iterator();i.hasNext();)
第10题:
现有: 3.import java.util.*; 4.class ForInTest { 5.static List list = new ArrayList(); 6.public static void main (String [] args){ 7. 8.list.add("a"); list.add("b"); list.add("c"); 9.//insert code here 10.System.out.print(o); } } 哪一行插入到第9行将导致输出“abc”?()