执行后,屏幕输出( A )。
A. equals B. == C. == equals D. 不输出 7、下面的表达式中正确的是( AC )。
A. String s=\你好\int i=3; s+=i; B. String s=\你好\int i=3; if(i==s){s+=i}; C. String s=\你好\int i=3; s=i+s; D. String s=\你好\int i=3; s=i+; 8、下列方法中,不属于类String的方法是( D )。
A. toLowerCase () B. valueOf () C. charAt () D. append () 9、下面哪个方法被在调用时有可能会改变原有对象?( C )。 A. String的toLowerCase( ) B. String的replace( ) C. StringBuffer的reverse( ) D. StringBuffer的length( ) 10、下面哪些语句含有语法错误?( AB )
A. StringBuffer s=\B. System.out.println((new StringBuffer(\
C. String s=\new StringBuffer(\D. String s= new StringBuffer(\11、有下列程序: public class aa{
public static void main(String[ ] args) { String s = \try {
double number1=Double.parseDouble(s); System.out.print(number1); int number2 = Integer.parseInt(s); System.out.println(\\ }catch(NumberFormatException nfe) { System.out.println(\Sorry\}catch(Exception e){ } } }
执行结果是( C )。
A. 18.03 B. Sorry C. 18.03 Sorry D. 程序编译错误
第六章 java.util包和**框架 一、选择题。
1、 下列( A )**不能含有重复元素。 A. Set B. List C. Map D. Collection
2、 当元素个数固定时,使用( D )**查找效率最高。 A. Set B List C. Map D. Arrays
3、 你希望存储少量数据并能快速访问. 你并不需要排序这些数据,那种数据结构最适合这种需求? ( D )
A. TreeSet B. HashMap C. LinkedList D. 数组
4、 欲构造ArrayList类的一个实例,此类继承了List接口,下列哪个方法是正确的 ?( B )
A. ArrayList myList=new Object(); B. List myList=new ArrayList(); C. ArrayList myList=new List(); D. List myList=new List();
5、 关于HashMap和TreeMap类,判断下列那种说法是正确的?( A) A. 它们都能方便地定位映射中的值对象 B. 它们都实现了SortedMap接口 C. 它们都能高效地按照顺序遍历键 D. 以上说法都不对
6、 下列哪个不是Iterator接口所定义的方法?( D ) A. A. hasNext() B. next() C. remove() D. nextElement() 7、 关于链表结构,陈述错误的是哪个?( B ) A. 链表可动态增长
B. 在链表中查找对象是最有效的 C. 链表中的元素可以重复
D. 通常情况下,向链表中插入元素的效率高于向ArrayList中插入元素的效率 8、 java语言的**框架类定义在( B )语言包中。 A. java.lang B. java.util C. java.array D.java.collections
二、综合编程题
1、 设有一数列:a1=3,a2=8,??,an=2an-1+2an-2,使用堆栈结构输出an的若干项。 [解答]:代码如下,运行程序时需要输入一个参数,指出想要输出数列的前多少项 import java.util.Stack; public class StackShow {
public static void main(String[] args) { Stack st = new Stack();
int count = Integer.valueOf(args[0]).intValue();
int temp;
Integer first = new Integer(3); Integer second = new Integer(8); st.add(first); st.add(second);
for (int i = 0; i < count - 2; i++) { temp = first.intValue() + second.intValue(); st.add(new Integer(temp)); first = second;
second = new Integer(temp); }
System.out.println(\输出这个系列的前\+ count + \个数:\Object result[] = st.toArray(); int wanghang = 0;
for (int i = result.length - 1; i >= 0 ; i--) { System.out.print(st.pop() + \\wanghang++;
if(wanghang % 5 == 0){ System.out.println(\} } } }
2、 利用映射结构保存由学号和姓名组成的键—值对,按学号的自然顺序将这些键值对一一打印出来。 import java.util.*; public class Student{ private String id; private String name; private int phoneNumber;
Student(String sId,String sName,int iPhone){ id=sId; name=sName; phoneNumber=iPhone; }
public String toString(){
return (id+\}
public static void main(String[] args){ Map students=new TreeMap();
Student s1=new Student(\张惠妹\students.put(s1.id,s1);
Student s2=new Student(\周杰伦\students.put(s2.id,s2);
Student s3=new Student(\王力宏\students.put(s3.id,s3);
Student s4=new Student(\周华健\students.put(s4.id,s4);
Student s5=new Student(\苏芮\students.put(s5.id,s5); Set keys=students.keySet(); Iterator it=keys.iterator(); Student s; String key; while(it.hasNext()){ key=(String)it.next();
System.out.println(students.get(key)); } } }
第七章 java.io包和输入输出 一、选择题
1、 下面关于System.out的说法哪个正确?( ABC ) A. System.out是一个PrintStream B. System.out是一个OutputStream C. System.out是一个FilterOutputStream D. System.out是一个PrintStream
2、 下面哪个语句可以建立文件“file.txt”的字节输入流?( A ) A. InputStream in=new InputStream(\B. InputStream in=new FileReader(\
C. FileInputStream in=new FileOutputStream(\D. FileInputStream in=new FileInputStream(\
3、 当前文件系统中已经存在了文件file.txt,该文件包涵有ASCII文本,代码片段如下: try {
File f = new File(\
outputStream out = new FileOutputStream(f, true); }catch (IOException) { }
结果是什么?( B ) A. 代码不能编译
B. 代码可以编译并运行,对该文件不会有改变 C. 代码可以编译并运行,并将该文件的长度设置为0 D. 抛出异常,因为该文件没有关闭
E. 代码可以编译运行,并从文件系统中删除该文件。
4、 下面哪个选项可以将“hello”字符写入文件file.txt的末尾?( B ) A. OutputStream out= new FileOutputStream (\Out.writeBytes (\
B. OutputStream os= new FileOutputStream (\true); DataOutputStream out = new DataOutputStream(os); out.writeBytes (\
C. OutputStream os= new FileOutputStream (\DataOutputStream out = new DataOutputStream(os); out.writeBytes (\
D. OutputStream os= new OutputStream (\true); DataOutputStream out = new DataOutputStream(os); out.writeBytes (\
5、 哪个类是FilterOutputStream类构造器的合法参数?( B ) A. InputStream