SCJP程序员认证考试

多选题interface A { public int getValue() }  class B implements A {  public int getValue() { return 1; }  }  class C extends B {  // insert code here  }  Which three code fragments, inserted individually at line 15, make use of polymorphism?()Apublic void ad

题目
多选题
interface A { public int getValue() }  class B implements A {  public int getValue() { return 1; }  }  class C extends B {  // insert code here  }  Which three code fragments, inserted individually at line 15, make use of polymorphism?()
A

public void add(C c) { c.getValue(); }

B

public void add(B b) { b.getValue(); }

C

public void add(A a) { a.getValue(); }

D

public void add(A a, B b) { a.getValue(); }

E

public void add(C c1, C c2) { c1.getValue(); }

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

第1题:

给出下面的接口: interface A{ int method1(int i); int method2(int j); } 下面那个类实现了这个接口,并且不是抽象的?()

A.class B implements A{ int method1(){} int method2(){} }

B.class B { int method1(int i){} int method2(int j){} }

C.class B implements A{ int method1(int i){} int method2(int j){} }

D.class B extends A{ int method1(int i){} int method2(int j){} }


正确答案:C

第2题:

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

第3题:

下列关于Test类的定义中,正确的是______。

A) class Test implements Runnabte{

public void run(){}

public void someMethod(){}

B) class Test implements Rnuuable{

public void run();

}

C) class Test implements Rnuuable{

public void someMethod();

}

D) class Test implements Rnuuable{

public void someMethod();{}

}

A.

B.

C.

D.


正确答案:A

第4题:

interface DeclareStuff{  public static final int EASY = 3;  void doStuff(int t); }  public class TestDeclare implements DeclareStuff {  public static void main(String [] args) {  int x=5;  new TestDeclare().doStuff(++x);  }  void doStuff(int s) {  s += EASY + ++s;  System.out.println(”s “ + s);  }  }  What is the result?() 

  • A、 s 14
  • B、 s 16
  • C、 s 10
  • D、 Compilation fails.
  • E、 An exception is thrown at runtime.

正确答案:D

第5题:

有一个接口定义如下,下列选项中实现了该接口并且不是抽象的是( )。interface A{ int method1 (int i); int method2 (int j);}

A.class B implements A{ int method1() { } int method2() { }}

B.class B{int method1(int i) { }int method2(int j) { }}

C.class B implements A{ int methodl(int i) { } int method2(intj) { }}

D.class B extends A{int method1(int i) { }int method2(int j) { }}


正确答案:C

第6题:

下列程序的输出结果是 interface Inter{ public final static int A=100; } class My implements Inter{ public static void main (String args[ ]) {System.out.println(A) ; }

A.100

B.0

C.A

D.程序有错误


正确答案:A
解析:本题主要考查接口的定义和使用,接口是一种含有抽象方法和常量的一种特殊的抽象类,不能包含成员变量,在程序中是输出常量A的值,所以输出的结果为5。

第7题:

已知类SubClass的getSum方法返回其父类成员i与类SubClass成员j的和;类SuperClass中的getSum为抽象函数,程序中的第14行有错误,请修改该错误并给出修改后的完整结果,然后完善程序中的空缺,当程序运行到第22行且尚未执行第22行语句时成员变量i的值,最后给出程序运行后的输出结果。

[Java代码]

行号 代码

01 public class UainJava{

02 public static void main(String[]args){

03 SuperClass s=new SubClass();

04 System.out.printin(s.getValue());

05 System.out.printIn(s.getSum()):

06 }

07 }

08 abstract class SuperClass {

09 private int i;

10 public SuperClass(){i=5;}

11 public int getValue(){

12 return i:

13 }

14 public final abstract int getSum():

15 }

16 class SubClass extends SuperClass{

17 int j;

18 public SubClass(){

19 this(-3);

20 }

21 public SubClass(int j){

22 (1) .j=j;

23 }

24 publiC int getValue()(return j;}

25 public int getSum(){

26 return (2) .getValue()+j;

27 }

28


正确答案:(1) this (2) super 错误更正结果为:public abstractint getSum(); 变量i的值为5 运行结果为: -3 2
(1) this (2) super 错误更正结果为:public abstract,int getSum(); 变量i的值为5 运行结果为: -3 2 解析:本题主要考察了Java程序语言中类成员变量的初始化、父类成员方法的调用、对象的构造等。(1)处要求用参数j的值更新数据成员,为避免与同名变量j冲突,应加this前缀,所以(1)处应为this,(2)处要求调用父类方法getValue(),但为了和子类方法相区别,应加前缀super,所以(2)处应为super。程序14行getSum函数是一个抽象函数,在SubClass子类中将被继承实现,所以该函数不可以被定义为final类型,因此应该去掉final关键字;当程序运行到22行之前时父类构造函数已执行,所以i值为5。最后程序输出的结果应为-3和2。

第8题:

下列哪个方法可用于创建一个可运行的类? ( )

A.public class X implements Runable {public void run(){...,.,}}

B.public class X implements Thread {public void run(){......}}

C.public class X implements Thread {public int run(){……}}

D.public class X implements Runable {protected void run(){.....}}


正确答案:A

第9题:

public class ClassA{ public int getValue(){ int value=0; boolean setting=true; String title="Hello"; if(value||(setting && title=="Hello")){return 1;} if(value==1&title.equals("Hello")){return 2;} } } And: ClassA a=new ClassA(); a.getValue(); What is the result?()

  • A、1
  • B、2
  • C、Compilation fails.
  • D、The code runs with no output.
  • E、An exception is thrown at runtime.

正确答案:C

第10题:

Which three demonstrate an “is a” relationship?() 

  • A、 public class X {  }     public class Y extends X { }
  • B、 public interface Shape { }     public interface Rectangle extends Shape{ }
  • C、 public interface Color { }     public class Shape { private Color color; }
  • D、 public interface Species { }     public class Animal { private Species species; }
  • E、 public class Person { }    public class Employee {      public Employee(Person person) { }
  • F、 interface Component { }     class Container implements Component {   private Component[] children; }

正确答案:A,B,F

更多相关问题