阅读下列程序片段。如果sayHello( )方法正常运行,则test( )方法的运行结果将是( )。A.HelloB.ArrayIndexOutOfBondsExceptionC.ExceptionFinallyD.HelloFinally

题目

阅读下列程序片段。如果sayHello( )方法正常运行,则test( )方法的运行结果将是( )。

A.Hello

B.ArrayIndexOutOfBondsException

C.ExceptionFinally

D.HelloFinally


相似考题
参考答案和解析
正确答案:D
sayHello()方法正常运行则程序不抛出异常,并执行finally,所以为符合选项D。
更多“ 阅读下列程序片段。如果sayHello( )方法正常运行,则test( )方法的运行结果将是( )。A.HelloB.ArrayIndexOutOfBondsExceptionC.ExceptionFinally”相关问题
  • 第1题:

    阅读下面程序 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()方法未定义的错误。

  • 第2题:

    阅读下列程序片段。 Public void test{ Try{ sayHello; system.out.println("hello"): }catch(ArraylndexOutOfBoundException e){ System.out.println("ArraylndexOutOfBoundExcep— tion"); }catch(Exception e){ System.out.println("Exception"): }finally{ System.Out.println("finally"); } } 如果sayHello方法正常运行,则test方法的运行结果将是( )。

    A.Hello

    B.ArraylndexOutOfBondsException

    C.Exception Finally

    D.Hello Finally


    正确答案:D
    D。【解析】sayHello方法正常运行则程序不抛出异常,并执行finally,所以为D。

  • 第3题:

    (多线程的两种实现方法)阅读下面程序: 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.程序编译通过,且运行正常


    创建用户自己的线程子类;在定义的类中实现Runnable接口;通过Callable接口和FutureTask类来实现异步线程;使用ThreadPoolExecutor等类产生线程池

  • 第4题:

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

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

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

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

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


    正确答案:A
    解析:本题考查线程的使用。Java中可以通过实现Runnable接口来创建线程。通过这种方式创建线程是把Runnable的一个对象作为参数传递给Thread类的一个构造方法,该对象提供线程体run()。而题目中的程序只是实现了Runnable接口,而并没有调用Thread类的构造方法创建线程对象。程序只是生成一个Test类对象t,不是线程对象。然后调用该对象的start()方法,而在Test类中没有定义start()方法。因此,题目中的程序不能通过编译,因为start()方法在Test类中没有定义。本题的正确答案是选项A。

  • 第5题:

    阅读下列程序片段 Publicvoidtest(){ Try{ sayHello(); system.out.println(“hello"); }catch(ArrayIndexOutOfBoundExceptione){ System.out.println(“ArraylndexOutOfBoundException”); }catch(Exceptione){ System.out.println(“Exception”); }finally{ System.out.println(“finally”); } } 如果sayHello()方法正常运行,则test()方法的运行结果将是( )。

    A.Hello

    B.ArraylndexOutOfBondsException

    C.Exception Finally

    D.Hello Finally


    正确答案:D
    解析:sayHello()方法正常运行则程序不抛出异常,并走finally,所以为D。