单选题class A {   public int getNumber(int a) {   return a + 1;   }   }    class B extends A {   public int getNumber (int a) {   return a + 2   }    public static void main (String args) {   A a = new B();   System.out.printIn(a.getNumber(0));   }   }   Wha

题目
单选题
class A {   public int getNumber(int a) {   return a + 1;   }   }    class B extends A {   public int getNumber (int a) {   return a + 2   }    public static void main (String args) {   A a = new B();   System.out.printIn(a.getNumber(0));   }   }   What is the result? ()
A

 Compilation succeeds and 1 is printed.

B

 Compilation succeeds and 2 is printed.

C

 An error at line 8 causes compilation to fail.

D

 An error at line 13 causes compilation to fail.

E

 An error at line 14 causes compilation to fail.

参考答案和解析
正确答案: C
解析: 暂无解析
如果没有搜索结果或未解决您的问题,请直接 联系老师 获取答案。
相似问题和答案

第1题:

能将程序补充完整的选项是______。 class Person{ private int a; phblic int change(int m){return m;} } public class Teacher extends Person{ public int b; public static void main(String arg[ ]){ Person p=new Person( ); Teacher t=new Teacher( ); int i; ______; } }

A.i=m

B.i=b

C.i=p.a

D.i=p.change(50)


正确答案:D
解析: 选项B中虽然b是类Teacher的public成员变量,但在静态方法中,不能使用类中的非静态成员;选项C中的a是Person类的private成员,不能在类外直接引用;选项D中的change(int m)方法是public方法,并且返回一个int型值,可以通过类的实例变量p引用并赋值给一个int型变量。

第2题:

执行下列代码后,输出的结果为( )。 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。

第3题:

class A

{public int getNumber(int a){return a+1;}}

class B extends A

{public int getNumber(int a, char c){return a+2;}public static void main(String[] args){B b=new B();System.out.println(b.getNumber(0));}}

what is the result?()

A.compilation succeeds and 1 is printed

B.compilation succeeds and 2 is printed

C.compilation succeeds and 3 is printed

D.An error at this program cause compilation to fail


参考答案:A

第4题:

以下程序的输出结果为:

public class test {

public static void main(String args[]) {

int s=0;

for (int k=0;k<=10;k++)

s+=method(2,k)-1;

System.out.println(s);

}

public static int method(int n,int m) {

if (m==0)

return 1;

else

return n*method(n,m-1、;

}

}

A. 2048

B. 1024

C. 2036

D.2000


正确答案:C

第5题:

下列程序段中,正确的是______。 ①class MvClass { int var = 100; static int getVar() { return var; } } ②public class MyClass { final int date; void MyClass (int d) { date = d; } } ③public class MyMain { public static void main(String args[]) { System.out.println(Myclass1.date); } } class MyClass1 { int data = 10; } ④class IamAbstract { final int f; double d; abstrct void method(); }

A.②④

B.①③

C.②

D.以上都不对


正确答案:D

第6题:

下面程序的输出结果是什么? 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

第7题:

能将程序补充完整的选项是 class Person { private int a; public int change(int m){return m; } } public class Teacher extends Person { public int b; public static void main(String arg[]) { Person p=new Person(); Teacher t=new Teacher(); int i; ______ } }

A.i=m

B.i=b

C.i=p.a

D.i=p.change(50)


正确答案:D
解析:本题考查类的声明。选项A中m没有被声明过,不能使用;选项B中虽然b是类Teacher的public成员变量,但在静态方法中,不能使用类中的非静态成员;选项C中a是类Person的private成员,在类外不能直接引用;选项D中change(int m)方法是public方法,并且返回一个int型值,可以通过类的实例变量p引用并赋值给一个int型变量。

第8题:

能将程序补充完整的选项是( )。class Person{ private int a; public int change(int m){ return m; }}public class Teacher extends Person{ public int b; public static void main(String arg[]) { Person p = new Person(); Teacher t = new Teacher(); int i; ______ }} B.

A.i=m

B.i=b

C. i=p.a

D.i=p. change(50)


正确答案:D

第9题:

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 来明确。

第10题:

1. class A {  2. public int getNumber(int a) {  3.     return a + 1;  4. }  5. }  6.    7. class B extends A {  8. public int getNumber (int a) {  9. return a + 2  10. }  11.    12. public static void main (String args[])  {  13. A a = new B();  14. System.out.printIn(a.getNumber(0));  15.    } 16. }     What is the result?()  

  • A、 Compilation succeeds and 1 is printed.
  • B、 Compilation succeeds and 2 is printed.
  • C、 An error at line 8 causes compilation to fail.
  • D、 An error at line 13 causes compilation to fail.
  • E、 An error at line 14 causes compilation to fail.

正确答案:B

更多相关问题