单选题下列程序的运行结果是(  )。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"


相似考题
更多“下列程序的运行结果是(  )。class Test extends Thread{  public static voi”相关问题
  • 第1题:

    下面程序的输出结果是什么? class C1{ static int j=0; public void method(int a){ j++; } } class Test extends C1{ public int method(){ return j++; } public void result(){ method(j); System.out.println(j+method()); } public static void main(String args[]){ new Te

    A.0

    B.1

    C.2

    D.3


    正确答案:C

  • 第2题:

    下列程序的运行结果是【 】。 include class test { private: int num; public: tes

    下列程序的运行结果是【 】。

    include <iostream. h>

    class test

    {

    private:

    int num;

    public:

    test()

    int TEST() {return num+100;}

    ~test()

    };

    test::test(){num=0;}

    test::~test(){cout<<"Destructor is active"<<endl;}

    void main()

    {

    test x[3]

    cout<<x[1]. TEST()<<endl;

    }


    正确答案:100
    100 解析:本题比较简单,考查考生基本的类的定义,构造函数以及对象数组的概念。

  • 第3题:

    下列代码的执行结果是public class Test{ public int aMethod(){ static int i=0; i++; System.out.println(i); } public static void main(String args[]){ Test test= new Test(); test. aMethod(); }}

    A.编译错误

    B.0

    C.1

    D.运行成功,但不输出


    正确答案:A
    解析:static不能修饰局部变量。

  • 第4题:

    下列程序的执行结果是______。 class A5 extends Thread { boolean b; A5 (boolean bb) { b = bb; } public void run() { System.out.println(this.getName() + "运行"); } } public class Testl5 { public static void main(String[] args) { A5 a1 = new A5(true); A5 a2 = new A5(false); if(a1.b) A1.start(); if (a2 .b) A2.start(); } }

    A.Thread-0

    B.Thread-1

    C.Thread-0

    D.Thread-1 Thread-1 Thread-0


    正确答案:A
    解析:类A5继承了Thread类,并且重写了Thread类的run()方法,调用本线程的getName()方法打印出系统给本线程定义的名称。在main()方法中,a1和a2是A5的对象,它们对应的系统默认的线程名称分别是Thread—0和Thread—1,根据类A5的类变量b的布尔值控制哪一个线程调用start()方法,这里应该是a1线程被调度执行。

  • 第5题:

    阅读下面程序 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)。

  • 第6题:

    下面程序段的输出结果是 class Test{ public static void main(String args[]){ MyThread t=new MyThread(); t.displayOutput("t has been created)); t.start(); } } class MyThread extends Thread{ public void displayOutput(String s){ System.out.println(s); } public void run(){ displayOutput(t is running."); } }

    A.t has been created.

    B.t has been created. t is running.

    C.t is running.

    D.编译出错


    正确答案:A
    解析:本题考查线程的创建和调用。创建一个新的线程对象后,通过使用 start()方法就可以启动该线程,线程也就处于可运行状态Runnable。Start()方法产生了线程运行需要的系统资源,并调用线程体,也就是run()方法,使得线程可以进入运行状态。程序运行时首先创建一个新的线程对象t,并调用displayOutput(Strings)方法输出t has been created。t.start()方法调用run()方法,输出t is running,所以正确答案为选项A。

  • 第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 Try extends Thread{ public static void main(String args[]){ Thread t=new Try; ; } public void runf System.out.println(”Try!"); } }

    A.t.start

    B.t.class

    C.t.thread

    D.t.static


    正确答案:A
    A。【解析】start是类Thread的方法,其中start方法用于启动线程,使之从新建状态转入就绪状态并进入就绪队列排队,一旦轮到它来享用CPU资源时,就可以脱离创建它的主线程独立地开始自己的生命周期了。

  • 第9题:

    3下碳程序段的输出结果是( )。 class Test{ public static void main(Srting args[]){ MyThread t=new MyThread(); t.displayOutput("t has been created"); t.start(); } } class MyThread extends Thread{ public void displayOutput(String s){ SYStem.out.println(s); } public void run() { displayOutput("t is running."); } }

    A.t has been created.

    B.t has been created. t is running.

    C.t is running.

    D.编译出错


    正确答案:A

  • 第10题:

    阅读下面程序 public class Test2 ______ { public static void main(String[] args) { Thread t=new Test2(); t.start(); } public void run() { System.out.println("How are you."); } } 程序中下画线处应填入的正确选项是

    A.implements Thread

    B.extends Runnable

    C.implements Runnable

    D.extends Thread


    正确答案:D

  • 第11题:

    下列程序创建了一个线程并运行,请在下划线处填入正确代码。

    public class Try extends Thread{

    public static void main(String args[]){

    Threadt=new Try();

    【 】;

    }

    public void run(){

    System.out.println(“Try!”);

    }

    }


    正确答案:i
    i

  • 第12题:

    单选题
    下列程序的运行结果是(  )。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"


    正确答案: A
    解析:
    此程序继承了Thread,同时也有run方法,符合线程的创建规则,但是在创建线程对象时,所使用的的类为Thread,此处所创建对象毫无意义,不会运行任何结果,应该创建Test的对象,所以该程序会通过编译,且运行正常,但是没有任何输出。

  • 第13题:

    下列程序的执行结果是 ( ) public class Test { public int aMethod() { satic int i=0; i++; System.out.println(i); } public static void.main(String args[]) { Test test=new Test(); test.aMethod(); }

    A.编译错误

    B.0

    C.1

    D.运行成功,但不输出


    正确答案:A

  • 第14题:

    阅读下列程序: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的说法是错误的。

  • 第15题:

    阅读下面代码 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

  • 第16题:

    阅读下面程序 public class Test2______ { public static void main(String[] args){ Thread t=new Test2(); t. start(); } public void run(){ System. out. priatln("How are you. "); } } 在程序下画线处填入的正确的选项是

    A.implements Thread

    B.extends Runnable

    C.implements Runnable

    D.extends Thread


    正确答案:D
    解析:Thread类是多线程基类,多线程启动类必须继承此类。而实现Runnable接口的类能作为多线程的一个执行任务,一般作为参数传给新的Thread类。

  • 第17题:

    下列程序的输出结果是class Test{public static void main(String args[]){int n=7;n<<=3;n=n&am

    下列程序的输出结果是 class Test{ public static void main(String args[]){ int n=7; n<<=3; n=n&n+1|n+2^n+3; n>>=2; System.out.println(n); } }

    A.0

    B.-1

    C.14

    D.64


    正确答案:C
    解析:本题考查Java中的运算符。首先要清楚程序里面涉及的运算符的含义。“”是按位左移运算符,“&”是按位与运算符,“|”是按位或运算符,“^”是按位异或运算符。题目中整型变量n=7相当于二进制中的111,n=3语句执行后,n值为111000,相当于十进制的56,而语句n=n&n+1|n+2^n+3执行后,n值为57;n>>=2语句执行后,n的值为14,所以选项C正确。

  • 第18题:

    在下面程序的下画线处应填入的选项是 public class Test______{ public static void main(String args[]) { Test t=new Test(); Thread tt=new Thread(t); tt.start(); } public void run() { for(int i=0;i<5;i++) System.out.println("i="+i); } }

    A.implements Runnable

    B.extends Thread

    C.implements Thread

    D.extends Runnable


    正确答案:A
    解析:创建线程有两种方法:实现java.lang.Runnahle接口和继承Thread类并重写run()方法。从创建线程实例的语句Thread tt=new Thread(t);可以看出本程序将Test类的实例t作为参数传递给Thread类的构造方法,因此是通过实现Runnable接口创建线程。

  • 第19题:

    使下列程序正常运行并且输出“Hello!”,横线处应填写的内容是( )。 class Test { public static void main(string[]args){ Test t=new Test; start; } Public void run{ System.out.println("Hello!¨); )

    A.extends Thread

    B.extends Float

    C.extends Iostream

    D.extends Stdio


    正确答案:A
    A。【解析】从后面重写了run方法来看,这是通过继承Thread类,并重写run方法定义线程体,然后创建该子类的对象的方式来创建线程。

  • 第20题:

    下列程序的功能是在监控台上每隔一秒钟显示一个字符串”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等。

  • 第21题:

    下列程序运行的结果是( )。public class Test { public static void main(String[] args) { int a=2,b=3; System.out.println( (a>b?" *** a =" :" ###b = ") + A) ; } }

    A.*** a=2

    B.*** a=3

    C.### b=2

    D.### b=3


    正确答案:C
    解析:本题输出的结果是条件表达式(a>b?" *** a =":" ###b =")的值和a连接之后的结果,由于a,b不成立,因此条件表达式的结果为“###b =”,再和a连接后输出为“###b =2”。

  • 第22题:

    阅读下面程序 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

  • 第23题:

    ( 30 )在程序的下划线处应填入的选项是

    public class Test _________{

    public static void main(String args[]){

    Test t = new Test();

    Thread tt = new Thread(t);

    tt.start();

    }

    public void run(){

    for(int i=0;i<5;i++){

    system.out.println( " i= " +i);

    }

    }

    }

    A ) implements Runnable

    B ) extends Thread

    C ) implements Thread

    D ) extends Runnable


    正确答案:A

  • 第24题:

    单选题
    下列程序的运行结果是(  )。class Shape{ public Shape(){ System.out.print("Shape"); }}class Circle extends Shape{ public Circle(){ System.out.print("Circle"); }}public class Test{ public static void main(String[]args){ Shape d=new Circle(); }}
    A

    Shape

    B

    Circle

    C

    ShapeCircle

    D

    程序有错误


    正确答案: B
    解析:
    继承是面向对象编程的一个主要优点之一,它对如何设计Java类有着直接的影响。该程序首先编写了一个Shape的类,然后又编写一个类Circle去继承Shape类。由于子类拥有父类所有的属性和方法,所以输出的是ShapeCircle。