C++程序设计 试题及答案(一)
时间:2011-07-17点击:次
C++程序设计 试题及答案(一)
学号: 学习中心名称: 专业: 层次: 姓名:
题号 | 一 | 二 | 三 | 四 | 五 | 六 | 七 | 八 | 九 | 十 | 总分 |
得分 |
|
|
|
|
|
|
|
|
|
|
|
注意:所有答案都要写在答题卡上
一、单项选择题
1.下面正确的字符常量是:
A) “c” B) ‘\\’’ C) ‘W’ D) ‘’
2.下列字符串中不能作为C++标识符使用的是:
A) WHILE B) user C) _1var D) 9stars
3.执行语句序列的输出结果是______。
int i=0;
while(i<25)
i+=3;
cout<<i;
A) 24 B) 25 C) 27 D) 28
4.下列符号中可以用作C++标识符的是______。
A) radius B) foo~bar C) else D) 3room
5.若a是int类型变量,则表达式a=25/3%3的值是:
A) 3 B) 2 C) 1 D) 0
6.以下叙述中不正确的是:
A) 在不同的函数中可以使用相同名字的变量
B) 函数中的形参是局部变量
C) 在一个函数内部定义的变量只在本函数范围内有效
D) 在一个函数内部定义的变量在所有函数内部有效
7.变量的引用,其含义是指该变量的:
A.值 B.类型 C.别名 D.地址
8.已知定义:char s[10]; 则下面不表示s[1]的地址的是:
A.s+1 B.*(s+1) C.&s[0]+1 D.&s[1]
9.通常拷贝构造函数的参数是:
A.对象 B.对象的成员 C.对象的引用 D.对象的指针
10.派生类对象的构造顺序是先调用:
A.派生类的构造函数 B.基类的构造函数
C.内嵌对象的构造函数 D.友元类的构造函数
二、填空题
1.动态多态是指在___(1)____时才确定使用哪个___(2)___函数的方式。
2.友员不是类的成员,但必须在___(3)__予以声明,它具有存取类的__(4)__成员的特权。
3.C++中class与struct的主要区别是在缺省访问权限时,__(5)_的成员为私有的,而__(6)_的成员为公有的。
4.若有定义语句:int a=3,b=2;,则表达式a<b?a :b的值是___(7)____。
5.表达式:26%3=___(8)____,32/5=___(9)____。
6.类的成员包括__(10)__和___(11)___两种,在面向对象的术语中,前者称为属性、后者称为方法。其访问权限有三种,由符号__(12)___、__(13)__和_(14)_指定,其中具有__(15)_权限的成员只有类中的成员函数才能访问、而具有__(16)__权限的成员在任何函数中都可访问。
7.对基类成员的初始化,必须在派生类构造函数的 (17) 中进行。
8.C++源程序文件的扩展名是 (18) ,头文件的扩展名是 (19) 。
9.若n为整型,则表达式n=(float)2/3的值是 (20) 。
三、写出程序的运行结果
1.写出程序运行结果
include <iostream.h>
#include <string.h>
class CRect
{
private:
char color[10];
int left;
int top;
int length;
int width;
public:
CRect();
CRect(char *c, int t, int lef, int len, int wid);
void SetColor(char *c);
void SetSize(int l=100, int w=100);
void Move(int t,int l);
void Draw();
};
CRect::CRect()
{
strcpy(color, "Black");
top = 0;
left = 0;
length = 0;
width = 0;
}
CRect::CRect(char *c, int t, int lef, int len, int wid)
{
strcpy(color, c);
top = t;
left = lef;
length = len;
width = wid;
}
void CRect::SetColor(char *c)
{
strcpy(color, c);
}
void CRect::SetSize(int l, int w)
{
length=l;
width = w;
}
void CRect::Move(int t,int l)
{
top = t;
left = l;
}
void CRect::Draw()
{
cout << "矩形左上角坐标为(" << left << "," << top << ")" << endl;
cout << "矩形长和宽分别为" << length << "," << width << endl;
cout << "矩形的颜色是" << color << endl;
}
void main()
{
CRect r;
r.SetColor("Red");
r.Move(10,20);
r.SetSize(100,200);
r.Draw();
r.Move(50,50);
r.SetColor("Blue");
r.Draw();
}
2.写出程序运行结果
#include <iostream.h>
class A
{
int x,y;
public:
A()
{
x=0;
y=0;
}
A(int a, int b)
{
x=a;
y=b;
}
~A()
{
if(x==y)
cout << "x=y" << endl;
else
cout << "x!=y" << endl;
}
void Display()
{
cout << "x=" << x << " ,y=" << y << endl;
}
};
void main()
{
A a1, a2(2,3);
a1.Display();
a2.Display();
}
3.写出程序运行结果
#include <iostream.h>
class A
{
private:
int n;
public:
A(int i)
{
n=i;
}
operator ++()
{
n++;
}
operator ++(int )
{
n+=2;
}
void Display()
{
cout << "n=" << n << endl;
}
};
void main()
{
A a(2), b(2);
a++;
++b;
a.Display();
b.Display();
}
4.写出程序运行结果
#include <iostream.h>
int func1(int n);
int func2(int n);
void main()
{
int sum;
sum = func2(5);
cout << sum << endl;
}
int func1(int n)
{
if(n==1)
return 1;
else
return n*func1(n-1);
}
int func2(int n)
{
int s = 0;
for(int i=1; i<=n; i++)
s += func1(i);
return s;
}
四、编程(根据要求填上程序缺少的部分)
1.完成如下的程序,使得输出为:
1234
#include <iostream.h>
#include <math.h>
class A
{
private:
________(1)___________
protected:
________(2)__________
public:
A(int a, int b, int c)
{
X=a;
Y=b;
Z=c;
}
int GetX()
{
return X;
}
int GetY()
{
return Y;
}
int GetZ()
{
return Z;
}
};
class B _____(3)_______
{
private:
int K;
public:
B(int a, int b, int c, int d)_______(4)_________
{
K=d;
}
void Show()
{
cout << GetX() << GetY() << Z << K << endl;
}
};
void main()
{
B b(1,2,3,4);
b.Show();
}
2.在主函数中定义有30个元素的数组s,函数func1()的功能是将2、4、6、…、56、58、60分别赋给数组元素s[0]、s[1]、s[2]、…、s[27] 、s[28] 、s[29]。函数func2()的功能是按顺序将数组的每5个元素求平均值并存入数组w中(即将s[0]、s[1]、s[2] 、s[3]、s[4]的平均值存入w[0]中,s[5]、s[6]、s[7] 、s[8]、s[9]的平均值存入w[1]中,…)。请填空使程序正确运行。
#include <iostream.h>
#include <math.h>
void func1(double s[])
{
int i,k;
for(k=2,i=0; i< 30; i++)
{
s[i]=k;
______(5)_________
}
}
void func2(double s[], double w[])
{
double sum;
int i,k;
for(k=0,i=0,sum=0; i<30; i++)
{
_________(6)___________
if( (i+1)%5==0)
{
w[k] = sum/5;
_________(7)__________
k++;
}
}
}
void main()
{
double s[30], w[6];
func1(s);
func2(s,w);
}
C++程序设计 试题及答案(一)答案
一、单项选择题
1.C
2. D
3. A
4. A
5. B
6. D
7. D
8.A
9. c
10. b
二、填空题
1. 执行, 成员
2. 类中, 私有
3. class, struct
4. 2
5. 2, 6
6. 数据成员, 函数成员
7. public, private, protect, 私有, 公共
8. .cpp .h
9. 0.5
三、写出程序的运行结果
1.矩形左上角坐标为50,50
矩形长和宽分别为100,100
矩形的颜色是Blue
2.x= 0,y=0
x=2,y=3
x=y
x!=y
3,n=3
n=4
4, 5
四、编程(根据要求填上程序缺少的部分)
1. int X,Y;
2. int Z;
3.public A
4.: A(a,b,c)
5.k+=2;
6.sum += s[i];
7.sum = 0;