class A {  public byte getNumber() {  return 1;  }  }  class

题目
单选题
class A {  public byte getNumber() {  return 1;  }  }  class B extends A {  public short getNumber() {  return 2;  }  public static void main(String args[]) {  B b = new B();  System.out.println(b.getNumber()); }  }   What is the result?()
A

 1

B

 2

C

 An exception is thrown at runtime.

D

 Compilation fails because of an error in line 8.

E

 Compilation fails because of an error in line 14.

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

第1题:

下面程序的输出结果是【】。include using namespace std; class base { protected: int

下面程序的输出结果是【 】。

include <iostream>

using namespace std;

class base

{

protected:

int a;

public:

base(){cout<<"0":}

};

class basel: virtual public base

{

public:

base1(){ cout<<"1";}

};

class base2 : virtual public base

{

public:

base2(){cout<<"2";}

};

class derived : public base1,public base2

{

public:

derived () {cout<<"3"; }

}

int main ()

{

derived obj;

cout<<end1;

return 0;

}


正确答案:0123
0123 解析:本题考核含有虚基类的继承中构造函数的调用顺序,应该先调用基类的构造函数,接着是按照派生类继承列表的顺序依次调用虚基类的构造函数,最有调用派生类自己的构造函数.题中先调用base的构造函数,然后调用base1、base2的构造函数,最后调用derived的构造函数。

第2题:

若有以下程序:include using namespace std;class A{protected: int a;public: A() {

若有以下程序: #include <iostream> using namespace std; class A { protected: int a; public: A() { a=10; } }; class A1 : public A { public: A1() { a=a+1; } }; class A2 : public A { public: A2 () { a=a+2; } }; class B : public A1,public A2 { public: B(){} void print() { cout<<a<<end1; } }; int main ( ) { B obj; obj.print(); return 0; } 程序运行后的输出结果是( )。

A.产生语法错误

B.11

C.12

D.10


正确答案:A
解析:本题考核派生类的定义和使用。本程序有语法错误,这是由于类B中的print函数中的语句“coutaend1;”要输出成员a的值,从而产生了二义性。分析如下:程序中定义了类A,类A1和类A2都是在类A的基础上以公有继承方式产生的派生类,而类B是在类A1和类A2的基础上经过多重继承方式产生的派生类,所以在类B中成员a有两个拷贝,系统无法区分是从A1继承过来的a,还从A2类继承过来的a。修改方法有以下两种:①在被访问的成员加上作用域分辨符“::”。将类B中的print()函数体中语句改为:coutA1::aend1;或coutA2::aend1;但两者的输出结果不同,前者输出11,而后者输出12。②采用虚基类的方法。就是在定义A1和A2时,在派生方式前加上关键词“virtual”。

第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 X {  public X aMethod() { return this;}  }  public class Y extends X {  }  Which two methods can be added to the definition of class Y?()

  • A、 public void aMethod() {}
  • B、 private void aMethod() {}
  • C、 public void aMethod(String s) {}
  • D、 private Y aMethod() { return null; }
  • E、 public X aMethod() { return new Y(); }

正确答案:C,E

第5题:

class A {  protected int method1(int a, int b) { return 0; }  }  Which two are valid in a class that extends class A?() 

  • A、 public int method1(int a, int b) { return 0; }
  • B、 private int method1(int a, int b) { return 0; }
  • C、 private int method1(int a, long b) { return 0; }
  • D、 public short method1(int a, int b) { return 0: }
  • E、 static protected int method1(int a, int b) { return 0; }

正确答案:A,C

第6题:

若有以下程序: include using namespace std; class Base { public:void who(){ cout

若有以下程序:

include <iostream>

using namespace std;

class Base

{

public:

void who()

{

cout<<"class Base"<<end1;

}

};

class Derivedl : public Base

{

public:

void who()

{

cout<<"class Derivedl"<<end1;

}

};

class Derived2 : public Base

{

public:

void who()

{

cout<<"class Derived2"<<end1;

}

};

int main()

{

Base *p;

Derivedl obj1;

Derived2 obi2;

p=&obj 1;

p=&obj2;

p->who ( );

return 0;

}

则该程序运行后的输出结果是【 】。


正确答案:class Derived2
class Derived2 解析:本题考核对象指针的应用。主函数中定义了一个Base类对象指针p,然后逐步被赋值为obj1和obj2,最后通过对象指针p调用函数who(),也即调用Derived2中的函数who(),输出class Derived2。

第7题:

有如下程序: include class x { protected: int a; public:x(){ a=1;} }; class x

有如下程序: #include <iostream.h> class x { protected: int a; public: x() { a=1; } }; class x1 : virtual public x { public: x1() { a+=1; cout<<a; } }; class x2 : virtual public x { public: x2() { a+=2; cout<<a; } }; class y : public xl,public x2 { public: y() { cout<<a<<end1; } }; int main() { y obj; return O; } 该程序运行后的输出结果是( )。

A.1

B.123

C.242

D.244


正确答案:D
解析:本题程序中引入了虚基类。在主函数main中,执行语句“yobj;”时,先执行虚基类x的构造函数,使a=1;然后执行类x1的构造函数,使a=2,并输出值2;再执行类x2的构造函数,使a=4,并输出值4;最后执行类y的构造函数,输出值4。

第8题:

Given:Which code, inserted at line 15, allows the class Sprite to compile?()

A.Foo { public int bar() { return 1; }

B.new Foo { public int bar() { return 1; }

C.new Foo() { public int bar() { return 1; }

D.new class Foo { public int bar() { return 1; }


参考答案:C

第9题:

1. public class ReturnIt {  2. return Type methodA(byte x, double y) {  3. return (long)x / y * 2;  4. }  5. }  What is the narrowest valid returnType for methodA in line2?()  

  • A、 int
  • B、 byte
  • C、 long
  • D、 short
  • E、 float
  • F、 double

正确答案:F

第10题:

public class OuterClass {  private double d1  1.0;  //insert code here   }   You need to insert an inner class declaration at line2. Which two inner class declarations are valid?() 

  • A、 static class InnerOne {  public double methoda() {return d1;}  }
  • B、 static class InnerOne {  static double methoda() {return d1;} }
  • C、 private class InnerOne {  public double methoda() {return d1;} }
  • D、 protected class InnerOne {  static double methoda() {return d1;} }
  • E、 public abstract class InnerOne {  public abstract double methoda();  }

正确答案:C,E

更多相关问题