Java语言程序设计

单选题下列程序的运行结果是(  )。class Test extends Thread{ public static void main(String[] args) { Thread t=new Thread(); t.start(); } public void run() { System.out.println("Hello"); }}A 程序不能通过编译,因为没有import语句将Thread类引入B 程序不能通过编译,因为Test类没有实现Runnable接口C

题目
单选题
下列程序的运行结果是(  )。class Test extends Thread{  public static void main(String[] args)  {    Thread t=new Thread();    t.start();  }  public void run()  {    System.out.println("Hello");  }}
A

程序不能通过编译,因为没有import语句将Thread类引入

B

程序不能通过编译,因为Test类没有实现Runnable接口

C

程序通过编译,且运行正常,没有任何输出

D

程序通过编译,且运行正常,打印出一个"Hello"

如果没有搜索结果,请直接 联系老师 获取答案。
如果没有搜索结果,请直接 联系老师 获取答案。
相似问题和答案

第1题:

( 24 )请阅读下面程序

public class ThreadTest {

public static void main ( String args[ ]){

Thread t1 = new Thread ( new Hello ()):

Thread t2 = new Thread ( new Hello ()):

t l .start ():

t2.start ();

class Hello implements Runnable {

int i ;

public void run (){

while ( true ) {

System.out.println ( "Hello"+i++ ) ;

if ( i=5 ) break :

}

该程序创建线程使用的方法是()

A )继承 Thread 类

B )实现 Runnable 接口

C ) t l.start ()

D ) t2.start ()


正确答案:B

第2题:

阅读下面程序

class Test implements Runnable{

public static void main(String[] args){

Test t = new Test();

t.start();

}

public void run(){ }

}

下列关于上述程序的叙述正确的是

A) 程序不能通过编译,因为 start() 方法在 Test 类中没有定义

B) 程序编译通过,但运行时出错,提示 start() 方法没有定义

C) 程序不能通过编译,因为 run() 方法没有定义方法体

D) 程序编译通过,且运行正常


正确答案:A

第3题:

对下列程序的叙述中,正确的是

1:public class X extends Thread implements Runnable{

2:publ主c void run(){

3: System.out.println(“this is run()”);

4:}

5:public static void main(String args〔〕){

6:Threadt二new Thread(new X());

7:t.start();

8:}

9:}

A.第1行会产生编译错误

B.第6行会产生编译错误

C.第6行会产生运行错误

D.程序正常运行


正确答案:B

第4题:

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

A.Test t=flew Test()

B.Thread t=new Thread();

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

D.Test t=new Thread();


正确答案:C
解析:根据t. start()可知t应该是一个Thread类,排除A)。Thread类与Test类之间没有继承关系,所以排除D)。B)没有指定创建线程的对象,因此t. start()语句不能使Test类的run方法运行。所以选C)。

第5题:

阅读下列程序:class ThreadTest extends Thread{ public static void main(String[]args){ Thread t=new Thread(this); t.start(); } public void run(){ System.out.print("A thread test."); }} 对该程序而言,正确结论是 ( )

A.该程序能够通过编译并打印输出“A thread test.”

B.该程序能够通过编译,但运行时将不调用ThreadTest类中的run()方法,因此不产生任何输出

C.该程序存在编译错误,因为在main()方法中不能使用this指针

D.上述选项都不正确


正确答案:C
解析:该题是对this知识点的考查。该程序在编译存在错误,在main()方法中不能使用this。所以选项A、B、C的说法是错误的。

第6题:

通过实现Rmmable接口创建线程,请在下面横线处填写代码完成此程序。

public class ThreadTest

{

public static void main(String args [])

{

Thread testObj1 = new Thread (new Hello ());

Thread testObj2 = new Thread (new Hello ());

testObj 2.start ( );

}

}

class Hello implements Runnable

{

int j;

public void run()

{

System.out.println("Hello" + j ++);

}

}


正确答案:testObj 1.start();
testObj 1.start();

第7题:

请阅读下面程序 public class ThreadTest{ public static void main(String args[]) ( Thread t1=new Thread(new Hello()); Thread t2=new Thread(new Hello()); t1.start(); t2.start(); } } class Hello implements Runnable { int i; public void run() { while(true) { System.out.prinfin("Hello"+i++); if(i=5) break; } } } 该程序创建线程使用的方法是

A.继承Thread类

B.实现Runnable接口

C.t1.start()

D.t2.start()


正确答案:B
解析:本题考查线程的创建。Java中,线程的创建有两种方法:
  (1)通过实现Runnable接口创建线程。Runnable接口中只定义了一个run()方法作为线程体。
  (2)通过继承Thread类创建线程。Thread类本身实现了Runnable接口。
  无论使用哪种方法来创建线程,新建的线程不会自动运行,必须调用线程的start()方法,才能运行该线程。
  本题程序中的Hello类实现了Runnable接口,即采用的是第一种方法创建线程。因此,本题的正确答案是选项B。

第8题:

阅读下列代码 public class Test implements Runnable{ public void run(Thread t){ System. out. println("Running. "); } public static void main(String[]args){ Thread tt=new Thread(new Test()); tt. start(); } } 代码运行结果是

A.将抛出一个异常

B.没有输出并正常结束

C.输出“Running”并正常结束

D.程序第2行将出现一个编译错误


正确答案:C
解析:类Test实现Runnable接口,main函数中实例化了一个新的以Test对象为执行任务的线程对象,然后调用start()方法启动子线程,程序正常执行,子线程进入入口run()方法,输出字符串“Running”。

第9题:

阅读下面代码 public class Test implements Runnable { public void run(Thread t) { System.out.println("Running"); } public static void main(String[] args) { Thread tt=new Thread(new Test()); tt.start(); } } 代码运行的结果是

A.将抛出一个异常

B.没有输出并正常结束

C.输出“Running”并正常结束

D.程序第2行将出现一个编译错误


正确答案:D

第10题:

阅读下面程序 class Test implements Runnable { public static void main(String[] args) { Test t=new Test(); t.start(): } public void run() {} } 下列关于上述程序的叙述正确的是

A.程序不能通过编译,因为start()方法在Test类中没有定义

B.程序编译通过,但运行时出错,提示start()方法没有定义

C.程序不能通过编译,因为run()方法没有定义方法体

D.程序编译通过,且运行正常


正确答案:A
解析:创建线程有两种方法:实现java.lang.Runnable接口;继承Thread类并重写run()方法。start()是Thread类中的方法,而本程序中的Test类实现了Runnable接口,Runnable接口中只定义了一个抽象方法run(),故Test类不能调用start()方法。编译时会出现start()方法未定义的错误。

更多相关问题