收藏 分销(赏)

2023年秋江苏省等级考试C++试卷附答案.docx

上传人:二*** 文档编号:4510118 上传时间:2024-09-26 格式:DOCX 页数:20 大小:26.30KB 下载积分:5 金币
下载 相关 举报
2023年秋江苏省等级考试C++试卷附答案.docx_第1页
第1页 / 共20页
本文档共20页,全文阅读请下载到手机保存,查看更方便
资源描述
2023年秋江苏省等级考试C++试卷 (笔试题,共60分) 第一部分部分 计算机信息基础 (20道选择题,共20分,略) 第二部分 C++语言程序设计(共430分) 一、 选择题(用答题卡答题,答案依次填在21~30答题号内) 21. 以下不符合C++语法规则的数值常量是 d 。 A.034 B.2.1E3 C.0Xab23 D.2E1.4 22. 表达式:3.6-5/2+1.2+5%2的值是 c 。 A.4.3 B.4.8 C.3.8 D.3.3 23. 下列关于虚函数的叙述中,对的的是 c 。 A. 虚函数可以是一个static类型的成员函数 B. 基类中用virtual说明一个虚函数后,派生类中定义相同原型的函数时可不必加virtual说明 C. 派生类中的虚函数与基类中相同原型的虚函数具有不同的参数个数或类型 D. 虚函数可以是非成员函数 24. 下列关于缺省构造函数的叙述中,不对的的是 a 。 A.每个类至少有一个缺省构造函数 B.缺省构造函数没有参数或每一个参数都有缺省值 C.缺省构造函数是唯一的 D.每个类至少有两个构造函数 25. 在循环体中,使用break和continue语句的作用分别是 d 。 A.两语句都结束该层循环 B.前者结束该层的本次循环,后者结束该层循环 C.两语句都结束该层 的本次循环 D.前者结束该层循环,后者结束该层的本次循环 26. 以下叙述中,不对的的是 d 。 A.派生类可以继承多个基类 B.可以有多个派生类继承同一个基类 C.派生类可以有多个虚基类 D.抽象类必须是虚基类 27. 设由类的成员函数和友元函数分别实现相同的运算符重载功能,以下叙述中对的的是 b 。 A.两种函数的参数个数相同 B.友元函数比成员函数多一个参数 C.成员函数比友元函数多一个参数 D.两种函数都带有this指针 28. 以下对函数重载的叙述中,对的的是 c 。 A.函数名不同,但函数实现的功能相同 B.函数名相同,函数的参数个数相同但参数的类型不同 C.函数名相同,但函数的参数个数不同或参数的类型不同 D.函数名相同,但函数的参数类型不同或函数的返回值的类型不同 29. 以下关于类成员的叙述中, 不对的的是 a 。 A.类的析构函数可以重载 B.类的构造函数可以重载 C.类中的成员都有明确的访问权限 D.可将成员函数定义为静态的 30. 以下的叙述中, 不对的的是 c 。 A.在不同函数中可以使用相同名字的变量 B.函数的形式参数是局部变量 C.在函数内的复合语句中定义的变量在本函数范围内有效 D.在函数内定义的变量只在本函数范围内有效 二、 填空题(请将答案填写在答题纸的相应答题号内,每个答案只占一行) ●基本概念题,共5分 1. 在C++中,函数的参数传递方式有三种:第一种是值传递,第二种是 指针传递 ,第三种是 引用传递 。 2. 符号“&”作为单目运算符时的功能是取地址;作为双目运算符时,其功能是 按位与 。 3. 设有以下语句: #define S(x) x*x int k=3,y; y= S(k+k); 执行赋值语句:“y= S(k+k);”,则y的值为 15 。 4. 假如派生类没有重载基类的纯虚函数时,则该派生类也是 抽象 类。 ●阅读程序题,共13分 5. [程序] (2分) #include <iostream.h> void f1(void) { int x=10; static int y=10; x+=y; y+=x; cout<<x<<’\t’<<y<<endl; } void main(void) { f1( ); f1 ( ); f1 ( ); } 执行程序,输出的第二行是 40 ,第三行是 70 。 6. [程序] (2分) #include <iostream.h> int x=100; void main(void) { int x=30; x+=::x++; { int x=60; ::x+=x; } cout<<++x<<'\n'; cout<<::x<<'\n'; } 执行程序,输出的第一行是 131 ,第二行是 161 。 7. [程序] (3分) #include <iostream.h> int fact(int n) { if (n>1) return n*fact(n-1); return 1; } int f (int a[ ], int n) { if (n>=2) return f(a,n-1) + a[n-1] ; return a[0] ; } void exchange(int a[], int n) { int i, temp ; for (i=0; i<n/2; i++){ temp=a[i]; a[i]=a[n-i-1]; a[n-i-1]=temp; } } void main(void) { int aa[5] = {1, 2, 3, 4}; cout<< f(aa,4) <<endl; cout<<fact(aa[3])<<endl; exchange (aa, 4); for (int i=0; i<4; i++) cout<<aa[i]<<'\t'; cout<<endl; } 执行程序,输出的第一行是 10 ,第二行是 24 ,第三行是 4 3 2 1 。 8. [程序] (3分) #include <iostream.h> class A{ int x; public: A(int i) { x=i; cout<<"A: x="<<x<<endl; } void print( ) { cout<<"x="<<x<< " "; } }; class B: public A{ int y; A a; public: B(int i, int j): A(i+j), a(j+10) { y=i*j; cout<<"B: y="<<y<<endl; } void print( ) { A::print( ); a.print( ); cout<<"y="<<y<<endl; } }; void main(void) { B b(5,10); b.print( ); } 执行程序,输出的第一行是 A:x=15 ,第三行是 B:y=50 ,第四行是 x=15,x=20,y=50 。 9. [程序] (3分) #include <iostream.h> const int SIZE=100; class Stack{ char stck[SIZE]; int top; public: Stack( ) { top=0; } virtual void push(char ch) { if (top== SIZE) return; if('a'<=ch && ch<='z') { stck[top]=ch; top++; } } virtual char pop( ) { if(top<0) return NULL; top--; return stck[top]; } void print( ) { for (int i=top-1; i>=0;i--) cout<<stck[i]; cout<<endl; } }; class Stack2: public Stack { char stck[SIZE]; int top; public: Stack2( ) { top=0; } void push(char ch) { if (top== SIZE) return; if('0'<=ch && ch<='9') { stck[top]=ch; top++; } } char pop( ) { if(top<0) return NULL; top--; return stck[top]; } void print( ) { for (int i=0; i<top;i++) cout<<stck[i];cout<<endl; } }; void main(void ) { char ch,*ptr; Stack s1,*p; Stack2 s2; char *str1 = "a1b2c3D4",*str2= "nangjin5678"; ptr=str1; p=&s1; while(*ptr) p->push(*ptr++); ptr=str2; p=&s2; while(*ptr) p->push(*ptr++); s1.print( ); s2.print( ); while(ch=p->pop( )) cout<<ch; cout<<endl; } 执行程序,输出的第一行是 cba ,第二行是 5678 ,第三行是 8765 。 ●完善程序题,共12分 10. 以下函数sortdel(char *s)的功能是:一方面将s所指向的字符串中的字符按照字符ASII码值的大小按升序排序,然后在排序后的字符串中删除反复的字符。在主函数中输入一个字符串,调用函数sortdel( ),输出排序后的字符串。 [程序] (4分) #include <iostream.h> void sortdel(char *s) { char *p,*q,*r,c; for(p=s; *p; p++) { // 选择法排序 for(r=p,q=p+1; *q; q++) if(*r>*q) r=q ; if(r!=p) { c=*r; *r=*p; *p==c ; } } for(p=s; *p; p++){ // 删除反复的字符 r=p+1; while( *p==*r ) r++; if(p != r ) for(q=p+1; *r; q++) { *q=*r ; r++; } *q='\0'; } } void main(void) { char str[200]; cin.getline(str,199); cout<<str<<endl; sortdel(str); cout<<str<<endl; } 11. 以下程序实现一个由数组构成的线性表类,动态产生线性表,表中存放若干实数。为了简化,本程序仅给出此线性表类的重要成员函数。程序中,重载赋值运算完毕线性表对象间的赋值;重载插入运算符“>>”完毕数组的输入;成员函数add( )实现将一个实数加入线性表。 [程序] (4分) #include <iostream.h> class List{ public: List(int=1); ~List( ){ delete []list; } List& operator =(const List&); void print( ) { for(int i=0;i<num;i++){ cout<<list[i]<<','; if((i+1)%5==0)cout<<'\n'; } } friend istream& operator >>(istream&, List&); void add(double x) { if( num==size){ double *list1= new double[size+1]; for(int i=0;i<size;i++)list1[i]=list[i]; delete [ ] list; list=list 1 ; size++; } list[num]=x; num++; } private: int size,num; double *list; }; List::List(int sz) { size=sz; list= new double[size] ; num=0; } List& List::operator =(const List& v) { if(&v!=this){ delete []list; size=v.size; list=new double[v.size]或double[size] ; for(int i=0;i<num; i++) list[i]=v.list[i]; } return *this; } Iostream&operator>> (istream& is, List& v) { for(int i=0;i<v.size;i++){ cout<<i<<":"; is>>v.list[i] ; } v.num=v.size; return is ; } void main(void) { List a(10),b(14); cout<<"Input List b:\n"; cin>>b; b.print( ); a=b; a.add(37.25); a.print( ); } 12. 以下程序的功能是:先创建两条带有空头结点(链表的第一个结点不存储数据,只是为了方便解决)的有序单向链表(结点元素值按升序排列),假设每条链表中的元素均不相同。然后将两条具有相同顺序的单链表归并成一条有序单链表,并且规定相同元素只归并一次。函数Create( )运用给定的参数数组(数组中元素不规定有序)创建一条具有空头结点的有序单链表,函数Merge( )把两条有序链表归并成一条有序链表,原先的两条链表保持不变。 [程序] (4分) #include <iostream.h> struct Node { int data; Node *next; }; void Create(Node *&head,int *a,int n) { //根据数组a中n个数据产生一条有序链表 Node *L, *t, *p, *q; //p指向待解决结点,q指向p的前驱结点 head=new Node; L=head->next=NULL; //L总是指向第一个结点(头结点的下一个结点) for (int i=0;i<n;i++) { t=new(Node); t->data=a[i]; t->next=NULL; if (L==NULL || a[i]<L->data) { //若链表为空或a[i]小于第一个结点的值 t->next=L; head->next=t; L=t ; } else { q=L; p=q->next; while (p!=NULL && a[i]>p->data) { //找出新结点t的插入点 q=p; p=p->next ; } t->next=p; q->next=t; } } } void Print(Node *h) { h=h->next; while (h!=NULL) { cout<<h->data<<" "; h=h->next; } cout<<endl; } Node *Merge(Node *h1,Node *h2) { Node *head,*p,*q,*t,*s; head=new Node;head->next=NULL; t=head; p=h1->next; q=h2->next; while (p!=NULL && q!=NULL) { s=new Node; if (p->data<q->data) { s->data=p->data; p=p->next; } else if (p->data>q->data) { s->data=q->data ; q=q->next; } else { s->data=p->data; p=p->next ; q=q->next; } t->next=s; t=s; } if (q!=NULL) p=q; while (p!=NULL) { s=new Node; s->data=p->data; p=p->next; t->next=s; t=s; } t->next=NULL; return head; } void main(void) { int a[ ]={12,1,8,2,5,11,3,6,9,7,10,16,4}; int b[ ]={0,12,8,2,5,18,13,16,9,17,3,6}; int m=sizeof(a)/sizeof(int), n=sizeof(b)/sizeof(int); Node *h1,*h2,*h3; Create(h1,a,m); Create(h2,b,n); cout<<"有序表h1为:"; Print(h1); cout<<"有序表h2为:"; Print(h2); h3=Merge(h1,h2); cout<<"两表归并后:"; Print(h3); } 参考答案 一、选择题 21.D 22.C 23.B 24.A 25. D 26. D 27. B 28. C 29. A 30.C 二、填空题 1. 指针(地址)传递 2. 引用传递 注:1和2可互换 3. 按位与 4. 15 5. 抽象(类) 6. 40 70 7. 80 150 8. 131 9. 161 10. 10 11. 24 12. 4 3 2 1 13. A: x=15 14. B: y=50 15. x=15 x=20 y=50 16. cba 17. 5678 18. 8765 19. r=q 20. *p=c 21. *p == *r 22. *q=*r 23. list =list1 24. list=new double[v.size]或 double[size] 25. istream& operator >> 26. return is 27. L=t 28. p=p->next 29. s->data=q->data 30. p=p->next
展开阅读全文

开通  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 

客服