下列程序的功能是在监控台上每隔一秒钟显示一个字符串”Hello!”,能够填写在程序中下画线位置,使程

题目

下列程序的功能是在监控台上每隔一秒钟显示一个字符串”Hello!”,能够填写在程序中下画线位置,使程序完整并能正确运行的语句是( )。 public class Test implements Runnable{ public static void main(String args[]){ Test t=new Test: Threadt t=new Thread(t); tt.start: } public void run{ for(;;){ try{ ; }catch(e){} System.out.println("Hello"); } } }

A.sleep(1000)

B.t.sleep(1000)InterruptedExceptionInterruptedException

C.Thread.sleep(1000)

D.Thread.sleep(1000)RuntimeExceptionInterruptedException

参考答案和解析
正确答案:D
D。【解析】本题考查Java中的线程和异常处理。题目首先通过实现Runnable接口创建线程,Testt=newTest语句定义了Test的1个实例,Threadtt=newThread(t)定义了1个名为tt的线程,tt.start语句启动线程。通过try-catch语句来处理异常。try代码包括一些简单语句或方法调用,遇到异常情况时,停止执行而跳转到相应处理异常的程序,然后由catch来控制。题目要求每间隔1s输出,间隔使用Thread.sleep(1000)语句来实现,调用InterruptedException来完成。RuntimeException类包含有较多子类,比如算术异常ArithmeticException,当除法分母为0等时使用;索引越界异常IndexOutOfBoundsException等。
如果没有搜索结果或未解决您的问题,请直接 联系老师 获取答案。
相似问题和答案

第1题:

阅读下面程序 public class Test implements Runnable { public static void main(String[] args) { ______ t.start(); } public void run() { System.out.println("Hello!"); } } 程序中下画线处应填入的正确选项是

A.Test t=new Test();

B.Thread t=new Thread();

C.Thread t=new Thread(new Test());

D.Test t=new Thread();


正确答案:C

第2题:

下列程序的功能是在监控台上每隔-秒钟显示-个字符串”Hello!¨,能够填写在程序中下画线位置,使程序完整并能正确运行的语句是( )。

A.sleep(1000)

B.t. sleep(1000) InterruptedExceptionInterruptedException

C.Thread. sleep(1000)

D.Thread. sleep(1000) RuntimeExceptionlnterruptedException


正确答案:D
本题考查Java中的线程和异常处理。题目首先通过实现Runnable接口创建线程,Testt=newTest()语句定义了Test的1个实例,Threadtt=newThread(t)定义了1个名为tt的线程,tt.slart()语句启动线程。通过try-catch语句来处理异常。try代码包括-些简单语句或方法调用,遇到异常情况时,停止执行而跳转到相应处理异常的程序,然后由catch来控制。题目要求每间隔ls输出.间隔使用Thread.sleep(1000)语句来实现,调用InterruptedExceI)lion来完成。RuntimeExeeption类包含有较多子类,比如,算术异常ArithmeticException,当除法分母为0等时使用;索引越界异常IndexOutOfBoundsException等。

第3题:

下列程序的功能是在监控台上每隔一秒钟显示一个字符串"Hello!",能够填写在程序中下划线位置,使程序完整并能正确运行的语句是

publicclassTestimplementsRunnable{

publicstaticvoidmain(Stringargs[]){

Testt=newTest();

Threadtt=newThread(t);

tt.start();

}

publicvoidrun(){

for(;;){

try{

}catchL______e){}

System.out.println("Hello");

}

}

}

A.

B.

C.

D.


正确答案:D
解析: 本题考查Java中的线程和异常处理。题目首先通过实现Runnable接口创建线程,TesttmTIewTest()语句定义了Test的1个实例,Threadtt=llewThread(t)定义了1个名为u的线程,tt.start()语句启动线程。通过try-catch语句来处理异常。 try代码包括一些简单语句或方法调用,遇到异常情况时,停止执行而跳转到相应处理异常的程序,然后由catch来控制。题目要求每间隔1s输出,间隔使用Thread.sleep(1000)语句来实现,调用IntemaptedException来完成。RuntimeException类包含有较多子类,比如算术异常ArithmeficExcepfon,当除法分母为0等时使用;索引越界异常IndexOutOfBoundsException等。

第4题:

下列程序的功能是在监控台上每隔一秒钟显示一个字符串“Hello!”,能够填写在程序中下划线位置,使程序完整并能正确运行的语句是public class Test implements Runnable{ public static void main(String args[]){ Test t=new Test(); Thread tt=new Thread(t); tt.start(); } public void run(){ for(;;){ try{ ; }catch( e){} System.out.println("Hello"); } } }

A.sleep(1000)

B.t.sleep(1000)InteruptedException InterruptedException

C.Thread.sleep(1000)

D.Thread.sleep(1000)RuntimeException InterruptedException


正确答案:D
解析:本题考查Java中的线程和异常处理。题目首先通过实现Runnable接口创建线程,Test t=new Test()语句定义了Test的1个实例,Thread tt=new Thread(t)定义了1个名为tt的线程,tt.start()语句启动线程。通过try-catch语句来处理异常。try代码包括一些简单语句或方法调用,遇到异常情况时,停止执行而跳转到相应处理异常的程序,然后由catch来控制。题目要求每间隔1s输出,间隔使用Thread.sleep(1000)语句来实现,调用InterruptedException来完成。RuntimeException类包含有较多子类,比如算术异常ArithmeticException,当除法分母为0等时使用;索引越界异常 IndexOutOfBoundsException等。

第5题:

在下画线处填入代码,使程序正常运行并且输出“Hello!”。

class Test ______ {

public staric void maim (string[] args) {

Test t=new Test();

t.start();

}

public void run() {

System.out.println("Hello!");

}

}


正确答案:extends Thread
extends Thread 解析:创建线程有两种方法:实现java.lang.Runnable接口和继承Thread类并重写run()方法。从创建线程实例的语句Test t=new Test();可以看出本程序通过继承Thread类来创建进程,因此答案为extends Thread。

第6题:

下列程序的功能是将一个整数数组写入二进制文件,在程序的下画线处应填人的选项是( )。

A.writeArray

B.writeByte

C.writeInt

D.writeDouble


正确答案:C
向流中写入整数数组,用writeInt方法。

第7题:

下列程序的功能是创建一个显示5个“Hello!”的线程并启动运行,试将程序补充完整。

public class ThreadTest extends Thread{

public static void main(String args[ ]){

ThreadTest t=new______;

t.start( );}

public void run( ){int i=0;

while(true){System.out.println("Hello!");

if(i++==4)break;

}

}


正确答案:ThreadTest( )
ThreadTest( ) 解析: ThreadTest继承自java.lang.Thread类,重写了run( )方法,实现了Java中的线程。ThreadTest t定义了空的线程对象,t.start( )方法启动了这个线程,因此ThreadTest t=new ______;就应该是实例化此线程对象,所以应填入“ThreadTest( )”。

第8题:

下列程序的功能是在监控台上每隔一秒钟显示一个字符串“你好!”,能够填写在程序中画线位置,使程序完整并能正确运行的语句是 public class Exam implements Runnable{ public static void main(String args[]){ Exam t=new Exam(); Thread tt=new Thread(t); tt.start(); } public void run(){ for(;;){ try{ ; }catch(e){} System.out.println("你好!"); } } }

A.sleep(1) RuntimeException

B.t.sleep(1000) InterruptedException

C.Thread.sleep(1) InterruptedException

D.Thread.sleep(1000) InterruptedException


正确答案:D
解析:本题考查Java中的线程和异常处理。题目首先通过实现Runnable接口创建线程,Test t=new Test()语句定义了Test的 1个实例,Thread tt=new Thread(t)定义了1个名为n的线程,tt.start()语句启动线程。通过try-catch语句来处理异常。try代码包括一些简单语句或方法调用,遇到异常情况时,停止执行而转跳到相应处理异常的程序,然后由catch来控制。题目要求每间隔1s输出,间隔使用Thread.sleep(1000)语句来实现,调用InterruptedException来完成。 RuntimeException类包含有较多子类,比如算术异常ArithmeticException,当除法分母为0等时使用;索引越界异常IndexOutOfBoundsException等。

第9题:

下列程序的功能是在监控台上每隔一秒钟显示一个字符串"Hello",能够填写在程序中横线位置,使程序完整并能正确运行的语句是( )。 public class Test implements Runnable { public static void main (String[ ] args ) { Test t =new Test( ); Thread tt= new Thread(t); tt. start( ); public void run ( ) { for(; ; ){ try { __________; } catch (__________ e ) { } System. out. println (" Hello" ); } } }

A.sleep(1000); InterruptedException

B.t. sleep(1000); InterruptedException

C.Thread. sleep(1000); RuntimeException

D.Thread. sleep(1000); InterruptedException


正确答案:D
解析:当清除线程调用因暂停类方法而产生的中断状态时,会发送InterruptedException类异常。