下面程序的输出结果是()。public class Sun{public static void main(String args[]){ int[] a={1

题目

下面程序的输出结果是( )。 public class Sun { public static void main(String args[]) { int[] a={1,2,3,4); int j=1,s=0; for(int i=3;i>=0;i--) { s=s+a[i]*j; j=j*10; } System.out.println(s); } }

A.1234

B.21

C.43

D.4321

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

第1题:

下列程序的运行结果是【 】。 include class SomeClass { public: SomeClass(int va

下列程序的运行结果是【 】。

include <iostream. h>

class SomeClass

{

public:

SomeClass(int value) { some_value=value;};

void show_data(void) { cout<<data<<"<<~some_value<<endl; };

static void set_data(int value) {data=value; }

private:

static int data;

int some_value

};

int SomeClass::data

void main(void)

{

SomeClass my_class(1001),your_class(2002);

your_class. set_data(4004);

my_elass. show_data()

}


正确答案:4004 1001
4004 1001 解析:本题考查静态成员变量在不同对象间的共享现象。无论哪个对象修改了其静态变量的值,其他对象再访问该变量时已经发生了变化。

第2题:

下面程序的输出结果是【】。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的构造函数。

第3题:

下面程序运行时输出结果为______。 include include class Rect { public: Rect(int

下面程序运行时输出结果为______。

include<iostream.h>

include

class Rect

{

public:

Rect(int l, int w){length=l; width=w;)

void Print(){cout<<"Area:"<<length*width<<end1;}

void operator delete(void*p){free(p);}

private:

int length, width;

};

void main()

{

Rect *p;

p=new Rect(5, 4);

p->Print();

delete p;

}


正确答案:Area:20
Area:20 解析:此题考查的是内存空间的分配和释放,即free和delete函数的应用。使用new对某种类型的变量进行动态分配的语法格式为:指针=new类型;使用delete对动态分配的变量进行释放是:delete指针。此题中,首先为变量分配内存单元,然后执行语句p->Print();输出Area:20,然后利用delete释放内存单元。

第4题:

阅读以下说明和Java程序,填写程序中的空(1)~(6),将解答写入答题纸的对应栏内。
【说明】
以下Java代码实现一个简单绘图工具,绘制不同形状以及不同颜色的图形。部分接口、类及其关系如图5-1所示。




【Java代码】
interface?DrawCircle?{? //绘制圆形 public(1) ;}class?RedCircle?implements?DrawCircle?{? ?//绘制红色圆形???????public?void?drawCircle(int?radius,intx,?int?y)??{????????????System.out.println("Drawing?Circle[red,radius:"?+?radius?+",x:"?+?x?+?",y:"?+y+?"]");???????}}class?GreenCircle?implements?DrawCircle?{????//绘制绿色圆形??????public?void?drawCircle(int?radius,?int?x,int?y)?{???????????System.out.println("Drawing?Circle[green,radius:"?+radius+",x:?"?+x+?",y:?"?+y+?"]");??????}}abstract?class?Shape?{????//形状? protected? ? (2)???;? ? public?Shape(DrawCircle?drawCircle)?{? ?this.drawCircle=?drawCircle;? ? ? public?abstract?void?draw();}class?Circle?extends?Shape?{? //圆形? ?private?int?x,y,radius;? public?Circle(int?x,int?y,intradius,DrawCircle?drawCircle)?{? ?(3)???;? this.x?=?x;? ? ? this.y?=?y;? ?this.radius?=radius;? }? ? ?public?void?draw()?{? ? drawCircle.? ?(4)? ?;? ? ? }}public?class?DrawCircleMain?{? public?static?void?main(String[]?args)?{? Shape?redCircle=new?Circle(?100,100,10,? (5) );//绘制红色圆形? Shape?greenCircle=new?Circle(200,200,10,(6) );//绘制绿色圆形? ?redCircle.draw(); greenCircle.draw();? ?}}


答案:
解析:
(1)void drawCircle (int radius,int x,int y)
(2)DrawCircle drawCircle
(3)super.drawcircle=drawcircle
(4)drawCircle(radius,x,y)
(5)new RedCircle()
(6)new GreenCircle()【解析】
第一空是填接口里面的方法,在接口的实现里面找,可以发现应该填void drawCircle (int radius,int x,int y)。
第二空可以根据后面this drawCircle=drawCircle判断,这里应该有一个drawCircle属性,因此应该填)DrawCircle drawCircle。
第三空这里用super,用super. drawcircle来引用父类的成员。
第四空调用drawCircle(radius,x,y)方法。
第五、六空分别创建一个红色圆形对象和一个绿色圆形对象作为Circle里面的实参。

第5题:

下面程序段中的错误语句是 ______。 class M{ int i; public: void ~AA(int); AA *p; void AA(); void AA(int x){i=x;}; };

A.AA *p;

B.void ~AA(int);

C.void AA(int);

D.void AA(int x){i=x;};


正确答案:B

第6题:

下面程序运行时输出结果为【】。 include include class Rect { public: Rec

下面程序运行时输出结果为【 】。

include<iostream.h>

include<malloc.h>

class Rect

{

public:

Rect(int1,int w)(length=1;width=w;)

void Print(){cout<<"Area:"<<length *width<<endl;)

void *operator new(size-t size){return malloc(size);}

void operator delete(void *p){free(p)

private:

int length,width;

};

void main()

{

Rect*p;

p=new Rect(5,4);

p->Print();

delete p;

}


正确答案:Area:20
Area:20

第7题:

对于下面程序,对p进行赋值正确的是( )。 class A{ public:fun(int i){cout<<i<<endl;} }; main(){ void(A::*p)(int); }

A.p=fun;

B.p=fun();

C.p=A::fun;

D.p=A::fun()


正确答案:C
解析:给类函数指针赋值的格式是:指针名>=类名::函数名>

第8题:

程序的输出结果是【 】。 include using namespace std; class A{ int x; public: A(int

程序的输出结果是【 】。

include <iostream>

using namespace std;

class A{

int x;

public:

A(int x=1):x(x){cout<<x;}

};

void main(){

A a,b(2),c(3);

}


正确答案:123
123 解析:a对象使用和默认的构造函数,b对象使用2来初始化对象c对象使用3来初始化对象,输出相应的值后,结果变为123。

第9题:

下面程序的输出结果是()。include using namespace std;class A {public:A( ) {cout<<

下面程序的输出结果是( )。 #include <iostream> using namespace std; class A { public: A( ) {cout<<"A";} } class B { public: B() {coat<<"B" ;} } class C: public A { public: B b; C() {cout<<"C";} } void mian(){ C c; }

A.CBA

B.ABC

C.ACB

D.BCA


正确答案:B
解析:先执行基类A构造函数输出A,调用类B的构造函数输出B,调用本身构造函数输出C。

第10题:

阅读下列说明和C++代码,填写程序中的空(1)~(6),将解答写入答题纸的对应栏内。
【说明】
以下C++代码实现一个简单绘图工具,绘制不同形状以及不同颜色的图形。部分类及其关系如图6-1所示。



【C++代码】
#include?#include?using?namespace?std;class?DrawCircle?{??????//绘制圆形,抽象类? ? ? public: (1);//定义参数为?int?radius,?int?x,?inty? ?virtual~DrawCircle()?{?}};class?RedCircle:public?DrawCircle?{????//绘制红色圆形? ? ? ? public: void?drawCircle(intradius,?int?x,?int?y)?{cout?<?drawCircle?=?drawCircle;? }? ?virtual~shape()?{?}? public:? ?virtual?void?draw()?=?0;};class?Circle:public?Shape?{????//圆形? ? private:? ? ?int?x,y,radius;? ? public:? Circle(int?x,inty,int?radius,DrawCircle?*drawCircle)? (3)? {? this->x?=?x;? ?this->y?=?y;? ? this->radius?=?radius; }? ? ? public:? void?draw(){? drawCircle?-> (4); }};int?main(){Shape?*redCirclenew?Circle(100,100,10,????(5)????);//绘制红色圆形? Shape?*greenCircle=new?Circle(100,100,10, (6)??);//绘制绿色圆形redCircle >draw();? ?greenCircle?->draw();? ?return?0;}


答案:
解析:
(6)(1)void drawCircle (int radius,int x,int y)
(2)DrawCircle*drawCircle
(3)drawcircle
(4)drawCircle(radius,x,y)
(5)new RedCircle()
(6)new GreenCircle()【解析】
第一空是填接口里面的方法,在接口的实现里面找,可以发现应该填void drawCircle (int radius,int x,int y)。
第二空可以根据后面this drawCircle=drawCircle判断,这里应该有一个drawCircle属性,因此应该填)DrawCircle drawCircle。
第三空这里填drawcircle,用-> drawcircle来引用父类的成员。
第四空调用drawCircle(radius,x,y)方法。
第五、六空分别创建一个红色圆形对象和一个绿色圆形对象作为Circle里面的实参。