现有:  class Top  {  static int x=l;  public Top (inty)  {  x*=3;  }      }  class Middle extends Top {      public Middle()  {x+=1;  )  public  static void main (String  []  args)  {      Middle m = new Middle();      System. out .println (x);      }     }

题目

现有:  class Top  {  static int x=l;  public Top (inty)  {  x*=3;  }      }  class Middle extends Top {      public Middle()  {x+=1;  )  public  static void main (String  []  args)  {      Middle m = new Middle();      System. out .println (x);      }     }      结果为:()     

  • A、1
  • B、2
  • C、3
  • D、编译失败
如果没有搜索结果或未解决您的问题,请直接 联系老师 获取答案。
相似问题和答案

第1题:

请将下面程序补充完整。

public class PowerCalc{

public static void main(String[]args){

double x=5.0;

System. out. println(x+"to the power 4 is"+power(x, 4));

System. out. println("7. 5 to the power 5 is"+power(7.5, 5));

System. out. println("7.5 to the power 0 is"+power(7.5, 0));

System. out. println("10 to the power -2 is"+power(10, -2));

}

static double【 】 (double x, int n){

if(n>1)

return x * power(x, n-1);

else if(n<0)

return 1.0/power(x, -n);

else

return n==0 ? 1.0:x;

}

}


正确答案:【 】power
【 】power 解析:通过程序片段可看出,在main方法中调用了。power方法,所以需要在类中对power方法进行定义,否则编译会报错。下面的程序片段就是对power方法的定义。

第2题:

执行下列代码段之后,x的值为______。 public class ex25 { public static void main(String[] args) { int x=12; int m=x%5; x>>>=m; System.out.println(x); }

A.7

B.3

C.0

D.1


正确答案:B

第3题:

下面程序的输出结果是什么? class C1{ static int j=0; public void method(int a){ j++; } } class Test extends C1{ public int method(){ return j++; } public void result(){ method(j); System.out.println(j+method()); } public static void main(String args[]){ new Te

A.0

B.1

C.2

D.3


正确答案:C

第4题:

设有如下类

class Loop{

public static void main(String[] agrs) {

int x=0;int y=0;

outer:

for(x=0;x<100;x++){

middle:

for(y=0;y<100;y++){

System.out.println("x="+x+"; y="+y);

if(y==10){<<>>}

}

}

}

}

在<<>>处插入什么代码可以结束外循环?

A.continue middle;

B.break outer;

C.break middle;

D.continue outer;

E.none of these


正确答案:B

第5题:

执行如下语句之后,输出的结果是______。 public class ex24 { public static void main(String[] args) { int x=5,y=3; x+=X-- *--y; System.out.println{x); } }

A.0

B.1

C.true

D.false


正确答案:C

第6题:

interface A{

int x = 0;

}

class B{

int x =1;

}

class C extends B implements A {

public void pX(){

System.out.println(x);

}

public static void main(String[] args) {

new C().pX();

}

}


正确答案:

 

错误。在编译时会发生错误(错误描述不同的JVM 有不同的信息,意思就是未明确的

x 调用,两个x 都匹配(就象在同时import java.util 和java.sql 两个包时直接声明Date 一样)。

对于父类的变量,可以用super.x 来明确,而接口的属性默认隐含为 public static final.所以可

以通过A.x 来明确。

第7题:

下面程序段的输出结果为 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。

第8题:

执行下列代码后,输出的结果为( )。 class Base { int x = 30; void setX( ) {x=1O;} } class SubClass extends Base { int x=40; void setX ( ) {x=20;} int getX( ) {return super. x; } } public class Test { public static void main(String[ ] args) { SubClass sub=new SubClass( ); sub. setX( ); System. out. println(sub, getX( ) ); } }

A.10

B.20

C.30

D.40


正确答案:C
解析:本题主要考查有关类的继承方面的知识。Java中,类是分层次的,当子类的成员变量与父类的成员变量名字相同时,子类的成员变量会隐藏父类的成员变量,当子类的成员方法与父类的成员方法名字、参数列表、返回值类型都相同时,子类的方法是父类的方法的重写。这样,在子类的对象调用方法时,是按照子类中方法定义执行,隐藏父类的方法的定义。当子类隐藏了父类的变量,并重写了父类的方法后,又要使用父类变量或父类被重写的方法时,可通过super来实现对父类变量的访问和父类方法的调用。因此,本题中在main ()中调用setX ()时,是调用的SubClass类中的setX ()函数,同时将SubClass类中的i变量值设为20。当main ()函数中调用getX ()函数时,并不是取了SubClass类中的i的值,而是取的Base类中i变量的值,此时i的值为其初始值30。

第9题:

以下程序的运行结果为:public class A {static int k=3;public static void main(String[] args) {int k=4;A x1=new A();x1.k++;A x2=new A();x2.k++;k++;System.out.println(x1.k);}}

A. 3

B. 4

C.5

D.6

E.7


正确答案:C

第10题:

以下程序调试结果为:

public class Test {

int m=5;

public void some(int x) {

m=x;

}

public static void main(String args []) {

new Demo().some(7);

}

}

class Demo extends Test {

int m=8;

public void some(int x) {

super.some(x);

System.out.println(m);

}

}

A.5

B.8

C.7

D.无任何输出

E.编译错误


正确答案:B

更多相关问题