单选题阅读下列程序片段: public void test(){try{sayHello();System.out.println("hello");}catch(ArrayIndexOutOfBoundException e){System.out.println("ArrayIndexOutOfBoundException");}catch(Exception e){System.out.println("Exception");}finally{System.out.println("finally

题目
单选题
阅读下列程序片段: public void test(){ try{ sayHello(); System.out.println("hello"); } catch(ArrayIndexOutOfBoundException e){ System.out.println("ArrayIndexOutOfBoundException"); } catch(Exception e){ System.out.println("Exception"); } finally{ System.out.println("finally"); } }如果sayHello()方法正常运行,则test()方法的运行结果将是(  )。
A

hello

B

ArrayIndexOutOfBoundException

C

Exception
  finally

D

hello
  finally


相似考题
更多“阅读下列程序片段: public void test(){ try{ sayHello(); System.out.pr”相关问题
  • 第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

  • 第2题:

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

  • 第3题:

    阅读下面程序 1 public class Try extends Thread { 2 public static void main(String args[]) { 3 Try t=new Try(); 4 t.start(); 5 } 6 7 public void run(int j) { 8 int i=0; 9 while(i<5) { 10 System.out.println("祝你成功!"); 11 i++: 12 } 13 } 14 } 该程序若能打印5行“祝你成功!”,必须改正程序中的某行代码,选择正确的修改是

    A.将第1行的extends Thread改为implements Runnable

    B.将第3行的new Try()改为new Thread()

    C.将第4行的t.start()改为start(t)

    D.将第7行的public void run(int j)改为public void run()


    正确答案:D

  • 第4题:

    下列程序片段中,能通过编译的是( )。

    A.public abstract class Animal{public void speak( );}

    B.public abstract class Animal{public void speak( ){};}

    C.public class Animal{pubilc abstract void speak( );}

    D.public abstract class Animal{pubile abstract void speak( ){};}


    正确答案:D
    Java中一个类是一个abstract类的子类,它必须具体实现父类的abstract方法。如果一个类中含有abstract方法,那么这个类必须用abstract来修饰(abstract类也可以没有abstract方法)。有abstract方法的父类只声明,由继承他的子类实现。所以选D。

  • 第5题:

    下列程序的运行结果是( )。 public class test{ private String[]data={¨10","10.5"); public void fun{ double s=0: for(int i=0;i<3;j++){ try{ s=s+Integer.parseInt(data[i]); catch(Exception e){ System.out.print("errorl:"+data[i]); } } } public static void main(string[]args){ try{ test d=new test: fun: }catch(Exception e){ System.OUt.println("error2") } } }

    A.errorl:10.5

    B.error2

    C.errorl:10.5 error2

    D.以上都不对


    正确答案:C
    C。【解析】try-catch块是可以嵌套分层的,并且通过异常对象的数据类型来进行匹配,以找到正确的catchblock异常错误处理代码。以下是通过异常对象的数据类型来进行匹配找到正确的catchblock的过程。①首先在抛出异常的try-catch块中查找catchblock,按顺序先与第一个catchblock块匹配,如果抛出的异常对象的数据类型与catchblock中传入的异常对象的临时变量(就是catch语句后面参数)的数据类型完全相同,或是它的子类型对象,则匹配成功,进入到catchblock中执行,否则到第2步;②如果有两个或更多的catchblock,则继续查找匹配第二个、第三个,直至最后一个catchblock,如匹配成功,则进入到对应的catchblock中执行,否则到第3步;③返回到上一级的try-catch块中,按规则继续查找对应的catchblock。如果找到,进入到对应的catchblock中执行,否则到第4步;④再到上上级的try-catch块中,如此不断递归,直到匹配到顶级的try-catch块中的最后一个catchblock,如果找到,进入到对应的catchblock中执行;否则程序将会执行terminate退出。所以本题选C。

  • 第6题:

    阅读下列程序片段。 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。

  • 第7题:

    阅读下列程序片段 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。

  • 第8题:

    下列程序片段中,能通过编译的是( )。 A.public abstract class Animal{ public void speak;}S

    下列程序片段中,能通过编译的是( )。

    A.public abstract class Animal{ public void speak;}

    B.public abstract class Animal{ public void speak{);}

    C.public class Animal{ pubilc abstract void speak;}

    D.public abstract class Animal{ pubile abstract void speak{};}


    正确答案:A
    A。【解析】Java中一个类是一个abstract类的子类,它必须具体实现父类的abstract方法。如果一个类中含有abstract方法,那么这个类必须用abstract来修饰(abstract类也可以没有abstract方法)。有abstract方法的父类只声明,由继承它的子类实现。所以选A。

  • 第9题:

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

    public class Try extends Thread{

    public static void main(String args[]){

    Threadt=new Try();

    【 】;

    }

    public void run(){

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

    }

    }


    正确答案:i
    i

  • 第10题:

    static void test() throws RuntimeException {  try {  System.out.print(”test “);  throw new RuntimeException();  }  catch (Exception ex) { System.out.print(”exception “); }  }  public static void main(String[] args) {  try { test(); }  catch (RuntimeException ex) { System.out.print(”runtime “); }  System.out.print(”end “);  }  What is the result?() 

    • A、 test end
    • B、 Compilation fails.
    • C、 test runtime end
    • D、 test exception end
    • E、 A Throwable is thrown by main at runtime.

    正确答案:D

  • 第11题:

    public class Test {} What is the prototype of the default constructor?()  

    • A、 Test()
    • B、 Test(void)
    • C、 public Test()
    • D、 public Test(void)
    • E、 public void Test()

    正确答案:C

  • 第12题:

    单选题
    编译如下的Java程序片段:  Class test{     Int count=9;     Public void a(){   Int count=10;   System.out,println(“count 1=” + count); }  Public void count(){   System.out.println(“count 2 =”+ count); }  Public static void main(String args[] ){   Test t=new Test();   t.a();   t.count(); } }  结果是()
    A

    不能通过编译

    B

    输出:count 1 =10  count 2=9

    C

    输出:count 1=9 count 2=9


    正确答案: A
    解析: 暂无解析

  • 第13题:

    下列关于Test类的定义中,正确的是______。

    A) class Test implements Runnabte{

    public void run(){}

    public void someMethod(){}

    B) class Test implements Rnuuable{

    public void run();

    }

    C) class Test implements Rnuuable{

    public void someMethod();

    }

    D) class Test implements Rnuuable{

    public void someMethod();{}

    }

    A.

    B.

    C.

    D.


    正确答案:A

  • 第14题:

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

  • 第15题:

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

  • 第16题:

    下列程序创建了一个线程并运行,横线处应填入的正确代码是( )。 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资源时,就可以脱离创建它的主线程独立地开始自己的生命周期了。

  • 第17题:

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

  • 第18题:

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

    A.Hello

    B.ArrayIndexOutOfBondsException

    C.ExceptionFinally

    D.HelloFinally


    正确答案:D
    sayHello()方法正常运行则程序不抛出异常,并执行finally,所以为符合选项D。

  • 第19题:

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

  • 第20题:

    阅读下面的程序public class Test {public static void main(String[] args) {for(int x = 0 ; x <=3 ; x++){continue;System.out.print(x%2);}}}运行结果正确的是

    A.跳出循环,无输出

    B.0121

    C.01

    D.0123


    答案:A
    解析:continue语句的作用是终止本次循环,因此contine后的代码永远不会被执行,都是无效代码

  • 第21题:

    以下语句能顺利通过编译: class test { static void sayHello() { this.toString(); } public String toString() { retur。()

    此题为判断题(对,错)。


    答案:错


  • 第22题:

    public class Test {  public static void aMethod() throws Exception {  try {  throw new Exception(); } finally {  System.out.println(“finally”);  }  }  public static void main(String args[]) {  try {  aMethod();  } catch (Exception e) {  System.out.println(“exception”);  }  System.out.println(“finished”);  }  }  What is the result?()  

    • A、 finally
    • B、 exception finished
    • C、 finally exception finished
    • D、 Compilation fails.

    正确答案:C

  • 第23题:

    单选题
    下列程序的运行结果是(  )。public class test{private String [] data = {"10","10.5"};public void fun(){ double s = 0; for(int i = 0; i try{ s = s + Integer.parseInt(data[i]); } catch(Exception e){ System.out.print("error1:" + data[i]); } }}public static void main(String[] args){ try{ test d = new test(); d.fun(); } catch(Exception e){ System.out.println("error2"); }} }
    A

    error1:10.5

    B

    error2

    C

    error1:10.5error2

    D

    以上都不对


    正确答案: D
    解析:
    Integer.parseInt(参数)函数作用是将字符类型数据转换为整型数据。当参数为一些不能转换为整型的字符时,会抛出异常。10.5是不可转换的,抛出异常,输出 error1:10.5。因为data数组溢出,所以抛出异常,输出error2。

  • 第24题:

    单选题
    public class Test {} What is the prototype of the default constructor?()
    A

     Test()

    B

     Test(void)

    C

     public Test()

    D

     public Test(void)

    E

     public void Test()


    正确答案: D
    解析: The correct answer to this question is C. The default constructor always takes the same access of the class. In this case, the class is public and so does the default constructor.