收藏 分销(赏)

C--程序设计复习题.doc

上传人:仙人****88 文档编号:8519792 上传时间:2025-02-16 格式:DOC 页数:12 大小:73.50KB 下载积分:10 金币
下载 相关 举报
C--程序设计复习题.doc_第1页
第1页 / 共12页
C--程序设计复习题.doc_第2页
第2页 / 共12页


点击查看更多>>
资源描述
2008—2009年度《C++程序设计》复习模拟试题 一、单选题 1、在每个C++程序中都必须包含有这样一个函数,该函数的函数名为    A       。 A. main           B. MAIN           C. name           D. function 2、设x和y均为bool量,则x&&y为真的条件是      A     。 A. 它们均为真    B. 其中一个为真    C. 它们均为假     D. 其中一个为假 3、下面的哪个保留字不能作为函数的返回类型?    C           。 A. void           B. int              C. new             D. long 4、假定a为一个整型数组名,则元素a[4]的字节地址为     C         。 A. a+4            B. a+8             C. a+16            D. a+32 5、假定AB为一个类,则执行“AB  a(4) , b[3] , * p[2] ;”语句时,自动调用该类构造函数的次数为     B            。 A. 3              B. 4               C. 6               D. 9 6、假定要对类AB定义加号操作符重载成员函数,实现两个AB类对象的加法,并返回相加结果,则该成员函数的声明语句为:    B             。 A.  AB  operator+(AB  & a , AB  & b)      B.  AB  operator+(AB  & a) C.  operator+(AB  a)                       D.  AB  & operator+( ) 二、填空题 1、C++语言中的每条基本语句以     ;        作为结束符,每条复合语句以       }     作为结束符。 2、执行“cout<<char('A'+2)<<endl;”语句后得到的输为     C            。 3、float 和 double 类型的大小分别为      4      和       8     。 4、关系表达式x+y>5的相反表达式为      x+y<=5                    。 5、假定一个一维数组的定义为“char  * a[8] ;”,则该数组所含元素的个数为    8      ,所占存储空间的字节数为     32       。(一个指针占4个字节) 6、变量分为全局和局部两种,      局部        变量没有赋初值时,其值是不确定的。 7、假定a是一个二维数组,则a[i][j]的指针访为   *(*(a+i)+j)                      。 8、假定一个结构类型定义为 “struct  D { int  a ;  union { int  b ; double  c ; } ; D  * d[2] ; } ;” , 则该类型的大小为       20       字节。 9、对一个类中的数据成员的初始化可以通过构造函数的     初始化表     实现,也可以通过构造函数中的  函数体           实现。 10、假定AB为一个类,则执行“AB  a[10];”语句时,系统自动调用该类的构造函数的次数为      10   。 11、假定类AB中有一个公用属性的静态数据成员bb,在类外不通过对象名访问该成员bb的写法为       AB::bb             。 12、在C++中建立参数类型和个数不同的同名函数是可能的,这称为_函数重载____。 13、运算符___new__动态分配一个对象。 14、关键字_enum____、_struct____、_union____和_class____在C++中用来建立新的数据类型。 15、类的_私有____成员只能被该类的成员函数或友元访问。 三、给出下列程序运行后的输出结果 1、 #include<iostream> using namespace std; class base{ int x; public: base(int a){x=a;cout<<"base..."<<x<<endl;} base(base &t){x=t.x;cout<<"base copy..."<<x<<endl;} virtual ~base(){cout<<"~base..."<<x<<endl;} }; class derived:public base{ int y; public: derived(int a,int b):base(a) { y=b;cout<<"derived..."<<y<<endl; } derived(derived &t):base(t){y=t.y;cout<<"derived..."<<y<<endl;} ~derived(){cout<<"~derived..."<<y<<endl;} }; void main() { base *pb=new derived(52,54); base a(*pb); delete pb; } 2、 #include<iostream> using namespace std; class A{ public: virtual void fa(void){cout<<"A::fa"<<endl;} void fb(void){cout<<"B::fb"<<endl;} }; class B:public A{ public: void fa(void){cout<<"B::fa"<<endl;} void fb(void){cout<<"B::fb"<<endl;} }; void main() { A *pa=new A; A *pb=new B; pa->fa(); pb->fb(); pb->fa(); pb->A::fa(); ((B*)pb)->fb(); } A::fa B::fb B::fa A::fa B::fb 3、 # include <iostream.h> void SB(char ch) {     switch(ch){     case 'A': case 'a':            cout <<"well!"; break;     case 'B': case 'b':            cout <<"good!"; break;     case 'C': case 'c':            cout <<"pass!"; break;     default:            cout <<"nad!"; break;     }} void main() {     char a1='b',a2='C',a3='f';     SB(a1);SB(a2);SB(a3);SB('A'); cout <<endl; } good!  pass!  bad!  well! 4、# include <iostream.h> # include <string.h> void main() {     char *a[5]={"student","worker","cadre","soldier","peasant"};     char *p1,*p2;     p1=p2=a[0];     for (int i=0; i<5; i++) {            if (strcmp(a[i],p1)>0) p1=a[i];            if (strcmp(a[i],p2)<0) p2=a[i];     }     cout <<p1<<' '<<p2<<endl; } 5、# include <iostream.h> int a=5; void main() {     int a=10,b=20;     cout <<a<<' '<<b<<endl;     {   int a=0,b=0;         for (int i=1; i<6; i++) {            a+=i; b+=a;     }            cout <<a<<' '<<b<<' '<<::a<<endl;     } cout <<a<<' '<<b<<endl; }   10  20 15  35  5 10  20 6、 # include <iomanip.h> int LB(int *a,int n) {     int s=1;     for (int i=0; i<n; i++)            s*=*a++;     return s; } void main() {     int a[]={1,2,3,4,5,6,7,8};     int b=LB(a,5)+LB(&a[3],3);     cout <<"b="<<b<<endl; } b=240 7、 # include <iostream.h> # include <string.h> struct Worker{     char name[15];   // 姓名     int age;         // 年龄     float pay;       // 工资 }; void main() {     Worker x;     char *t="liouting";     int d=38; float f=493;     strcpy(x.name,t);     x.age=d; x.pay=f;     cout <<x.name<<' '<<x.age<<' '<<x.pay<<endl; } 8、 # include <iostream.h> class A {     int a; public:     A(int aa=0) { a=aa; }     ~A() { cout <<"Destructor A!"<<a<<endl;  } }; class B:public A {     int b; public:     B(int aa=0,int bb=0):A(aa) { b=bb; }     ~B() { cout <<"Destructor B!"<<b<<endl; } }; void main() {     B x(5),y(6,7);  // 后定义的变量将先被释放 } Destructor  B!  7 Destructor  A!  6 Destructor  B!  0 Destructor  A!  5 四、写出下列每个函数的功能 1、# include <iostream.h> int SA(int a,int b) {     if (a>b) return 1;     else if (a==b) return 0;     else return -1; } 2、float FI(int n) {     // n为大于等于1的整数     float x,y=0;     do {            cin >>x;            n--; y+=x*x;     } while (n>0);     return  y; } 3、template <class Type> void WE(Type a[],Type b[],int n) {     for (int i=0; i<n; i++)            b[n-i-1]=a[i]; } 4、struct StrNode {     char name[15];     // 字符串域     StrNode * next;    // 指针域 }; void QB(StrNode * & f ,int n) {     if (n==0) { f=NULL; return; }     f=new StrNode;     cin >>f->name;     StrNode * p=f;     while (--n) {            p=p->next=new StrNode;            cin >>p->name;     }     p->next=NULL; } 五、编写程序,把从键盘上输入的一批整数(以-1作为终止输入的标志)保存到文本文件“a:xxk1.dat”中。 参考答案及评分标准 一、单选题(每小题1分,共6分) 评分标准:选对者得1分,否则不得分。 1、A       2、A        3、C        4、C       5、B       6、B 二、填空题(每小题2分,共24分) 评分标准:每题与参考答案相同者得2分,否则不得分。 1、   :    }                        2、   C 3、   4     8                        4、   x+y<=5                    5、   8      32     6、局部 7、  *(a[i]+j)   或  *(*(a+i)+j) 8、   20                            9、  初始化表    函数体 10、  10                            11、  AB::bb 12、函数重载 13、new 14、enum struct union class 15、私有 三、给出下列程序运行后的输出结果(每小题6分,共36分) 评分标准:每题与参考答案的数据和显示格式完全相同者得6分,否则酌情给分。 1、base...52 derived...54 base copy...52 ~derived...54 ~base...52 ~base...52 2、A::fa B::fb B::fa A::fa B::fb 3、  good!  pass!  bad!  well! 4、  worker  cadre 5、  10  20 15  35  5 10  20 6、  b=240 7、  liouting  38  493 8、  Destructor  B!  7 Destructor  A!  6 Destructor  B!  0 Destructor  A!  5 四、写出下列每个函数的功能(每小题6分,共24分)     评分标准:每题与参考答案的叙述含义相同者得6分,否则酌情给分。     1、比较两个整数a和b的大小,若a>b则返回1,若a= =b则返回0,若a<b则返回-1。     2、求出从键盘上输入的n个常数的平方和并返回。     3、模板函数,把数组a的每个元素按逆序放入数组b中。     4、建立一个具有n个结点的链表,每个结点的字符串值由键盘输入,链表的表头指针由引用变量f带回。 五、编写程序,把从键盘上输入的一批整数(以-1作为终止输入的标志)保存到文本文件“a:xxk1.dat”中。(10分) 评分标准:见参考程序中的注释。 # include <iostream.h>   // 使用此命令得1分 # include <fstream.h> # include <stdlib.h> void main() {        ofstream fout("a:xxk1.dat");  // 定义输出文件流打开文件2分        if (!fout){               cerr <<"文件没有打开!"<<endl;               exit(1);        }        // 可有可无        int x;        cin >>x;        while (x!=-1) {               fout <<x<<' ';               cin >>x;        }    // 能够从键盘向文件正确输出数据得6分        fout.close();   // 关闭输出文件流得1分 }
展开阅读全文

开通  VIP会员、SVIP会员  优惠大
下载10份以上建议开通VIP会员
下载20份以上建议开通SVIP会员


开通VIP      成为共赢上传

当前位置:首页 > 教育专区 > 小学其他

移动网页_全站_页脚广告1

关于我们      便捷服务       自信AI       AI导航        抽奖活动

©2010-2026 宁波自信网络信息技术有限公司  版权所有

客服电话:0574-28810668  投诉电话:18658249818

gongan.png浙公网安备33021202000488号   

icp.png浙ICP备2021020529号-1  |  浙B2-20240490  

关注我们 :微信公众号    抖音    微博    LOFTER 

客服