选择题 11. 关于下列代码编译或执行结果的描述中,正确的是______。
public class Test{
public static void main(String args[]){
TcstThread pml=new TestThread("One")
pm1. start()
TestThread pro2=new TestThread("Two")
pro2. start()
}
}
class TestThread extends Thread(
private String sTname="";
TestThread(String s){
sTname=s;
}
public void run(){
for(int i=0; i<2; i++){
try{
sleep (1000);
}catch (InterruptedException e){}
system. out. print (sTname+"");
}
}
}
A.不能通过编译,TestThread类中不能定义变量和构造方法 B.输出One One Two Two C.输出Two One One Two D.选项B或C都有可能出现
A B C D
D
[解析] 启动两个线程,线程之间没有进行同步,所以B和C均有可能。
12. 下列程序的功能是统计字符串中“array”的个数,在程序的空白处应填入的正确选项是
public class FindKeyWords{
public static void main(Sting[] args){
Sting text=
"An array is a data structur that stores a collection of"
+"values of the same type. You access each individual value"
+"through an integer index. For example, if a is an array"
+"of inergers,then a[i]is the ith integer in the array.";
int arrayCount=0;
int index=-1;
Sting arrarStr="array";
index=text.indexof(arrayStr);
While(index 0){
++arrayCount;
index+=arrayStr.length();
index=text.indexof(arrayStr,index);
}
System.out.println
("the text contains"+arrayCount+"arrays");
}
A B C D
D
[解析] while循环首先计算终止条件,当布尔表达式(termiantion)的值为true时,循环执行大括号中的语句。若某次判断其值为false,则结束循环。若首次计算条件就不满足,循环体中的语句一次都不会被执行。所以程序中只有当index>=0时才会执行while循环。因此,本题的正确答案是D。
13. 在下列程序的空白处,应填入的正确选项是______。
import Java.io*;
pulilc class Obj ectStreamTest{
publilc static void main(string args[]) throws IOException{
ObjectOutputStream oos=new ObjectOutputStream
(new FileOutputStream("serial.bin"));
Java until.Date d=new Java.until Date();
oos (d);
ObjectInputStream ois=
new ObjectInputStream(new FileOutputStream("serial.bin"));
try{
java until.date restoredDate=
(Java.until.Date)ois.readObject();
System.out.println
("read object back from serial.bin file:"
+restoredDate);
}
catch(ClassNotFoundException cnf) {
System.out.println("class not found");
}
}
A.WriterObject B.Writer C.BufferedWriter D.WriterObject
A B C D
D
[解析] java.io包中,提供了ObjectInputstream和ObjectOutputStream将数据流功能扩展至可读写对象。在ObjectInpulStream中用readObject()方法可以直接读取一个对象,ObjetOutputstream中用writeObject()方法可以直接将对象保存到输出流中。因此,本题的正确答案是D。
20. 阅读下列程序
public class VariableUse{
public static void main(string[]args){
int a;
if(a==8){
int b=9;
System. out. println("a="+a);
System. out. println("b="+b);
}
System. out. println("a="+a);
System. out. println("b="+b);
}
}
该程序在编译时的结果是______。
A.变量a未赋值 B.第二个System. out. println("b="+b)语句中,变量b作用域有错 C.第二个System. out. println("a="+a)语句中,变量a作用域有错 D.第一个System. out. println("b="+b)语句中,变量b作用域有错
A B C D
B
[解析] 局部变量b是在if(a==8){}里定义的,作用域也只在这个if语句范围内,第二个System. out. println("b="+b)语句中,变量b超出了作用域。
21. 在下列程序的空白处,应填入的正确选项是______。
import java.io.*;
public class writeInt{
public static void main(string[]A) {
int[] myArray={10,20,30,40};
try{
DataoutputSystem dos=new DataOutputSystem(new
FileOutputSystem("ints dat"));
for(int i=0;i<myarray.length;i++)
dos writeInt(myArray[i]);
dos.______;
System.out.println("Have written binary file ints.dat");
}
catch(IOException ioe)
{ System.out.println("IOException");
}
}
}
A.start() B.close() C.read() D.write()
A B C D
B
[解析] 选项A中start()用于线程的启用和Applet的显示,不用于字节输出。由于空白处之前的语句已经执行完字节输出操作,不需要在执行读和写操作,所以选项C、D是错误的。在执行完字节输出操作后,需要关闭输出释放所占系统内存。因此,本题的正确答案是B。
34. 下列代码的执行结果是______
public class Test5{
public static void main(String args[]){
String s1=new String("hello");
String s2=new String("hello");
System.out.println(s1==s2);
System.out.println(s1.equals(s2));
}
}
A.true false B.true true C.false true D.false false
A B C D