资源描述
(完整word)西南交大历年C++期末试题
历 年 期 末 试 题
C + +
2007——-2008C++期末试题
一.选择题(10小题,每题3分)
1. 程序设计语言的基本成分是数据成分、运算成分、控制成分和
A)对象成分 B)变量成分 C)语句成份 D)传输成分
2. 以下各选项组中,均是C++语言关键字的组是
A) public operator this B)shod string static
C)it while>= D)private cout printf
3. C++语言的跳转语句中,对于break和continue说法正确的是()
A)break语句只应用与循环体中
B)continue语句只应用与循环体中
C)break是无条件跳转语句,continue不是
D)break和continue的跳转范围不够明确,容易产生问题
4。 for(int x=0,y=o;!x& &y<=5;y++)语句执行循环的次数是()
A)0 B)5 C)6 D)无次数
5。 下面程序错误的语句是
①#include〈iostream。 h〉
②void main()
③{
④int*p=new int[1];
⑤p=9;
⑥cout 〈〈*p〈〈eddl;
⑦delete[]p
⑧}
A)④ B)⑤ C)⑥ D)⑦
6. 下列描述正确的是
A)表示m>n为true或m<n为true的表达式为m>n&&m〈n
B)switch语句结构中必须有default语句
C)if语句结构中必须有else语句
D)如果至少有一个操作数为true,则包含“‖”运算符的表达式为true
7。 下列语句中不是死循环的是
A)int i=10; B)for( int i=1:i<10;i++)
while(1) {
} i-—;
i--; }
if(i==1)break;
}
C)int i=0; D)int i=1;
do{ for(;;)i=i+1;
i++;
}while(i〉=0):
8. 下面对静态数据成员的描述中,正确的是
A)静态数据成员是类的所有对象共享的数据
B)类的每个对象都有自己的静态数据成员
C)类的不同对象有不同的静态数据成员值
D)静态数据成员不能通过类的对象调用
9. 下面关于数组的描述错误的是
A)在C++语言中数组的名字就是指向该数组第一个元素的指针
B)长度为n的数组,下标的范围是0—n—1
C)数组的大小必须在编译时确定
D)数组只能通过值参数和引用参数两种方式传递给函数
10. 以下程序中,错误的行是
①#include〈iostream. h〉
②class A
③{
④ public:
⑤ int n=2:
⑥ A( int val){cout〈〈val<〈endl;}
⑦ ~A(){};
⑧};
⑨void main()
⑩{
⑩ A a(0);
⑩}
A)⑤ B)⑥ C)⑦ D)⑩
二.写出下列程序运行结果(四小题,每题5分)
1。 #incude
int f(int);
int main()
{ int i;
for(i=0;i〈5;i++)
cout〈<F(I)<<”";
return0; }
int f(int i)
{ static int k=1;
for(;i>0;i— -)
k + = i;
return k; }
2。 #include〈iostream。 h>
void fun(int*a,int*b)
{ int x=*a
*a=,b;*b=x;
cout<<*a〈<*b〈<" "; }
void main()
{ int x=1,y=2;
fun(&x,&y);
cout<<x〈<y<〈endl;
3. #include〈iostream.h>
void main(void)
{ int i,j,n=4;
for(i=1;i<=n;i++)
{ for(j=1;j<=30;j++)
cout〈<’ ’; for(j=1; j〈= 2*(n—i) ;j++)
cout<〈' ';
for(j=1; j〈=2*i—1 ;j++)
{if (j==1||j==2*i-1) cout〈<'*’;
else cout<<" ";}
cout<〈endl; }
for(i=1;i〈=n-1;i++)
{ for(j=1;j〈=30;j++)
cout<〈’ '; for(j=1; j<=7-2*i ;j++)
{if(j==1||j==7—2*i) cout<<’*’;
else cout<<” ”;}
cout<<endl; } }
4。 void main()
{int i,j,n;
cout<〈"input 行数: ”;cin〉〉n;
for(i=1;i<=n;i++)
{for(j=1;j<=i;j++)
cout〈〈setw(2)〈〈i;
for(j=i+1;j〈=n;j++)
cout〈<setw(2)〈〈j;
cout<〈endl;}}
5. #include 〈iostream.h>
void main()
{ void fun(int*,int*);
int a=5,b=8;
while(a!=b)
{ fun(&a,&b);
cout〈〈a〈<'\t’〈<b<〈endl;}
}
void fun(int *pa,int *pb)
{ if(*pa〉=*pb) *pa-=*pb;
else *pb-=*pa; }
6。 #include <iostream.h>
void main()
{char *p; int i;
int len(char *ptr);
p=”asdfghjkl9876";
i=len(p);
cout<〈"length of ”<<"\"”〈<p
<<"\"”〈〈” is "<<i<<endl;}
int len(char *ptr)
{ int i;
for(i=0;*ptr++!='\0';i++)
; return(i);}
三.编写程序
1。求Fibonacci数列的第n项及前n项之和。
2. S是一家小型园林公司的老板,他需要创建一个 程序使其销售人员能够算出铺设草坪的费用。作为一家围栏公司的老板,J也需要一个程序,可以计算出安装围栏的费用.
2006――2007 C++期末试题
一、填空(每空2分,共20分)
1。C++源程序文件扩展名为_____________.
2。unsigned long 型数据占__________字节存储空间.
3.执行语句cout<〈"d:\\data\\c++\\test.exe”;后,输出结果是___________
____________________.
4。表达式8|3的结果是__________.
5.全局变量和__________________若在定义时未进行初始化,则自动初始化为0.
6.将s初始化为值是"Hello!”的指针常量,初始化语句是_____________________。
7。类中的成员只能为该类的成员函数及该类的派生类中的成员函数访问,则应加上访问属性__________________。
8。定义输出文件流f,并以追加数据方式打开相应文件"a:aaa.dat",定义f的语句是_________________________________.
9。设文件f。cpp中的一个函数要访问在另一个文件中定义的int型变量x,则在f.cpp中x应定义为__________________________。
10.函数模板的说明格式是:template__________________函数定义。
二、单项选择题(每空2分,共20分)
1。类型修饰符unsigned修饰( )类型是错误的。
A char B int C long int D float
2。下列变量名中,( )是合法的。
A _var B bye-bye C int D 123_cc
3.设int a=3,* p=&a;,则*p的值是( ).
A 变量a的地址值 B 无意义 C 变量p的地址值 D 3
4。设int m=0;,下列表示引用的方法中,( )是正确的。
A int &x=m; B int &y=10; C int &z; D float &t=&m
5。循环for(int i=0,x=0;!x&&i<=5;i++);的循环体执行次数是( )。
A 5 B 6 C 1 D 无限
6。磁盘文件操作时,在打开磁盘文件的访问方式中,( )是以追加数据
方式打开文件的。
A in B out C app D ate
7.类X中,若没有定义构造函数,则默认的构造函数是( )。
A X ( ){ } B X (X & x){ *this=x;} C ~X ( ){ } D 不存在
8.设int * p2=&x,* p1=a;*p2=*b;则变量a和b的类型分别是( )。
A int和int B int * 和int C int和int * D int * 和int *
9。设p为指针变量,则以下表达式正确的是( )。
A --p++ B --++p C ——(p+5) D (p-5)++
10。设void f1(int &x,char * p);int m;char s[ ]="c++";以下调用合法的是( )。
A f1(&m,&s); B f1(&m,s); C f1(m,s); D f1(m,&s);
三、回答问题(每题5分,共15分)
1.设int a[3][4];double b[3][4][5];
请写出数组指针pa和pb的定义,使其分别与数组名a和b等价。
2.设int f1(int n,char * s);char * f2(int n,char * s);
3。请初始化一维指针数组week。其初值表初值为"Sun”,”Mon",”Tue”,"Wed”,"Thu","Fri”和"Sat”。
4.要使语句p1=new int(10) ;p2=new double [10];能正常执行,写出p1和p2的定义。
5。设有int f1(int n){n+=2;return n*n;}
int f2(int &n){n+=2;return n*n;}
执行 int m=5,n=3;
m=f1(n);cout<〈m<〈’ ’<<n〈<endl;
m=f2(n);cout〈<m〈〈’ '〈<n<<endl;
写出结果。
四、(每题10分,共20分)
1.写出下列程序的执行结果。
# include 〈iostream。h>
void main()
{int i=1,j=2,k=3,a=10;
if (!i) a—-;
else if(j)
if(k) a=5;
else a=6;
a++;
cout<〈a〈<endl;
if(i〈j)
if(i!=3)
if(!k)
a=1;
else if(k)
a=5;
a+=2;
cout<〈a<<endl; }
2。设a盘上文本文件aaa。dat中保存有0至100之间的所有奇数,下列程序将全部数据及其和输出至屏幕上,请将程序补充完全.
# include 〈stdlib.h〉
# include ____________
void main(void)
{ _____________ f1("a:aaa。dat",ios::in|ios::nocreate);
if (!f1) {
cerr〈〈"a:aaa.dat file not open!"<<endl;
exit(1);
}
int x,s=0;
while (___________){f1>>x;cout<〈x〈<’ ’;______________;}
f1.____________;
}
五、(每题10分,共20分)
1。用while循环编程,求自然数1至100之间各奇数平方和并输出。
2.设有函数void sum(int score[ ][5],int row);,它将数组score每行后四个元素之和用首元素保存。试①编写这个函数 ②编写主函数并初始化相应实参调用这个函数,并输出结果
3.一圆型游泳池如图所示,现在需在其周围建一圆型过道,并在其四周围上栅栏。栅栏价格为35元/米,过道造价为20元/平方米。过道宽度为3米,游泳池半径由键盘输入。要求编程计算并输出过道和栅栏的造价。
2005――2006 C++期末试题
一.填空题(每小题2分,共24分) 1.若需要定义一个标识符常量,并且使C++能够进行类型检查,则应在定义语句的开始使用保留字 。
2.算术表达式+46-1对应的c++表达式为 .
3.逻辑表达式x>3 & & x<10的相反表达式为 。
4.逻辑表达式a>b||b==5的相反表达式为 。
5.假定一个二维数组的定义为“char a[5][6];”,则该数组所含元素的个数为 ,所占存储空间的字节数为 。
6.变量分为全局和局部两种, 变量没有赋初值时,将由系统自动置为0。
7.假定a是一个一维数组,则a[i]对应的存储地址(以字节为单位)为 。
8.假定一个结构类型的定义为“structA{int a,b;A * c;};”,则该类型的大小为
字节.
9.假定要访问一个结构指针p所指对象中的b指针成员所指的对象,则表示方法为
。
10.在一个派生类中,对基类成员、类对象成员和非类对象成员的初始化次序是先)
,后 ,最后为 。
11.假定用户没有给一个名为AB的类定义构造函数,则系统为其隐含定义的构造函数为 。
12.若需要把一个函数“void F();"定义为一个类AB的友元函数,则应在类AB的定义中加入一条语句:
二.写出程序运行结果(共5小题,每小题6分,30分)
1.#include 〈iostream.h>
#include <iomanip。h〉
void fiddle(int in1, int &in2);
void main()
{ int count = 7, index = 12;
cout 〈< ”The values are ”〈〈setw(5)<〈count;
cout〈<setw(5)<〈index<<endl;
fiddle(count, index);
cout 〈〈 "The values are ”;
cout<〈setw(5)<<count;
cout〈<setw(5)<<index〈<endl;}
void fiddle(int in1, int &in2)
{ in1 = in1 + 100; in2 = in2 + 100;
cout <〈 ”The values are "<<setw(5)〈<in1;
cout<<setw(5)<<in2〈〈endl;}
2.#include〈iostream>
using namespace std;
void main()
{ int i(1),a(0);
for(;i<=5;i++)
{
do{ i++;
a++;
}while(i<3);
i++;
}
cout〈〈a〈<”,”<<i〈<endl;
}
3.#include<iostream>
using namespace std;
void main()
{int i,j;
int n=5;
for(i=1;i<=n;i++)
{ for(j=1;j<=i;j++)
cout〈<” ";
for(j=1;j<=2*(n-i);j++)
cout〈〈"*”;
cout〈〈endl; }
4.#include 〈iostream>
using namespace std;
int a=1,b=2,c=3;
void main()
{ int a=4,b=5;
int han(int a,int b);
c=a+b;
cout<〈c<<endl;
cout<〈han(c,b)〈〈endl;
cout<〈a<<endl;}
int han(int c,int b)
{ cout<〈a〈<endl;
a=b+c;
cout〈<a<<endl;
return a;}
5.#include <iostream>
using namespace std;
void main()
{ int n=0;
while (n<4)
switch(n)
{case 0:
case 1: cout〈<” "〈〈n;
case 2: cout〈<" "<<n++;break;
default: cout〈〈”*”;n++;}}
三.改错(共3小题,每小题6分,18分)
1.#include 〈iostream>
using namespace std;
void main()
{int num = 1;
while (num 〈 5)
{cout 〈〈 num 〈< endl;
} //end while}
2.#include <iostream〉
using namespace std;
void getNumber(int &);
void calcSum(int, int, int);
int main()
{ int num1 = 0;
int num2 = 0;
int num3 = 0;
getNumber(&num1);
getNumber(&num2);
calcSum(num1, num2, num3);
cout 〈< "Sum: " <〈 num3 〈< endl;
return 0; }
void getNumber(int &num)
{ cout <〈 ”Enter a number: ";
cin >〉 num;}
void calcSum(int n1, int n2, int n3)
{ n3 = n1 + n2;}
3.#include <iostream〉
#include <string〉
using namespace std;
void getName(string);
int main()
{ string name = ””;
getName(name);
cout 〈< "You entered ” <〈 name << ” as the name.” << endl;
return 0;}
void getName(string nameInput)
{ cout <〈 "Customer name: ";
getline(cin, nameInput);}
四.编写程序(3小题,每小题6分,共18分)
1.编程计算键盘输入正整数的各位数字之和。
2.实现x、y交换(用两种方法)
3.如果一个三位数的个位数、十位数和百位数的立方和等于自身,则称该数为水仙花数,编程找出所有的水仙花数.
五、编程(10分)
1.编写一个函数void SelectSort(int a[],int n),采用选择排序的方法按升序排列数组a中的n个元素。
2。 编写一个程序,接受用户输入的角度值(以度为单位),计算并输出该角度的正弦值和余弦值。输出显示为2位小数,并将输入的角度值和程序的输出写入到文件ex04.dat中.
2004――2005 C++期末试题
一、单选题(每小题2分,共北分)
1.在每个C++程序中都必须包含有这样一·个函数,该函数的函数名为( )。
A.maln B.MAIN
C.name D.functiOn
2.设x和y均为b001量,则x&&y为真的条件是( )。
A。其中一个为假 B.其中一个为真
C.它们均为假 D。它们均为真
3.假定p是一个指向float型数据的指针,则p+1所指数据的地址比p所指数据的地址
大( )。
A.1 B.2
C.4 D.8
4.设x和y均为bool量,则x︱︱y为假的条件是( )。
A.它们均为真 B.它们均为假
C.其中一个为真 D。其中一个为假
5.假定a为一个整型数组名,则元素a[4]的字节地址为( )。
A。a+4 B.a+8 C。a十16 D.a十32
6.当使用fstream流类定义一个流对象并打开一个磁盘文件时,文件的隐含打开方式为 ( )。
A.iOS::in B.iOS::Out C.iOs::in︱iOs::Out D.没有
二、填空题(每小题2分,共24分)
1.若需要定义一个标识符常量,并且使C++能够进行类型检查,则应在定义语句的开始使用保留字——.
2.算术表达式(xy的2次除以3a)+4b一1对应的c++表达式为-—·
3.逻辑表达式x〉y&&x!=l0的相反表达式为—-。
4.逻辑表达式a〈=b︱︱b==15的相反表达式为——。
5.假定一个二维数组的定义为“char*a[5][4];",则该数组所含元素的个数为—-,所占存储空间的字节数为——。
6.变量分为全局和局部两种,—-变量没有赋初值时,将由系统自动置为o。
7。假定a是一个一维数组,则a[i]对应的存储地址(以字节为单位)为
8.假定一个结构类型的定义为“struct A{double a,b,A*c;};”,则该类型的大小为——-——-—字节。
9.假定要访问一个结构指针P所指对象中的b指针成员所指的对象,则表示方法为
10.在一个派生类中,对基类成员、类对象成员和非类对象成员的初始化次序是先——,后——,最后为—-。
11.假定用户没有给一个名为AB的类定义构造函数,则系统为其隐含定义的构造函数为——。
12.若需要把一个函数"void F();"定义为一个类AB的友元函数,则应在类AB的定义中加入一条语句
三、给出下列程序运行后的输出结果(每小题6分,共30分)
1. #include〈iostream. h〉
include<stdlib. h>
double SD(int a, int b, char op) {
double x;
switch(op) {
case’+': x= double(a)+b; break;
case’—':x=double(a)-b; break;
case'*':x=double(a)*b; break;
ease'/' ;if(b)x=double(a)/b;
else exit(l);
break;
default: exit (1);
return x;
void main() {
int x=20, y=5;
eout<〈SD(x, y,’+')<〈’’;
court.SD(x, y,'*')<<'’;
eout<〈SD(x-y,y, '/')〈〈endl; )
2. #include<iostream. h>
include<string, h>
void main() {
char*a[5]={”student", ”worker", ”cadre”, ”soldier", ”apen”};
char *pl,*p2;
pl=p2=a[0];
for(int i=l; i〈5; i++) {
if(strcmp(a[i], p1)>0) pl=a[i];
if(strcmp(a[i], p2)<0) p2=a[i];
)
cout〈〈pl〈<''〈〈p2<<endl;
3. #include<iostream。 h>
void WF(int x, int y) {
x=x+y;
y=x+y;
cout〈<"subs:"〈<”x, y="<〈x<<",”〈〈y<<endl;
void main() {
int x=8, y=l5;
cout<<”main:"<〈"x, y=”〈<x〈〈i", ”〈<y〈<endl;
WF(x, y);
X=2*X;
cout〈〈"main:"〈〈"x, y=”'<<x<〈”, ”〈<y〈〈endI; }
4。 # include〈iomanip, h〉
void LG(int*& a, int& m) {
a=new int[m]
int* p=a;
for(int i=0, i<m, i++)
*p++=i*i+l,}
void main() {
int * b, n=5,
LG(b, n);
for(int i=0; i<n; i++)
cout〈〈b[i]<〈”
cout〈〈endl;
delete[Ih; }
5。 #include<iostream。 h>
#include〈string, h>
struct Worker {
char name[15]; //姓名
int age; //年龄
float pay; //工资 };
void main() { Worker x;
char * t=”WeiRong";
int d=45; float f=1235;
strcpy(x, name, t)
x. age=d; x. pay=f;
cout〈〈x, name<<''<〈x。 age<〈’’<<x. pay〈〈endl;
四、写出下列每个函数的功能(每小题6分,共24分)
1. int SC(int a, int b, iht c) {
if(a〉b) a=b;
i~(a>c) a=c;
return al }
2. #include<iostream. h〉
template<class TT〉
TT WG(TT a, TT b) {
if(a〉b) return 1;
else if(a==b) return 0;
else return —-1; }
3。 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 0, p=f;
while(-—n) {
p=p->next = new StrNode;
cin>〉p->name;
〉 p-〉next=NULL; }
4. //struct Worker {
// char name[15]; //姓名
// iht age; //年龄
// float pay; //工资
//};
istream & operator>>(istream & istr, Worker & x) {
cout<<”请输入一个职工记录:姓名、年龄、工资、”<<endl;
istr〉>x。name>〉x。age>〉x。pay;
return istr;}
五、(10分)
1。 编一程序计算并输出12+22+...+n2的值,其中n值由键盘输入。
2. 编程将数字1到25的平方保存在顺序访问文件中
2003――2004C++期末试题
一、单选题(每小题2分,共12分)
1.设x和y均为b。。1量,则x&&y为真的条件是( ).
A.它们均为真 B.其中一个为真 C.它们均为假 D.其中一个为假
2.设x和y均为bool量,则x||y为假的条件是( ).
A.它们均为真 B.其中一个为真 C.它们均为假 D。其中一个为假
3.声明或定义一个内联函数时,必须在函数开始使用保留宇(
展开阅读全文