给定如下Java程序片断:  class A{  public A (){   System.out.println("A");  } }  class B extends A{  public B(){  System.out.println("B"); }  public static void main(String[] args){    B b=new B();  } }  上述程序将()。 A、不能通过编译B、通过编译,输出为:A BC、通过编译,输出为:BD、通过编译,输出为:A

题目

给定如下Java程序片断:  class A{  public A (){   System.out.println("A");  } }  class B extends A{  public B(){  System.out.println("B"); }  public static void main(String[] args){    B b=new B();  } }  上述程序将()。 

  • A、不能通过编译
  • B、通过编译,输出为:A B
  • C、通过编译,输出为:B
  • D、通过编译,输出为:A
如果没有搜索结果或未解决您的问题,请直接 联系老师 获取答案。
相似问题和答案

第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 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。

第3题:

给定如下Java程序片断:classA{publicA(){System.out.println("A");}}classBextendsA{publicB(){System.out.println("B");}publicstaticvoidmain(String[]args){Bb=newB();}}上述程序将()。

A.不能通过编译

B.通过编译,输出为:AB

C.通过编译,输出为:B

D.通过编译,输出为:A


参考答案:B

第4题:

编译运行以下程序后,关于输出结果的说明正确的是( )。 public class Conditional{ public static void main (String args[]){ int x=2: System.out.println("value is”+ ((x<1)?2:2)); } }

A.输出结果为:valueis22.2

B.输出结果为:value is 2

C.输出结果为:value is 2.0

D.编译错误


正确答案:C

第5题:

下面程序段的输出结果为 package test; public class A { int x=20; static int y=6; public static void main(String args[]) { Class B b=new Class B(); b.go(10); System.out.println(”x=”+b.x); } } class Class B { int x; void go(int y) { ClassA a=new ClassA(); x=a.y; } }

A.x=10

B.x=20

C.x=6

D.编译不通过


正确答案:C
解析:本题考查在Java中静态变量(类变量)的用法。在题目程序段中生成了一个staticinty=6类变量,在ClassA中调用的b.go(10),只不过是在ClassB中的一个局部变量,通过调用ClassB中的go方法可以生成一个ClassA对象,并给这个新生成的对象赋以ClassA中的类变量y的值。从main()方法作为入口执行程序,首先生成一个ClassB的对象,然后b.go(10)会调用ClassA,会给x和y赋值,x=a.y后,x值为6,再返回去执行System.out.println(”x=”+b.x)语句,输出为x=6,可见,正确答案为选项C。

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

编译运行下程序后,关于输出结果的说法正确的是( )。 public class conditional{ public smile void main(string args[]){ int x=4; System.out.println("alue is "+((x>4)?99:9)); } }

A.输出结果为 value is 99.99

B.输出结果为 value is 9

C.输出结果为 valueis9.0

D.编译错误


正确答案:C

第8题:

阅读下面的程序: 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)的编译错误。

第9题:

给定如下JAVA程序片断下述程序将()。

A.不能通过编译

B.通过编译,输出为:AB

C.通过编译,输出为:B

D.通过编译,输出为:A


正确答案:B

第10题:

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

A.编译时将发生错误

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

C.输出为0

D.输出为null


正确答案:C

更多相关问题