计算机二级

有如下的代码段,当编译和运行时,下列各选项中说法正确的是( )。 public class Z { public static void main(String args[]) { new Z (); } Z() { Z alias1 = this; Z alias2 = this; synchronized(alias1) { try { alias2.wait(); System.out.println("DONE WAITING"); } catch (InterruptedException e)

题目

有如下的代码段,当编译和运行时,下列各选项中说法正确的是( )。 public class Z { public static void main(String args[]) { new Z (); } Z() { Z alias1 = this; Z alias2 = this; synchronized(alias1) { try { alias2.wait(); System.out.println("DONE WAITING"); } catch (InterruptedException e) { System.out.println( "INTERRUPTED"); } catch (Exception e) { System.out.println("OTHER EXCEPTION"); } finally { System.out.println ("FINALLY"); } } System.out.println("ALL DONE"); } }

A.应用程序编译正常,但是不打印任何数据

B.应用程序编译正常,并打印数据“DONE WAITING”

C.应用程序编译正常,并打印数据“FINALLY”

D.应用程序编译正常,并打印数据“ALL DONE”

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

第1题:

阅读下列代码后 public class Person{ int arr[]=new int[10]; public static void main(String args[]){ System.out.println(arr[1]); } } 正确的说法是( )。

A.编译时将产生错误

B.编译时正确,运行时将产生错误

C.输出零

D.输出空


正确答案:A
解析:本题考查考生对Java中数组的定义及使用。int arr[]=new int[10]表示数组arr是一个含有10个元素的整数数组。Java中的数据类型必须实例化后才能使用,但是有种情况例外,就是该成员是用static声明的。题目中对于数组并没有实例化,因此不能使用,所以选项A说法正确。如果加上static修饰符,改为 static int arr[]=new int[10]或者将该数组实例化即可,输出为0。

第2题:

已知如下代码: public class Test { long a[]=newlong[10]: public static void main(String arg[]){ System.out.println(a[6]): } } 请问哪个语句是正确的( )。

A.输出为null

B.输出为0

C.编译时出错

D.运行时出错


正确答案:B

第3题:

阅读下面的程序: public class Person{ int arr[ ]=Hew int[10]; public static void main(String args[ ]){ System.out.println(arr[1]); } } 正确的说法是______。

A.编译时将产生错误

B.编译时正确,运行时将产生错误

C.输出为0

D.输出为空


正确答案:A
解析: 由于数组arr声明时未使用static关键字,而main( )方法直接引用arr(而非通过 Person的实例引用),将产生“非静态变量不能从静态上下文中引用” (non-static variable cannot be referenced from a static context)的编译错误。

第4题:

执行下列代码段之后,变量z的值为______。 Public class Test8 { public static void main(String[] args) { int x=2; int y=3; int z=4; z-....= y-x--; System.out.println(z); }

A.1

B.2

C.3

D.4


正确答案:D
解析:表达式中的运算次序应该是先对y做减量运算,得到y=2,然后再取x的值x=2,做减法运算得到0,最后用z减去0,得到答案为4。

第5题:

阅读下面代码 public class Person { static int arr[]=new int[10]; public static void main(String args) { System.out.println(arr[9]); } } 该代码的运行结果是

A.编译时将产生错误

B.编译时正确,运行时将产生错误

C.输出零

D.输出空


正确答案:B
解析:Java程序中,main()方法的格式为public staric void main(String args[]) { },返回值为void,参数必须为字符数组。本题目程序的参数不是字符数组,编译不会出错,但是运行时会找不到main()方法,程序无法执行。

第6题:

阅读下列代码

Public class Person{

Static int arr[ ] = new int (10);

Public static void main (String args ) {

System.out.println(arr[9]);

}

}

该代码运行的结果是

A )编译时将产生错误

B )编译时正确,运行时将产生错误

C )输出 0

D )输出空


正确答案:C

第7题:

如下两个源程序文件,分别编译后,运行Example.class文件,运行结果为______。

AB.java文件代码如下;

package test;

public class AB

{

int a=60;

public void show()

{

System.out.println(”a=”+a);

}

Example.java文件代码如下:

import test.AB;

class Example

{

public static void main(String args[])

{

AB bj=new AB();

obj.show();

}

}


正确答案:a=60
a=60

第8题:

阅读下列代码段,选出该代码段的正确文件名( )。 class A { void methodl() { System.out.println("methodl in class A"); } } public class B { void method2() { System.out.println("method2 in class B"); } public static void main(String args[]) { System.out.println("main in class B"); } }

A.A.java

B.A.class

C.B.class

D.B.java


正确答案:D
解析:Java源程序文件是以.java为后缀的,Java字节码文件以.class为后缀,而且 Java源文件中只能有一个public类,该类的名字为源文件名,程序段中类B是以public修饰的,因此源程序文件名为B.java。

第9题:

给出下列的程序,其叙述正确的是public class Man { static int arr[]= new int [10]; public static void main (String a []){ System.out.println(arr[1]); }}

A.编译时将发生错误

B.编译时正确但是运行时出错

C.输出为0

D.输出为null


正确答案:C
解析:由于数组元素是整型,所以其初始值为0。

第10题:

阅读下列代码段,选出该代码段的正确的文件名( )。 class A { void method () { System.out.println ("methodl in class A"); } } public class B { void method2 () { System.out.println("method2 in class B"); } public static void main (String args[]) { System.out.println ("main () in class B"); } }

A.A.java

B.A.class

C.B.class

D.B.java


正确答案:D
解析:Java源文件以.java为后缀,Java字节码文件以.class为后缀。Java源文件中只有一个public的类,该类的名字为源文件名,这里类B是以public修饰的,因此源文件名为B.java。

更多相关问题