计算机二级

下列程序段的执行结果为()。includeusing namespace std;class example{int n;public:下列程序段的执行结果为( )。 #include<iostream> using namespace std; class example{ int n; public: example(int i){n=i;} void add(){s+=n;} static int s; void pr(){ cout<<s<<endl; } }; int example::s=0;

题目
下列程序段的执行结果为()。includeusing namespace std;class example{int n;public:

下列程序段的执行结果为( )。 #include<iostream> using namespace std; class example{ int n; public: example(int i){n=i;} void add(){s+=n;} static int s; void pr(){ cout<<s<<endl; } }; int example::s=0; int fuc(char *x); int main(){ example x(2),y(3),z(4); x. add(); y. add(); z.pr(); return 0; }

A.2

B.3

C.5

D.6

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

第1题:

有如下程序: include using namespace std; class Base { public:

有如下程序: #include <iostream> using namespace std; class Base { public: Base(int x=0) { cout<<x; } } class Derived: public Base{ public: Derived(int x=0) { cout<<x; } private: Base val; }; int main() { Derived d(1); return 0; }程序的输出结果是

A.0

B.1

C.1

D.1


正确答案:D
解析:本题考查的知识点是:类的构造。建立一个类的对象时,构造函数的执行顺序如下:
①执行基类的构造函数,调用顺序按照各个基类被继承时声明的顺序(自左向右);
②执行成员对象的构造函数,调用顺序按照各个成员对象在类中声明的顺序(自上而下):(如果一行声明了多个对象,则还要遵循自左向右)
③执行自身的构造函数。
本题Derived类继承于Base类,所以首先会构造基类Base,但Derived类的构造函数没有初始化列表,所以将调用Base类的默认构造函数,输出一个0。接下来由于它的成员中还定义了一个Base类的对象,而构造函数也没有显示初始化这个对象,所以再次调用Base类的默认构造函数输出一个0。最后构造自身,因为主函数中传入了构造参数1,所以构造自身时输出了一个1。故最终输出结果为001,应该选择 D。

第2题:

以下程序的执行结果为______。include using namespace std; class base { public: vir

以下程序的执行结果为______。

include<iostream>

using namespace std;

class base

public:

virtual void who()

cout<<"base class"<<endl;

};

class derivel:public base

public:

void who()

cout<<"d


正确答案:base class derivel class derive2 class
base class derivel class derive2 class

第3题:

下面程序运行后输出的结果是【】。 include using namespace std; class example{ const

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

include <iostream>

using namespace std;

class example{

const int m;

public:

example(int i):m(i){}

void pr(){cout<<"m="<<m<<endl'}

};

int main(){

example x(100);

x.pr();

return 0;

}


正确答案:m=100
m=100 解析:在类example中,定义了一个常数据成员m,所以构造函数只能通过初始化列表来初始化它。

第4题:

有如下程序: include using namespace std; class BASE { public

有如下程序: #include<iostream> using namespace std; class BASE { public: ~BASE(){cout<<"BASE";} }; class DERIVED:public BASE { public: ~DERIVED(){cout<<"DERIVED";} }; int main(){DERIVEDx;retum 0;} 执行后的输出结果是

A.BASE

B.DERIVED

C.BASEDERIVED

D.DERIVEDBASE


正确答案:D
解析:本题考查基类析构函数和派生类析构函数的调用次序。

第5题:

有如下程序: include using namespace std; class Test { public

有如下程序: #include<iostream> using namespace std; class Test { public: Test(){n+=2;} ~Test(){n-=3;} static int getNum(){return n;} private: static int n; }; int Tesl::n=1 int main() { Test*p=new Test; delete p; cout<<"n="<<Tes::tgetNum()<<endl; return 0; } 执行后的输出结果是

A.n=0

B.n=1

C.n=2

D.n=3


正确答案:A
解析:本题考查构造函数和析构函数的调用。类的静态成员和成员函数是类属,不依赖于对象实例存在。

第6题:

程序的输出结果是【 】。 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。

第7题:

程序的输出结果是【 】。 include using namespace std; class A{ public: A(){a=b=2;}

程序的输出结果是【 】。

include <iostream>

using namespace std;

class A{

public:

A(){a=b=2;}

A(int i,int j){a=i;b=j;}

void display(){cout<<a<<b;}

private:

int a,b;

};

void main(){

A m,n(4,8);

m.display();

n.display();

}


正确答案:2248
2248 解析:m对象使用和默认的构造函数,其a与b变量的值均为2;而n变量使用4和8来初始化程序的变量,a,b的值为4和8,依次输出的结果为2248。

第8题:

有如下程序: include using namespace std; class Sample{ public:

有如下程序: #include<iostream> using namespace std; class Sample{ public: Sample()<) ~Sample(){cout<<'*';} }; int main(){ Sample temp[2],*pTemp[2]; return 0; } 执行这个程序输出星号(*)的个数为( )。

A.1

B.2

C.3

D.4


正确答案:B
解析:此题考查的是构造函数和析构函数。构造函数在对象被创建时由系统自动调用,而析构函数在对象的生存期即将结束时由系统自动调用。此题中,主程序在创建Sample类的对象temp[2]数组时,调用Sample类的默认构造函数,在主函数退出时,调用析构函数清除对象temp[2],输出字母*,因为对象是包含两个元素的数组,所以调用两次析构函数,即输出两个*。

第9题:

有如下程序: include using namespace std; class A { public:

有如下程序: #include<iostream> using namespace std; class A { public: A(){cout<<"A";} }; class B<public:B(){cout<<"B";)); class C:public A { B b; public: C(){cout<<"C";} }; int main(){Cobj;retum 0;} 执行后的输出结果是

A.CBA

B.BAC

C.ACB

D.ABC


正确答案:D
解析:本题考查基类构造函数,数据成员构造函数的调用次序。系统首先要通过派生类的构造函数调用基类的构造函数,对基类成员初始化:然后对派生类中新增的成员初始化。

第10题:

有如下程序: include using namespace std; class AA { int n;

有如下程序: #include<iostream> using namespace std; class AA { int n; public: AA(int k);n(k){} intget(){return n;} int get()const{return n+1;} }; int main() { AA a(5); const AA b(6); cout<<a.get()<<b.get(); return 0; } 执行后的输出结果是

A.55

B.57

C.75

D.77


正确答案:B
解析:本题考查const对象。一个const对象只允许被声明为const的方法函数引用。

更多相关问题