计算机二级

在oneMethod( )方法运行正常的情况下,程序段将输出什么? public void test( ){ try { oneMethod( ); System.out.println("condition 1"); } catch (ArrayIndexOutOfBoundsException e){ System.out.println("condition 2"); } catch(Exception e){ System.out.println("condition 3"); } finally

题目

在oneMethod( )方法运行正常的情况下,程序段将输出什么? public void test( ){ try { oneMethod( ); System.out.println("condition 1"); } catch (ArrayIndexOutOfBoundsException e){ System.out.println("condition 2"); } catch(Exception e){ System.out.println("condition 3"); } finally { System.out.pritln("finally"); } }

A.condition 1

B.condition 2

C.condition 3

D.condition 1

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

第1题:

下列关于如下这段代码的说法中正确的是( )。 public class test { public static void throwit() { throw new RuntimeException(); } public static void main(String args[]) { try{ System.out.println(“你好吗”); throwit(); System.out.println(“出错!”); } finally{ System.out.println(“结束”); } } }

A.以上程序段不会编译

B.程序输出“你好吗”,然后有RuntimeException异常发生,输出“出错!”,输出“结束”

C.程序输出“你好吗”,然后有RuntimeException异常发生,然后输出“结束”

D.程序输出“你好吗”,然后输出“结束”,然后有RunthneException异常发生


正确答案:D
解析:此程序没有语法错误,可以通过编译,所以选项A错误。程序首先从执行try块开始,输出“你好”,然后执行throwit()方法。在throwit()方法中导致Runtime Exception异常的发生,因此,不会执行by块中throwit()方法后的语句。由于by块的后面存在finally块,在Java中,一旦定义了finally块,就保证一定会执行它,而不管是否发生了异常,所以执行finally块中的语句,从而输出“结束”。由于没有catch块来捕获发生的异常,程序的执行被中止。

第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题:

下列程序段的输出结果是【 】。

public class Test {

void printValue(int m) {

do {

System.out.println("The value is"+m);

}while (--m>10);

}

public static void main (String args[]) {

int i=10;

Test t= new Test();

t.printValue(i);

}

}


正确答案:Thevalue is 10
Thevalue is 10 解析:本题考查do-while循环的用法。do-while至少执行一次,在执行完do中的内容后,判断while中的条件是否为true。如果为true,就再执行do中的内容,然后再进行判断。依次类推,直到while的判断为false时退出循环,执行循环后面的内容。题目中m的值为10,当程序运行到do-while循环时,程序先执行一次循环然后再作判断,在判断条件--m>10时,其值为false,退出循环。因此只执行了一次输出操作,输出内容为:The value is 10。

第4题:

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

第5题:

下列程序的执行结果是 ( ) 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

第6题:

阅读下列代码 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”。

第7题:

请在下划线处填入代码,是程序正常运行并且输出 “ Hello! ”

Class Test 【 15 】 {

Public static void main (String[] arge){

Test t = new Test();

t.start();

}

Public void run(){

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

}


正确答案:

第8题:

阅读下列程序片段。

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.ExceptionFinally

D.HelloFinally


正确答案:D

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

第9题:

在oneMethod( )方法运行正常的情况下,程序段将输出( )。 public void test( ){ try {oneMethod( ); System.out.println ("condition 1"); }catch (ArratlndexOutOfBoundException e){ System.out.println("condition2"): }catch (Exception e){ System.out.println("condition 3"); }finnally{ System.out.println("finally"): } }

A.condition 1

B.condition2

C.condition 3

D.condition 1 finally


正确答案: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()方法未定义的错误。

更多相关问题