2008自考c++试题(附答案).txt女人谨记:一定要吃好玩好睡好喝好。一旦累死了,就别的女人花咱的钱,住咱的 房,睡咱的老公,泡咱的男朋友,还打咱的娃。一、单项选择题(本大题共20小题,每小题1分,共20分)
在每小题列出的四个备选项中只有一个是符合题目要求的,请将其代码填写在题后的括号内。错选、多选或未选均无分。
1.对C++语言和C语言的兼容性,描述正确的是( )
A.C++兼容C B.C++部分兼容C
C.C++不兼容C D.C兼容C++
2.在C++中使用流进行输入输出,其中用于屏幕输出的对象是( )
A.cerr B.cin
C.cout D.cfile
3.对使用关键字new所开辟的动态存储空间,释放时必须使用( )
A.free B.create
C.delete D.realse
4.如没有使用private关键字定义类的数据成员,则默认为( )
A.private B.public
C.protected D.friend
5.使用值传递方式将实参传给形参,下列说法正确的是( )
A.形参是实参的备份 B.实参是形参的备份
C.形参和实参是同一对象 D.形参和实参无联系
6.在函数调用时,如某一默认参数要指明一个特定值,则有( )
A.其之前所有参数都必须赋值 B.其之后所有参数都必须赋值
C.其前、后所有参数都必须赋值 D.其前、后所有参数都不必赋值
7.设存在函数int max(int,int)返回两参数中较大值,若求22,59,70三者中最大值,下列表达式不正确的是( )
A.int m = max(22,max(59,70)); B.int m = max(max(22,59),70);
C.int m = max(22,59,70); D.int m = max(59,max(22,70));
8.下列哪个类型函数不适合声明为内联函数( )
A.函数体语句较多 B.函数体语句较少
C.函数执行时间较短 D.函数执行时间过长
9.int Func(int,int);不可与下列哪个函数构成重载( )
A.int Func(int,int,int); B.double Func(int,int);
C.double Func(double,double); D.double Func(int,double);
10.对类中声明的变量,下列描述中正确的是( )
A.属于全局变量
B.只属于该类
C.属于该类,某些情况下也可被该类不同实例所共享
D.任何情况下都可被该类所有实例共享
11.类的私有成员可在何处访问( )
A.通过子类的对象访问 B.本类及子类的成员函数中
C.通过该类对象访问 D.本类的成员函数中
12.如果没有为一个类定义任何构造函数的情况下,下列描述正确的是( )
A.编译器总是自动创建一个不带参数的构造函数
B.这个类没有构造函数
C.这个类不需要构造函数
D.该类不能通过编译
13
.一个类可包含析构函数的个数是( )
A.0个 B.1个
C.至少一个 D.0个或多个
14.this指针存在的目的是( )
A.保证基类公有成员在子类中可以被访问
B.保证每个对象拥有自己的数据成员,但共享处理这些数据成员的代码
C.保证基类保护成员在子类中可以被访问
D.保证基类私有成员在子类中可以被访问
15.下列关于类的权限的描述错误的是( )
A.类本身的成员函数只能访问自身的私有成员
B.类的对象只能访问该类的公有成员
C.普通函数不能直接访问类的公有成员,必须通过对象访问
D.一个类可以将另一个类的对象作为成员
16.在编译指令中,宏定义使用哪个指令( )
A.#include B.#define
C.#if D.#else
17.设类A将其它类对象作为成员,则建立A类对象时,下列描述正确的是( )
A.A类构造函数先执行 B.成员构造函数先执行
C.两者并行执行 D.不能确定
18.下列描述错误的是( )
A.在创建对象前,静态成员不存在 B.静态成员是类的成员
C.静态成员不能是虚函数 D.静态成员函数不能直接访问非静态成员
19.对于友元描述正确的是( )
A.友元是本类的成员函数 B.友元不是本类的成员函数
C.友元不是函数 D.友元不能访问本类私有成员
20.在哪种派生方式中,派生类可以访问基类中的protected成员( )
A.public和private B.public和protected
C.protected和private D.仅protected
二、填空题(本大题共20小题,每小题1分,共20分)
请在每小题的空格中填上正确答案。错填、不填均无分。
21.设要把一个文件输出流对象myFile与文件“f:\myText.txt相关联,所用的C++语句是:__________。
22.C++中ostream类的直接基类是________。
23.运算符[ ]只能用________运算符来重载,不能用友元运算符来重载。
24.定义虚函数所用的关键字是________。
25.vector类中用于返回向量中第1个对象的方法是________。
26.在C++中,利用向量类模板定义一个具有20个char的向量E,其元素均被置为字符‘t’,实现此操作的语句是________。
27.类的继承是指子类继承基类的________和成员函数。
28.不同对象可以调用相同名称的函数,但可导致完全不同的行为的现象称为________。
29.________指令指示编译器将一个源文件嵌入到带该指令的源文件之中。
30.设类A有成员函数
void Fun(void);
若要定义一个指向类成员函数的指针变量pafn来指向Fun,该指针变量的声明语句是:________。
31.设在程序中使用如下语句申请了一个对象数组:
Point * ptr = new Point[2];
则在需要释放ptr指向的动态数组对象时,所使用的语句是________。
32.在保护派生中,基类权限为Private的成员在派生类中________。
33.类A有如下成员函数
int A::fun(double x){return (int) x/2;}
int A::fun(int x){return x*2;}
设a为类A的对象,在主函数中有int s=a.fun(6.0)+a.fun(2),则执行该语句后,s的值为________。
34.对象传送的消息一般由3部分组成:接收对象名、调用操作名和________。
35.将指向对象的指针作为函数参数,形参是对象指针,实参是对象的________。
36.在使用string类的find成员函数来检索主串中是否含有指定的子串时,若在主串中不含指定的子串,find函数的返回值是________。
37.在C++中,声明布尔类型变量所用的关键字是________。
38.执行下列代码
int a=29,b=100;
cout <
程序的输出结果是:________。
39.执行下列代码
cout <<″Hex:″<< hex << 255;
程序的输出结果为________。
40.C++语言中可以实现输出一个换行符并刷新流功能的操控符是________。
三、改错题(本大题共5小题,每小题2分,共10分)
下面的类定义中有一处错误,请用下横线标出错误所在行并给出修改意见。
41.#include
main( ) {
int x=5,y=6;
const int * p=&x;
*p=y;
cout<<*p<
}
42.#include
class f{
private:int x,y;
public:f1( ){x=0;y=0;}
print( ){cout<
}
main( ){
f a;
a.f1(1,1);
a.print( );
}
43.#include
class f{
private:int x=0,y=0;
public:void f1(int a,int b){x=a;y=b;}
void get( ){cout<
};
main( ){
f a;
a.f1(1,3);
a.get ( );
}
44.#include
class point{private:float x;
public:void f(float a){x=a;}
void f( ){x=0;}
friend float max(point& a,point& b);
};
float max(point& a,point& b)
{return(a.x>b.x)? a.x:b.x;}
main( ){
point a,b;
a.f(2.2);b.f(3.3);
cout<
}
45.#include
template
class f{
private:T x,y;
public:void f1(T a,T b){x=a;y=b;}
T max( ){retum(x>y)?x:y;}
};
main( ){
f a;
a.f1(1.5,3.8); ’
cout<
}
四、完成程序题(本大题共5小题,每小题4分,共20分)
46.完成下面类中的成员函数的定义。
class point
{
private:
int m,n;
public:
point(int,int);
point(point&);
};
point::point(int a,int b)
{
m=a;
________=b;
}
point::point(________)
{
m=t.m;
n=t.n;
}
47.下面是一个输入半径,输出其面积和周长的C++程序,在下划线处填上正确的语句。
#include
using namespace std;
________pi=3.14159;
void main( )
{
double r;
cout<<″r=″;
___________ ;
double l=2.0*pi*r;
double s=pi*r*r;
cout<<″\n The long is:″<
cout<<″The area is:″<
}
48.在下划线处填上缺少的部分。
#include
#include
using namespace std;
class complex
{
public:
int real;
int imag;
complex(int r=0,int i=0)
{
real=r;
imag=i;
}
};
complex operator+(________,complex& b)
{
int r=a.real+b.real;
int i=a.imag+b.imag;
return_________;
}
void main( )
{
complex x(1,2),y(3,4),z;
z=x+y;
cout<
}
49.程序的输出结果如下:
1,9
50,30
请根据输出数据在下面程序中的下划线处填写正确的语句。
源程序如下:
#include
using namespace std;
class base
{
private:
int m;
public:
base( ){ };
base(int a):m(a){}
int get( ){return m;}
void set(int a){m=a;}
};
void main()
{
base*ptr=new base[2];
ptr- >set(30);
ptr= _________;
ptr- >set(50);
base a[2]={1,9};
cout<
cout<
ptr=ptr-1;
cout<<________<
delete[ ]ptr;
}
50.在下面横线处填上求两个浮点数之差的cha函数的原型声明、调用方法。
#include
using namespace std;
void main( )
{
float a,b;
________//函数cha的原型声明
a=12.5;
b=6.5;
float c=_________; //调用函数cha
cout<
}
float cha(float x,float y)
{
float w;
w=x-y;
return w;
}
五、程序分析题(本大题共4小题,每小题5分,共20分)
51.给出下面程序的输出结果。
#include
template
class Sample
{
T n;
public:
Sample(T i){n=i;}
int operator==(Sample &);
};
template
int Sample
{
if(n==s.n)
return 1;
else
return 0;
}
void main( )
{
Sample
cout<<″s1与s2的数据成员″<<(sl==s2 ?″相等″:″不相等″)<
Sample
cout<<″s3与s4的数据成员″<<(s3==s4 ? ″相等″
:″不相等″)<
}
52.给出下面程序的输出结果。
#include
using namespace std;
template
T max(T ml,T m2)
{return(m1>m2)?ml:m2:}
void main( ) {
cout<
cout<
}
53.给出下面程序的输出结果
#include
using namespace std;
class A {
public:
int x;
A( ) { }
A(int a){x=a;}
int get(int a){return x+a;} };
void main( ) {
A a(8);
int(A::*p)(int);
p=A::get;
cout<<(a.*p)(5)<
A*pi=&a;
cout<<(pi- >*P)(7)<
54.给出下面程序的输出结果。
include
#include
using namespace std;
class Book {
char*title;
char*author;
int numsold;
public:
Book( ){ }
Book(const char*strl,const char*str2,const int num) {
int len=strlen(strl);
title=new char[len+1];
strcpy(title,str1);
len=strlen(str2);
author=new char[len+1];
strcpy(author,str2);
numsold=num; }
void setbook(const char*str1,const char*str2,const int num) {
int len=strlen(str1);
title=new char[len+1];
strcpy(title,str1);
len=strlen(str2);
author=new char[len+1];
strcpy(author,str2);
numsold=num;
}
~Book( ) {
delete title;
delete author;
}
void print(ostream&output) {
output<<″书名:″<
output<<″作者:″<
output<<″月销售量:″<
} .
};
void main( ) {
Book obj1(″数据结构″,″严蔚敏″,200),obj2;
obj1.print(cout);
obj2.setbook(″C++语言程序设计″,″李春葆″,210);
obj2.print(cout);
}
六、程序设计题(本大题共1小题,共10分)
55.在三角形类TRI实现两个函数,功能是输入三个顶点坐标判断是否构成三角形
#include
#include
class point{
private:float x,y;
public:point(float a,float b){x=a;y=b;}
point( ){x=0;y=0;} ,
void set(float a,float b){x=a;y=b;}
float getx( ){return x;}
float gety( ){return y;}
};
class tri{
point x,y,z;
float s1,s2,s3;
public;...settri(....);//用于输入三个顶点坐标
....test(....);//用于判断是否构成三角形
}; 请写出两个函数的过程(如果需要形式参数,请给出形参类型和数量,以及返回值
类型)
答案:
全国2008年10月自学考试C++程序设计试题答案
(供同学们参考,有争议的答案,请留言)
一、选择题
1. A 2. C 3.C 4. A 5. A 6.A 7. C 8.A 9.B 10.C 11.D 12.A 13.B 14.B 15.A 16.B 17.B 18.A 19.B 20.B
二、填空题
21.ofstream myFile(“f:\\myText.txt)
22.ios
23. 类
24.virtual
25.front()
26.vector
27. 数据成员
28.多态性
29.#include
30.void (A::*pafn) (void)
31.delete [] prt
32.不可访问的
33.7
34. 必要的参数
35. 地址值
36.-1
37. bool
38. 29100
39.Hex:ff
40.endl
三、改错题
41.错误 *p=y;
修改意见:将const int *p=&x 中的const去掉
42.错误:a.f1(1,1)
修改意见:增加一个f1的函数重载:void f1(int I,int j){x=I; y=j;}
43.错误:private:int x=0,y=0;
修改意见:private:int x,y;
44.错误:cout<
修改意见:cout<
45.错误:f a;
改为:f
四、完成程序题
46.(1) n (2) point &t
47. (1) const (2) cin<
48. (1)complex & a
(2) complex (r,i);
49.(1)ptr+1 (2) prt->get()
50.(1) float cha(float, float);
(2) cha(a,b)
五、程序分析题
51. s1 和s2的数据成员不相等
s3和s4的数据成员相等
52. 7 5.0
y a
53.13
15
54. 书名:数据结构
作者:严蔚敏
月销售量:200
书名:C++语言程序设计
作者:李春葆
月销售量:210
六、程序设计题
void settri()//用于输出三个顶点坐标
{
int a,b;
cout<<"input the firest point"<
x.set(a,b);
cout<<"input the second point"<
y.set(a,b);
cout<<"input the third point"<
z.set(a,b);
cout<<"the three point's codinate is:"<
void test()//用于判断是否构成三角形
{
if((x.gety()-y.gety())/(x.getx()-y.getx())!=(y.gety()-z.gety())/(y.getx()-z.getx()))
cout<<"yes"<
cout<<"no"<
};
void main()
{
tri a;
a.settri();
a.test();
system("pause");
}