2006年4月计算机等级考试二级C++真题

选择题:
1.设变量a是整型,f是实型,i是双精度型,则表达式10+’a’+i*f值的数据类型为(C)。
A.int B.float C.double D.不确定
2.设变量n为float类型,m为int型,则以下能够将n中的数值保留小数点后两位,第3位进行四舍五入运算的表达式是(B)
A.n=(n*100+0.5)/100.0 B.m= n*100+0.5,n=m/100.0
C.n= n*100+0.5/100.0 D.n=(n/100+0.5)*100.0
3.与下面程序等价的是(A)
While(a)
{If(b)continue;C;}
A.while(a){if(!b)c;} B.while(c){if(!b)break;c;} C.while(c){if(b)c;} D.while(a){if(b)break;c;}
4.以下有关变量时间与空间属性的叙述不正确的是(C)
A.变量的时间属性是指变量有一定的生存期;
B.变量的空间属性是指变量有一定的作用域;
C.static关键字加在变量定义语句前只能使变量的生存期延长;
D.静态全局变量是指其作用域最多只局限在本文件范围。
5.以下有关编译预处理的叙述正确的是(D)
A.宏定义与const型常量是一样的,宏名相当于一个内存变量。
B.宏替换使用宏后面表达式的值去替换程序代码中出现的宏名;
C.文件包含命令中如果指定了被包含文件的路径,则只到该路径位置查找文件;
D.条件编译命令中的格式与选择结构语句的格式不完全一样
6.已知教师记录的描述为
struct teacher
{ int id;
char name[20];
struct {int y;intm;int d;}birth;
}t;
将变量t中的d成员赋值为12的语句为(D)
A.d=12 B.birth.d=12 C.t.d=12 D.t.birth.d=12
7.typedef long BIG的作用是(D)
A.建立了一种新的数据类型 B.定义了一个整型变量
C.定义了一个长整型变量 D.定义了一个新的数据类型标识符
8以下有关类的说法,不正确的是(D)
A.类是一种用户自定义的数据类型;
B.只有类中的成员函数或类的友元函数才能存取类的私有数据;
C.在类中,如果不做特殊说明,所有数据均为私有数据;
D. 在类中,如果不做特殊说明,所有成员函数均为公有数据。
9.以下不是构造函数特征的是(D)
A.构造函数的函数名与类名相同 B.构造函数可以重载
C.构造函数可以设置默认参数 D.构造函数必须指定类型说明
10.以下有关类和对象的叙述的叙述,不正确的是(C)
A.任何一个对象都属于一个具体的类;
B.类与对象的关系和数据类型与变量的关系类似;
C.类的数据成员不允许是另一个类的对象;
D.一个类可以被实例化多个对象。
11.设有以下类的定义:
class Ex
{ int x;
public :
void setx(int t=0);
};
若在类外定义成员函数setx(),以下定义形式正确的是(B)
A.void setx(int t){…} B. void Ex:: setx(int t){…}
C. Ex::void setx(int t){…} D. void setx(){…}
12.以下对派生类的描述中不正确的是(

C)
A.一个派生类可以作为另一个派生类的基类;
B.一个派生类可以有多个基类;
C.具有继承关系时,基类成员在派生类中的访问权限不变;
D.派生类的构造函数与基类的构造函数有成员初始化参数传递关系。
13.设有基类定义:
class cbase
{ private:int a;
protected:int b;
public:int c;
};
派生类采用何种继承方式可以使成员变量b成为自己的私有成员(A)
A.私有继承 B.保护继承 C.公有继承 D.私有,保护,公有均有
14.设有cbase为基类,cderived是cbase的派生类,且有以下定义:
cbase a1,*b1;
cderived aa1,*bb1;
则以下语句不合语法的是(D)
A.b1=bb1; B.a1=aa1; C.b1=&aa1; D.bb1=(cbase*)b1;
15.下列不能以写方式打开文件的是(C)
A.ofstream f(“c1.txt”); B.ofstream f; f.open((“c1.txt”);
C. fstream f(“c1.txt”); D. fstream f; f.open((“c1.txt”,ios::out);
填空题
1.执行下列语句后,a的值是-264。int a=12;a+=a-=a*a;
2.若有定义:char c=’\010’;则变量c中包含的字符个数为1个。
3.当执行完下列语句段后,i,j,k的值分别为5,4,6
int a=10,b,c,d,I,j,k;
b=c=d=5;
i=j=k=0;
for(;a>b;++b)i++;
while(a>++c)j++;
do(k++;)while(a>d++);
4.若有宏定义:#define F(a,b) a-b,#define G(a,b) (a+b).而程序执行语句中有如下语句:a=6;b=4;cout<5.已知以下枚举类型定义,枚举量Fortran的值是102.
enum language{Basic=3,Assembly,Ada=100,Cobol,Fortran};
6.在C++类的定义中,利用属性描述对象的特征,利用方法或操作描述对象的方法。
7.类的成员按访问权限可分为三类,分别是public,private,protected。如果不做特殊说明,类成员的默认访问权限是private。
8.构造函数的主要作用是初始化对象的数据成员,析构函数的主要作用是释放内存等清理工作。
9.在继承机制下,当对象消亡时,编译系统先执行派生类析构函数,然后才会执行对象成员和基类的析构函数。
10.在下列程序中,x数据可访问,y,w,z不可访问。
class A
{ public:
int x;
protected:
int w;
private:
int z;};
class B:public A
{public:
void setw(int a){w=a;}
protected:
int y;}
void main()
{ B bb;
bb.x=5;
bb.y=10;
bb.w=15;
bb.z=20;
bb.setw(10);}
11.在编译时解析的函数调用称为前期绑定,在运行时解析的函数调用称为后期绑定。
12.如果在类中定义了一个成员函数为虚函数,则表明在该继承层次链条的派生类中可能重新定义这个成员函数的实现,即这个虚函数可能会被派生类的同名函数所覆盖(override)。
问答题:
1.设a=1,b=2,c=3,分别求表达式c+=a>b?++a:++b,a<<2,c=(a=10,b+a*2,b*2),~b,bIc的值
c+=a>b?++a:++b 6
a<<2

4
c=(a=10,b+a*2,b*2) 40
~b -3
bIc 3
2.按要求写出C++表达式
(1)将整数k转换成实数 (float)k
(2)求实数x的小数部分 x-(int)x
(3)求自然数m的十位数字 (m/10)%10
(4)将ch中的大写字母转换成相应的小写字母 ch>=’A’&&ch<=’Z’?ch+32:ch
3.以下函数是否可以认为是重载函数,为什么?
(1)enum E1(one,two);
enum E2(three,four);
int f(int x, E1 y);
int f(int x, E2 y);
答:是
(2)typedef double scientific;
double f(double x);
scientific f(scientific x);
答:不是
程序阅读题:
程序一:
#include
using namespace std;
int main()
{ int m,n;
cout<<”Enter m and n:”;
cin>>m>>n;
while(m!=n)
{while(m>n) m-=n;
while(n>m) n-=m;}
cout<<”m=”<return 0;}
程序运行时,输入95,14
输出:m=1
程序二:
#include
struct st{int x;int *y}*p;
int s[]={5,6,7,8};
st a[]={10,&s[0],20,&s[1],30,&s[2],40,&s[3]};
void main()
{ p=a;
cout<x<<”,”;
cout<<(++p)->x<<”,”;
cout<<*(++p)->y<<”,”;
cout<<++(*(++p)->y)<<”,”;}
输出:10,20,7,9
程序三:
#include
class Sample
{char c1,c2;
public:
Sample (char a){c2=(c1=a)-32;}
void disp()
{ cout<Void main()
{ Sample a(‘a’),b(‘b’);
a.disp();b.disp():}
输出:a转换为A
B转化为B
程序四:
#include
int count=0;
class Point
{int x,y;
public:
Point()
{x=1;y=1;count++}
~Point()
{count--;}
friend void display();};
void display()
{cout<<”There are”<void main()
{Point a;
display();
{Point b[5];display();}
display();}
输出:There are 1 points,There are 6 points,There are 1 points,
程序五:
#include
int count=0;
class Point
{int x,y;
public:
Point()
{x=1;y=1;count++}
~Point()
{count--;}
friend void display();};
void display()
{cout<<”There are”<void main()
{Point a;
display();
Point b[5];display();
display();}
输出:There are 1 points,There are 6 points,There are 6 points,
程序六:
#include
using namespace std;
class A;
{Public:
Virtual void a()
{cout<<”A::a()”<Void b()
{cout<<”A::b()”<class B:public A
{public:
virtual void a()
{cout<<”B::a()”<Void b()
{cout<<”B::b()”<class C:classB
{Public:
Virtual void a()
{cout<<”C::a()”<Void b()
{cout<<”C::b()”<Int main()
{A *p1;
B b1;
C c1;
P1=&b1;
P1->a();
P1->b();
P1->&c1;
p1->a();
p1->b();
return 0;}
输出:B::a();
A::b();
C::a();
A::b();
程序设计题:
1.试分别用非递归与递归函数的方式编写s=

1+2+3+…+n的函数,函数原型为
float sum(int n),限定n>=1。
#include
#include
using namespace std;
float sumD(int n);
float sumF(int n);
void main()
{ int sd,sf,n;
cout<<"Please input n:"<cin>>n;
sd=sumD(n);
sf=sumF(n);
cout<<"sumD="<cout<<"sumF="<float sumD(int n)
{ int s;
if (n==1) s=1;
else s=n+sumD(n-1);
return s;}
float sumF(int n)
{ int s=0,i;
for(i=1;i<=n;i++) { s=s+i; }
return s;}
2.编写函数int f(char *s),判断s所指的串是否是“回文串”,即前后对称的串,如:“a131a”,“a1bb1a”,若是返回1,否则返回0;
#include
#include
#include
using namespace std;
int f(char *s);
void main()
{ int i,j;
char p[50];
cout<<"please input string:"<cin>>p;
cout<cout<if(f(p))
cout<else cout<int f(char *s)
{ int m,n,i;
n=strlen(s);
m=n;
for(i=0;i{ if(s[i]!=s[n-1])
return 0;
n--; }
return 1; }
3.设计一个立方体类Box,它能计算并输出立方体的体积和表面积。
#include
//using namespace std;
class Box
{
public:
Box(int x)
{
a=x;
}
int SumV()
{
v=a*a*a;
return v;
}
int SumS()
{
s=6*a*a;
return s;
}
private:
int a,s,v;
};
void main()
{
int a;
cin>>a;
Box A(a);
cout<}


相关文档
最新文档