您好,欢迎来到筏尚旅游网。
搜索
您的当前位置:首页成都元亨教育[面向对象程序设计]复习题

成都元亨教育[面向对象程序设计]复习题

来源:筏尚旅游网
面向对象程序设计复习题

一、选择题

1、基类中的保护成员在其私有派生类中的访问属性是( A )。 A.私有 B.公有 C.保护 D.不确定

2、在类的非静态成员函数中,不可以直接访问的数据成员是( D )。 A.本类的私有成员 B.本类的保护成员 C.本类的公有成员 D.本类的所有非静态成员 3、下列标示符中,不符合C++标示符命名规则的是( C )。 A.x10 B. _total C.π D.www

4、下列整型常数中,不符合C++语法的是( C )。 A.12u B.-67l C.059 D.0X11ff 5、下列常量中,不符合C++语法的是( D )。

A.”\\0” B.‘*’ C.“” D.“It’s an array!” 6、下列数组的初始化形式中,不符合C++语法的是( A )。 A.int s1[3]={1,2,3,4} B.int s2[3]={0} C.char s3[3]={‘a’,’x’,’d’} D.char s4[]=“asd” 7、如果说明语句为:int a[4]={10,20,30,40,50}, *p=&a[2];则*p的值是 ( B )。

A.a[2]的地址 B.30 C.20 D.不确定

8、已知:int a[3][3];下列表示数组元素a[0][0]的方法中,不符合C++语法的是( C )。

A.**a B.*a[0] C.a[0] D.*(*a[1]-1)

9、已知:int a=3,b=5,下列表达式中,值为0的表达式是( B )。 A.a%5-5 B.a/b C.!a||b D.a&&b 10、C++中字符常数的定界符是( B )。 A.” B.‘ C.# D.\\

11、已知:int a=9,sum=0;下列循环中,不构成死循环的是( A )。 A.for(;a;a++) sum+=a-- B.do{sum+=a--}while(--a); C.while(a=1) sum+=a--; D.do{sum+=a}while(a-1);

1 / 20

12、如果定义一个函数时未指定该函数的返回值类型,则函数返回值类型应是 ( B )。

A.无 B.int C.任意 D.void 13、下列描述中,属于传值调用的是( C )。

A.形参是指针,实参是地址 B.形参、实参都是数组

C.形参是变量,形参是常数 D.形参是指针,实参是数组名。 14、在类的友元函数中,可以访问的数据成员是( D )。 A.本类的私有成员 B.本类的保护成员 C.本类的公有成员 D.本类的所有成员

15、下列各种函数中,在对象生命期结束时自动调用的函数的是( B )。 A.构造函数 B.析构函数 C.友元函数 D.虚函数 16、下列描述中,属于析构函数的特征是( D )。 A.函数的名字与类名无关 B.函数可以重载

C.函数形参可有可无 D.不能指定函数返回值的类型 17、列对类的描述中,不符合C++语法的是( B )。 A.一个派生类至少有一个基类 B.一个派生类只能有一个基类 C.一个基类不可以有多个派生类 D.抽象类一定是另一个类的基类

18、C++中的虚函数可以实现的面向对象的基本特性是( D )。 A.封装性 B.继承性 C.抽象性 D.多态性 19、下列关于运算符重载的描述中,错误的是( D )。 A.运算符只能重载为成员函数 B.运算符只能重载为友元函数 C.有些运算符只能重载为成员函数 D.有些运算符只能重载为友元函数

20、有文件打开语句:ofstream fout(“data”);则下列描述中,正确的描述是( D )。

A.fout是一个标准输入流对象; B.fout是一个标准输出流对象;

2 / 20

C.fout是一个文件输入流对象; D.fout是一个文件输出流对象; 21、下列标示符中,符合C++标示符命名规则的是( A )。 A.Amax B.x(2) C.int D.姓名 22、下列常数中,不符合C++语法的是( C )。 A..5 B.‘7’ C.2e0.5 D.3.14f 23、下列字符常量中,不符合C++语法的是( B )。 A.’\\0’

B.‘\\’

C.‘3’

D.‘*’

24、以下指针用法中错误的是( B )。 A.int i; int *ptr=&i; C.int *ptr=0;

B.int *ptr=2000;

D.int i, *ptr=&i; *ptr=5;

25、下列描述中,不属于地址调用的是( D )。

A.形参、实参是指针 B.形参、实参都是数组

C.形参是指针,实参是数组名 D.形参是变量的引用,实参是变量 26、下列各种函数中,在对象生命期结束时自动调用的函数是( B )。 A.构造函数 B.析构函数 C.友元函数 D.虚函数 27、下列描述中,不属于虚函数的特征是( C )。

A.函数定义时不加virtual B.是类的非静态成员函数 C.函数没有形参 D.不能重载 28、C++中设置友元函数的目的是( B )。

A.解决继承中的二义性问题 B.提高编程效率 C.实现动态多态性 D.实现静态多态性 29、下列关于运算符重载的描述中,正确的是( D )。 A.运算符重载可以改变操作数的个数 B.运算符重载可以改变原有运算符的优先级 C.运算符重载可以改变运算符的结合性 D.运算符重载可以定义新的运算方法

30、用I/O流打开一个文件时,如果不指定文件存储方式,则该文件的格式是(B)。 A.二进制文件 B.文本文件 C.无格式 D.不确定 二、判断题(每题1分,共10分,对的打√,错的打×)

3 / 20

1、一个程序文件中,main函数有且只能有一个。 ( √ ) 2、程序中的注释是可执行的语句。 ( × ) 3、C++的表达式中不允许不同类型数据的混合运算。 ( × ) 4、系统函数不需要任何人定义,编程时可直接使用。 ( × ) 5、数组名可以看作一个指针变量。 ( × ) 6、不同函数间传递数组时,采用的是引用调用方式。 ( × ) 7、虚函数可以是类的静态成员。 ( × ) 8、抽象类只能做基类,但可以定义该类的对象。 ( × ) 9、文本文件只能保存字符数据,不能保存数值数据。 ( × ) 10、函数调用在前,定义在后时,需要原型声明。 ( √ ) 11、main函数总是程序中第一个被执行的函数。 ( √ ) 12、程序中的注释是写给编程人员看的。

( √ )

13、C++的表达式中允许不同类型数据的混合运算。 ( √ ) 14、函数在调用之前必须先定义或声明。 ( √ )

15、数组名中存放的是一个地址。

( √ )

16、不同函数间传递数组时,采用的是地址调用方式。 ( √ ) 17、基类的指针可以指向其派生类的对象。

( √ )

18、复制构造函数与构造函数的执行时机是一样的。

( × )

19、类中定义的成员函数都是内联函数。 ( √ ) 20、没有main函数的C++程序也可以执行。 ( × ) 21、程序中有没有注释不影响程序的执行。

( √ )

22、一个函数的函数体中不能包含另一个函数的定义。 ( √ )23、类的任何公有成员在类体外必须通过对象来访问。 ( × )24、一个指针变量指向数组元素时可以做++或—操作。 ( √ )25、具有块作用域的变量在块结束时,所占存储空间一定被释放。 (三、阅读程序写结果 1、#include void main(){ int s=0, k;

4 / 20

) × for(k=7; k>=0; k--){ switch(k){ case 1: case 4:

case 7: s+=k; break; case 2: case 3: case 6: break; case 0:

case 5: s++; break;

} }

cout<<”S=”<输出结果:答:S=14

2、写出以下程序运行时分别输入1、0、5时的输出结果。

#include void main() { int a,b=5;

cout<<”输入一个整数:”; cin>>a; switch(a>0)

{ case 1: switch(b<10)

{ case 0:cout<<″*″<default:cout<<″!\\n″; case 0:switch(b>0)

5 / 20

{ case 0:cout<<″ok1\\n″; case 1:cout<<″ok2\\n″; } } }

输出结果:答:

第一次运行: 输入一个整数:1

# ! ok2

第二次运行: 输入一个整数:0

ok2

第三次运行: 输入一个整数:5

# ! ok2

3、写出下列程序运行时输入9的运行结果

#include void main(){

int a[10]={23,4,78,9,9,37,92,47,9,17}, x, i, k= -1; cout<<”Input a number:”; cin>>x;

for(i=0; i<10; i++) if(a[i]!=x) a[++k]=a[i]; for(i=0; i<=k; i++) cout<6 / 20

输出结果:答:

23 4 78 37 92 47 17

4、#include void main() {int a[4],i,j,k;

for(i=0;i<4;i++) a[i]=0; k=3;

for(i=0;ia[j]=a[i]+2;

cout<输出结果:答:

10 12

5、#include long fac(int); void main() {int i;

for(i=1;i<=5;i++)

cout<long fac(int n) { static long f=1; f=f*n; return f; }

输出结果:答:

7 / 20

1 2 2 6 3 24 4 120 5 720

6、#include void swap(int &m1,int &m2) { m1*=2; m2+=2; } void main()

{ int a(10), b(20); cout<输出结果:答:

10 20 20 22

7、#include class myclass { private: int x,y;

static long sum;

public: myclass(int a,int b) { x=a;y=b;}

void getxy() { sum*=x*y;

cout<<″sum=″<};

8 / 20

long myclass::sum=1; void main()

{ myclass ob1(1,2); ob1·getxy(); myclass ob2(3,4); ob2·getxy(); myclass ob3(5,6); ob3·getxy(); }

输出结果:答:

sum=2 sum=24 sum=720

8、#include class cube { public:

cube(int ht=2,int wd=5,int dp=3) { height=ht; width=wd; depth=dp;

cout<<″depth=″<~cube() { cout<<″depth=″<int volume() {return height *width*depth;} private:

int height,width,depth; };

void main()

{ cube cone(10,20,15),ctwo; cout<9 / 20

cout<输出结果:答:

depth=15construncted called depth=3construncted called 3000 30

depth=3Destructed called depth=15Destructed called

9、#include class A{ public:

virtual void disp() {cout<<”A”<class B:public A{

void disp(){ cout<<”B”<void p1(A &a1) { a1.disp(); } void p2(A a2) { a2.disp(); } void main() { B b; p1(b); p2(b); }

输出结果:答:

10 / 20

B A

10、#include

void f(int); void main() {

for(int i(1); i<=4 ; i++) f(i);

}

void f(int j) {

static int a=10; int b=1; a+=10; b+=5;

cout<输出结果:答:

20+6=26 30+6=36 40+6=46 50+6=56

四、程序填空

1、以下的程序实现求分数序列2/1,3/2,5/3,8/5,13/8,21/13,……前15项的和。

#include #define N 15 void main( ){ int i, m, n, k;

11 / 20

double s=0 ; m=2, n=1;

for( i=1; i<=N; i++){ s=s+1.0*m/n; k=m;

m= m+n ; n= k ; }

cout<<”s=”<2、下面是利用运算符重载实现字符串加等的程序,请在空白处填入合适的内容。 #include #include class Cstring {char *s; public:

Cstring(char *s1=0){

s=new char[strlen(s1)+1];

strcpy(s,s1); }

___ Cstring operator+=(Cstring &s1);__ void list(){cout<Cstring Cstring::operator+=(Cstring &s1) {

char *t=new char[strlen(s)+strlen(s1.s)+1]; strcpy(t,s); strcat(t,s1.s); delete s; s=t;

__ return *this;__ }

void main() {

12 / 20

Cstring cstr1(\"abcd\"),cstr2(\"1234\"); __ cstr1+=cstr2;__ cstr1.list(); }

3、下面程序将输入到数组a中的数据重新按逆序存放,然后将逆序存放后的数据输出。

#include const int N 10; void main()

{ int a[N],*p=a,*q,r;

cout<<”输入10个整数:”;

for (int i=0; i>a[I++]; p=a; q=&a[9];

while( _____ pp++;

_____ q-- __ ; }

cout<<” 逆序存放结果:”; p= a ;

for(i=0; i4、、输入一串有字母和数字混合的字符串,编程序将字符串中的字母和数字分开,分别存入两个不同的文件。

# include

# include # include void main() {

char ch[100];

cout<<\"请输入一串有字符和数字的字符串:\"<13 / 20

cout<<\"文件打开失败! \"<if(!fout2) { } cin>>ch; int i=0; do{

if( '0'<=ch[i]&&ch[i]<='9' )

fout1<fout2<if(('a'<=ch[i]&&ch[i]<='z')||('A'<=ch[i]&&ch[i]<='Z')) i++;

cout<<\"文件打开失败! \"<}while(ch[i]!=0); fout1.close(); fout2.close();

}

5、下面的程序输出小于200的素数,而且每行最多输出10个素数。

# include

# include class Prime{ int p; public:

Prime(int n){if (n<3) p=3;else p=n;} void Run(); };

void Prime::Run() {

int k,j,flag,line=0; for(j=2;jflag=1;

for (k=2;kif(j%k==0)

flag=0 ;

if(flag)

14 / 20

{

cout<if( line%10==0 )

cout<} }

cout<void main() {

Prime obj(200) ; obj.Run(); }

6、下面是将一个一维数组及各元素乘积写入文本文件data.txt的程序,请在空白处填入合适的内容。

___#include __ #include void main(){

int a[10]={1,2,3,4,5,6,7,8,9,10},cum=0; __ ofstream fout(\"data.txt\"); _ if(!fout){ //打开文件失败

cout<<\"Cannot open the file!\"<for(i=0;i<10;i++){ cum*=a[i];

__ fout<_ fout<}

7、以下程序用友元函数实现复数加和复数赋制运算,完成程序。

15 / 20

#include class complex{ private:

double real,image; public:

complex( ){real=0; image=0; }

complex(double r , double i ){real=r; image=i; }

friend complex operator+(complex &a,const

complex &b);

complex &operator=(complex &c); void print();

};

complex operator + (complex &a,const complex &b) {

return complex(_ a.real+b.real, a.image+b.image _); }

complex &complex::operator = (complex &c) {

real=c.real; imag=c.imag; return *this; }

void complex::print() {

cout<void main()

{ complex c1,c2(1.5,4.0),c3(6.5, -2.8); complex c4=c2+c3; c1=c4;

c1.print(); c4.print(); } 8、下面是利用运算符重载实现字符串赋值(=)的程序,请在空白处填入合适的内容。

#include #include class Cstring {char *s; public:

Cstring(char *s1=0){ s=new char[strlen(s1)+1];

16 / 20

};

strcpy(s,s1); }

__ Cstring operator=(Cstring &s1);__ void list(){cout<Cstring Cstring::operator=(Cstring &s1) { }

void main() {

Cstring cstr1(\"abcd\"),cstr2(\"1234\"); ____ cstr1=cstr2;____ cstr1.list(); delete s;

s=new char[strlen(s1.s)+1]; strcpy(s,s1.s); __ return *this;_

}

五、编程题

1、从由键盘输入的若干整数中找出其中最大的数并输出。当输入为负数时结束输入。

答:#include

void main( ) {

int a, smax; cin>>a; smax=a;

while(a>=0){

smax= a>smax?a:smax; cin>>a; }

cout<<”smax=”<17 / 20

2、键盘输入10个整数,计算其和并输出到屏幕上。 答:#include

void main() {

int x, sum=0; for(int i=0;i<10;i++) { cin>>x; sum+=x; }

cout<3、编写求n个整数中最大值的函数,函数原型为:int maxi(int x[],int n);并编写主函数,调用该函数求键盘输入的十个整数之和,并在屏幕上输出。 答:#include

int maxi(int x[],int n) {

int ma=x[0]; for(int i=1;ima=x[i];

return ma;

}

void main() {

int a[10]; for(int i=0;i<10;i++) cin>>a[i];

cout<<”The max value is:”<4、编写一个函数void delchar(char*s1,char s2),从字符串s1中删去与字符s2相同的字符,并用main ()测试该函数。 答:#include

#include

void delchar(char *s1,char s2) {

18 / 20

}

void main() {

char str[50];char s; cout<<\"请输入一串字符:\"; cin>>str; cout<<\"请输入一个字符:\"; cin>>s; delchar(str, s); }

5、将键盘输入的一串字符中的大写字母挑出并输出到屏幕上。说明:字符串中不含空格,长度不超过40个字符。 答:

#include void main() {

char str[40]; cout<<\"输入字符串:\";

cin>>str;

for(int i=0;str[i]!='\\0';i++) if(str[i]>='A'&&str[i]<='Z') cout<6、编写求n个整数之和的函数,函数原型为:int sum(int x[],int n);并编写主函数,调用该函数求5×5矩阵的主对角线元素之和及辅对角线上元素之和,并在屏幕上输出。矩阵数据由键盘输入。 答:

#include int sum(int a[],int n) {

19 / 20

int N=strlen(s1),i,j; for (i=0,j=0;is1[j++]=s1[i]; for(;jcout<int s=0; for(int i=0;ivoid main() {

int array[5][5],t1[5],t2[5],i,j; for(i=0;i<5;i++) {

for(j=0;j<5;j++) cin>>array[i][j];

t1[i]=array[i][i]; t2[i]=array[i][5-i-1]; }

cout<<\"主对角元素和是:\"<详情地址:成都市武侯区科华北路153宏地大厦14A

20 / 20

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- efsc.cn 版权所有 赣ICP备2024042792号-1

违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务