资源描述
一、单选题
1、在每个C++程序中都必须包含有这样一个函数,该函数的函数名为 。
A. main B. MAIN C. name D. function
2、设x和y均为bool量,则x&&y为真的条件是 。
A. 它们均为真 B. 其中一个为真 C. 它们均为假 D. 其中一个为假
3、下面的哪个保留字不能作为函数的返回类型? 。
A. void B. int C. new D. long
4、假定a为一个整型数组名,则元素a[4]的字节地址为 。
A. a+4 B. a+8 C. a+16 D. a+32
5、假定AB为一个类,则执行“AB a(4) , b[3] , * p[2] ;”语句时,自动调用该类构造函数的次数为 。
A. 3 B. 4 C. 6 D. 9
6、假定要对类AB定义加号操作符重载成员函数,实现两个AB类对象的加法,并返回相加结果,则该成员函数的声明语句为: 。
A. AB operator+(AB & a , AB & b) B. AB operator+(AB & a)
C. operator+(AB a) D. AB & operator+( )
7.由C++目标文件连接而成的可执行文件的缺省扩展名为 。
A. cpp B. exe
C. obj D. lik
8.在下面的一维数组定义中,哪一个有语法错误。
A. int a[ ]={1,2,3} B. int a[10]={0}
C. int a[ ] D. int a[5]
9.在下面的函数声明中,存在着语法错误的是 。
A. void BC(int a , int) B. void BD(int , int)
C. void BE(int , int=5) D. int BF(int x ; int y)
10. 设x和y均为bool量,则x||y为真的条件是 。
A. 它们均为真 B. 其中一个为真 C. 它们均为假 D. 其中一个为假
11.对于结构中定义的成员,其隐含访问权限为 。
A. public B. protected
C. private D. static
12.假定AB为一个类,则该类的拷贝构造函数的声明语句为 。
A. AB &(AB x) B. AB(AB x)
C. AB(AB &) D. AB(AB * x)
二、填空题(每题 2 分,共 24分)
1、程序运行中需要从键盘上输入多于一个数据时,各数据之间应使用 或 符号作为分隔符。
2、执行“cout <<char('A'+4)<<endl;”语句后得到的输出结果为 。
3、float 和 double 类型的大小分别为 和 。
4、算术表达式 对应的C++表达式为 。
5、执行“int x=45,y=16;cout<<x/y<<’’<<x%y<<endl;”语句序列后得到的输出结果为 。
6、假定一个一维数组的定义为“char * a[8] ;”,则该数组所含元素的个数为 ,所占存储空间的字节数为 。
7、变量分为全局和局部两种, 变量没有赋初值时,其值是不确定的。
8、假定a是一个二维数组,则a[i][j]的指针访问方式为 。
9、假定一个结构类型定义为“struct D { int a ; union { int b ; double c ; } ; D * d[2] ; } ;” ,则该类型的大小为 字节。
10、对一个类中的数据成员的初始化可以通过构造函数中的 实现,也可以通过构造函数中的 实现。
11、假定AB为一个类,则执行“AB a[10];”语句时,系统自动调用该类的构造函数的次数为 。
12、假定类AB中有一个公用属性的静态数据成员bb,在类外不通过对象名访问该成员bb的写法为 。
13.假定x=10,y=6,则表达式2+x+ +和+ +y*3的值分别为 和 。
14.已知’A’~’Z’的ASCII码为65~90,当执行“char ch=14*5+2; cout <<ch<<endl;”语句序列后,得到的输出结果为 。
15.使用const 语句定义一个标识符常量时,则必须对它同时进行 。
16.假定x=5,则执行“a=(x? 10:4*2);”语句后a的值为 。
17.若x=5,y=10,则x>y和x<=y的逻辑值分别为 和 。
18.执行“typedef int ABC[10];”语句把ABC定义为具有10个整型元素的 。
19.假定p所指对象的值为25,p+1所指对象的值为46,则执行“(*p)++;”语句后,p所指对象的值为 。
20.假定一个二维数组为a[M][N],则a[i]的地址值(以字节为单位)为 。
21.假定要访问一个结构指针p所指对象中的b指针成员所指的对象,则表示方法为 。
22.设px是指向一个类动态对象的指针变量,则执行“delete px;”语句时,将自动调用该类的 。
23.若需要把一个函数“void F( );”定义为一个类AB的友元函数,则应在类AB的定义中加入一条语句: 。
24.假定一个类AB中有一个静态整型成员bb,在类外为它进行定义并初始化为0时,所使用写法为 。
三、给出下列程序运行后的输出结果(每题 6分,共36分)
1、# 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;
}
2、# 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;
}
3、# 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;
}
4、# 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;
}
5、# 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;
}
6、# 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); // 后定义的变量将先被释放
}
7、 # include <iostream.h>
void main()
{
int s=0;
for (int i=1; ; i++) {
if (s>50) break;
if (i%2==0) s+=i;
}
cout <<"i,s="<<i<<","<<s<<endl;
}
8、 # include <iostream.h>
void main()
{
char a[]="abcdabcabfgacd";
int i1=0,i2=0,i=0;
while (a[i]) {
if (a[i]=='a') i1++;
if (a[i]=='b') i2++;
i++;
}
cout <<i1<<' '<<i2<<endl;
}
9、 # include <iomanip.h>
void main()
{
int a[9]={2,4,6,8,10,12,14,16,18};
for (int i=0; i<9; i++) {
cout <<setw(5)<<*(a+i);
if ((i+1)%3==0) cout <<endl;
}
}
10、 # include <iomanip.h>
void LE(int * a,int * b) {
int x=*a;
*a=*b; *b=x;
cout <<*a<<' '<<*b<<endl;
}
void main()
{
int x=10,y=25;
LE(&x,&y); cout <<x<<' '<<y<<endl;
}
11、 # include <iostream.h>
class A {
int a,b;
public :
A() { a=b=0; }
A(int aa,int bb) {
a=aa; b=bb;
cout <<a<<' '<<b<<endl;
}
};
void main()
{
A x,y(2,3),z(4,5);
}
12、 # include <iostream.h>
template <class TT>
class FF {
TT a1,a2,a3;
public :
FF(TT b1,TT b2,TT b3) {
a1=b1; a2=b2; a3=b3;
}
TT Sum() { return a1+a2+a3; }
};
void main()
{
FF <int> x(2,3,4),y(5,7,9);
cout <<x.Sum()<<' '<<y.Sum()<<endl;
}
四、写出下列每个函数的功能(每题 6 分,共 24 分)
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;
}
5、double SF(double x,int n) {
// n为大于等于0的整数
double p=1,s=1;
for (int i=1; i<=n; i++) {
p*=x;
s+=p/(i+1);
}
return s;
}
6、 float FH() {
float x,y=0,n=0;
cin >>x;
while (x!=-1) {
n++; y+=x;
cin >>x;
}
if (n==0) return y; else return y/n;
}
7、 # include <iostream.h>
void WA(int a[],int n) {
for (int i=0; i<n-1; i++) {
int k=i;
for (int j=i+1; j<n; j++)
if (a[j]<a[k]) k=j;
int x=a[i]; a[i]=a[k]; a[k]=x;
}
}
8、 # include <iomanip.h>
# include <fstream.h>
void JB(char * fname)
// 可以把fname所指字符串作为文件标识符的文件称为fname文件
// 假定该文件中保存着一批字符串,每个字符串的长度均小于20
{
ifstream fin(fname);
char a[20];
int i=0;
while (fin>>a) {
cout <<a<<endl;
i++;
}
fin.close();
cout <<"i="<<i<<endl;
}
五、编写程序(每题 10分,共 10 分)
1.把从键盘上输入的一批整数(以-1作为终止输入的标志)保存到文本文件“a:xxk1.dat”中。
2.编写一个函数,统计出具有n个元素的一维数组中大于等于所有元素平均值的元素个数并返回。
int Count(double a[],int n); // 此为该函数的声明。
试卷答案
一、单选题(每小题1分,共6分)
评分标准:选对者得1分,否则不得分。
1、A 2、A 3、C 4、C 5、B 6、B
7、B 8、C 9、D 10、 B 11、A 12、D
二、填空题(每小题2分,共24分)
评分标准:每题与参考答案相同者得2分,否则不得分。
1、 逗号 空格 2、 E
3、 4 8 4、 (x*y*y)/(3*a)+4*b-1
5、 2 13 6、 8 32
7、 局部 8、 *(a[i]+j) 或 *(*(a+i)+j)
9、 20 10、 初始化表 函数体
11、 10 12、 AB::bb
13、 12 21
14、 H
15、 初始化
16、 10
17、 false 或 0 true 或 1
18、 数组类型
19、 26
20、 a+( i*N )*sizeof( a[0][0] ) 或 a+i*sizeof( a[i] )
21、 *(p->b)
22、 析构函数
23、 friend void F( );
24、 AB:bb = 0;
三、给出下列程序运行后的输出结果(每小题6分,共36分)
评分标准:每题与参考答案的数据和显示格式完全相同者得6分,否则酌情给分。
1、 good! pass! bad! well!
2、 worker cadre
3、 10 20
15 35 5
10 20
4、 b=240
5、 liouting 38 493
6、 Destructor B! 7
Destructor A! 6
Destructor B! 0
Destructor A! 5
7、 i,s=15,56
8、 4 3
9、 2 4 6
8 10 12
14 16 18
10、 25 10
25 10
11、 2 3
4 5
12、 9 21
四、写出下列每个函数的功能(每小题6分,共24分)
评分标准:每题与参考答案的叙述含义相同者得6分,否则酌情给分。
1、比较两个整数a和b的大小,若a>b则返回1,若a= =b则返回0,若a<b则返回-1。
2、求出从键盘上输入的n个常数的平方和并返回。
3、模板函数,把数组a的每个元素按逆序放入数组b中。
4、建立一个具有n个结点的链表,每个结点的字符串值由键盘输入,链表的表头指针由引用变量f带回。
5、计算 1+X/2+X2/3+…+Xn/(n+1)的值并返回。
6、求出从键盘上输入的一批常数的平均值,以-1作为结束输入的标志。
7、采用选择排序的方法对数组a中的n个整数按照从小到大有次序重新排列。
8、从向文件fname中依次读取每个字符串并输出到屏幕上显示出来,同时统计并显示出文件中的字符串个数。
五、编写程序,把从键盘上输入的一批整数(以-1作为终止输入的标志)保存到文本文件“a:xxk1.dat”中。(10分)
评分标准:见参考程序中的注释。
1. # 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分
}
2. int Count(double a[],int n) {
double m=0;
int i;
for (i=0; i<n; i++) m+=a[i]; // 计算出所有元素之和得3分
m=m/n; // 计算出平均值得1分
int c=0;
for (i=0; i<n; i++)
if (a[i]>m) c++; // 按条件统计出元素个数得4分
return c; // 返回统计结果得2分
第 19 页 共 19 页
展开阅读全文