资源描述
姓名 学号 学院 专业 座位号
( 密 封 线 内 不 答 题 )
1. ……………………………………………………密………………………………………………封………………………………………线……………………………………线………………………………………
_____________ ________
…
诚信应考,考试作弊将带来严重后果!
《C++程序设计试卷》
注意事项:1. 考前请将密封线内填写清楚;
2. 所有答案请答在试卷的答案栏上;
3.考试形式:闭卷;
4. 本试卷共 五 大题,满分100分, 考试时间120分钟。
题 号
一
二
三
四
五
总分
得 分
评卷人
一、 单项选择题:(每题2分,共20分)
1. 结构化程序设计的三种基本控制结构是( )。
A)输入、处理、输出 B)树形、网形、环形
C)顺序、选择、循环 D)主程序、子程序、函数
2. 下列哪个是C++的合法变量名( )?
A) 8d B) ex3.12 C)1_2a D) _int
3. 若整型变量 int a=2,b=1,c=3,d=4; 则条件表达式a<b?a:c<d?c:d的值为( )。
A) 1 B) 2 C) 3 D) 4
4. for(int x=0,y=0;!x&&y<=5;y++)语句执行循环的次数是( )。
A)0 B)5 C)6 D)无限循环
5. 若一个函数体中定义与全局变量相同名字的变量x,在函数体内对x赋值,修改的是 ( )。
A)局部变量和全局变量都被修改 B) 全局变量x
C)不确定 D) 局部变量 x
6. 设有如下定义语句:int a[5],*p=a;,则下列表达式错误的是( )。
A)p++ B)a++ C)p-a D)a+1
7. 设有变量定义: double x; int m; 函数原型声明:void f(double *, int &);
则正确的函数调用语句是( )。
A) f(&x,m); B) f( x, &m); C) f(*x, &m); D) f (x,*m);
8. 若用数组名作为调用函数的实参,则传递给形参的是( )。
A) 数组存贮首地址 B) 数组的第一个元素值
C) 数组中全部元素的值 D) 数组元素的个数
9. 设有二维数组a[3][4], 不等价的一对表达是( )。
A) *(a[0]+2) 与 a[0][2] B) a[0]+3 与 &a[0][3]
C) *a[1] 与 a[1][0] D) a[0][2] 与 a[0]+2
10. 若有函数原型int max (int a , int b); 并且有:int (*p) ( int , int ) = max ;
调用max函数的正确方法是( )。
A) ( * p ) max ( a , b ) ; B) * p max ( a , b ) ;
C) ( * p ) ( a , b ) ; D) * p ( a , b ) ;
答案栏:
1、____ 2、____ 3、____ 4、____5、____ 6、____7、___ 8、____9、___ 10、____
二、 简答题:(共20分)
1. 语句 cout<< ”y\x42\\x\102\nx”的显示结果是什么?说明理由。(3分)
答:
2. 以下程序的输出结果是什么?请说明原因。(2分)
int main()
{ unsigned short a=65535;
short int b;
b=a;
cout<<”b=”<<b;
return 0;
}
答:
3. 设有说明char a[6], * b =a; sizeof(a), sizeof(b)的值各是多少?分析结果原因。(3分)
答:
4. 设有说明int a[2*3]; 请写出两个表示数组a最后一个元素地址的表达式(2分)。
答:
5. 设在主函数中有以下定义和函数调用语句,且fun函数为void类型;请写出fun函数的原型。(2分)
main()
{ double s[10][22];
int n;
┆
fun(s);
┆
}
答:
6. 设有如下定义:
struct person{char name[10]; int age;};
person class[10]={“Johu”, 17,
“Paul”, 19
“Mary”, 18,
“Adam 16,};
根据上述定义,写出能输出字母M语句。(3分)
答:
7. 以下语句不能正确输出单链表head的数据元素值,请找出原因。(2分)
struct link{int data; link * next; };
link *head, *p;
……
p=head;
while(p!=NULL) {cout<<p.data; p++; }
……
答:
8. 设有函数调用语句 array_max_min(a ,n, max, min); 功能是由参数max, min返回基本整型数组a 的n个元素中的最大值和最小值。对应的函数原型是什么?(3分)(只需给出原型,不用写函数定义)
答:
三、 阅读程序,写出运行结果:(每小题4分,共20分)
《 C++程序设计》试卷第 4 页 共 25 页
1. #include<iostream>
using namespace std;
int main ()
{ int x,n;
x=n=5;
x+=n++;
cout<<x<<n<<endl;
return 0;
}
2. #include<iostream>
using namespace std;
int main ()
{ char s[6]="abcde", *p=s;
cout<<*p<<p<<endl;
return 0;
}
3. #include <iostream>
using namespace std;
int main()
{ int i,j;
for( i=1; i<=3; i++ )
{ j=1;
while (j<i)
{ cout << i<<','<<j<<endl;
j++;
}
}
return 0;
}
4. 以下程序的输出的结果是
#include <iostream>
using namespace std;
void incre();
int x=3;
int main()
{ int i;
for (i=1;i<x;i++) incre();
}
void incre()
{ static int x=1;
x*=x+1;
cout <<x;
}
5. #include <iostream>
using namespace std;
void fun ( int , int , int * ) ;
int main ( )
{int x , y , z ;
fun ( 5 , 6 , &x ) ;
fun ( 7 , x , &y ) ;
fun ( x , y , &z ) ;
cout << x << ","<< y << "," << z ;
return 0;
}
void fun ( int a , int b , int * c )
{ b+=a ; * c=b-a ; }
答案栏:1、__________________________ 2、__________________________
3、__________________________ 4、__________________________
5、__________________________
四、 程序填空题:(每空2分,共22分)
1. 下面程序的功能是:输入三角形的三条边存放在变量a,b和c 中,判别它们能否构成三角形,若能,则判断是等边、等腰、还是其它三角形,在横线上填上适当内容。
#include <iostream>
using namespace std;
int main()
{ float a, b, c ;
cout<<"a,b,c=";
cin>>a>>b>>c;
if ( a+b>c && b+c>a && c+a>b )
{
if ( 【1】 )
cout<<"等边三角形!\n";
else if ( 【2】 )
cout<<"等腰三角形!\n";
else cout<<"其它三角形!\n";
}
else cout<<"不能构成三角形!\n";
return 0;
}
2. 以下程序功能是打印100以内个位数为6且能被3整除的所有数。
#include <iostream>
using namespace std;
int main ( )
{ int i , j ;
for ( i = 0 ; ___【3】___ ; i + + )
{ j = i * 10 + 6 ;
if ( ___【4】____ ) continue ;
cout << j << ” ” ;
}
return 0;
}
3. 下列程序实现两个变量的值互换。
#include <iostream>
using namespace std;
void swap(int *, int *);
int main()
{ int a=3,b=8;
swap(【5】 );
cout<<"a="<<a<<" b="<<b<<endl;
return 0;
}
void swap(int *x,int *y)
{ int temp= 【6】 ; *x=*y; *y=temp; }
4. 求n(n≥6)内的所有偶数表示为两个素数之和,下图为输入16的运行结果。补充完整以下程序。
[提示:一个偶数n(n≥6)可以表示为 1+(n-1),2+(n-2),3+(n-3),… ]
#include <iostream>
using namespace std;
#include<cmath>
#include<iomanip>
int isprime(int);
int main()
{ int num,i,n;
cout<<" 请输入一个偶数N(N>=6):";
cin>>num;
for( n=6; n<=num; n+=2)
for( i=3;i<=n/2;i+=2)
if(____ 【7】_________)
{cout<<setw(3)<<n<<"="<<setw(3)<<i<<" +"<<setw(3)<<(n-i)<<endl;
break;}
return 0;
}
int isprime(int m)
{ int i, k=sqrt(m);
for(i=2; i<=k; i++)
if(____ 【8】_________) return 0 ;
____ 【9】_________
}
5. 下面是一个类的测试程序,其执行结果为:
50-12=38
请将程序补充完整。
#include<iostream>
using namespace std;
class Test
{ int m,n;
public:
void init(int,int);
void print();
};
void Test::init(int i,int j)
{ ______ 【10】_______________
}
void Test::print()
{ ________ 【11】__________________
}
int main()
{ Test a;
a.init(50,12);
a.print();
return 0;
}
答案栏:
(1)__________________________ (2) __________________________
(3) __________________________ (4) __________________________
(5) __________________________ (6) __________________________
(7) __________________________ (8) __________________________
(9) __________________________ (10) _________________________
(11) _____________________________________
五、 编程题:(18分)
1. (6分)编写程序,打印正整数的平方和立方值。程序运行后显示相应的提示信息,要求输入2个正整数,然后显示这个范围的数据的平方和立方值。例如,分别输入整数1和10,执行效果如下图所示。
答:
2. (6分)以下程序求一维数组元素的最大值,并返回此值。请依题意编写函数f及填写函数原型。
#include <iostream>
using namespace std;
const int n=10;
__________ //f函数原型
int main()
{int a[n], i, max;
for(i=0; i<n; i++)
cin>>a[i];
max=f(a,n);
cout<<"max="<<max;
return 0;
}
答:
3. (6分)以下程序的功能是分离一个浮点数的整数部分和小数部分。程序用字符串存放输入数据,执行效果如图所示。根据main函数,请写出separate函数的原型和实现定义。
#include<iostream>
using namespace std;
____________________________________________ //separate函数原型
int main()
{ char s[20];
int i=0, d=0;
cout<<"请输入一个浮点数: ";
cin>>s;
separate(s, i, d); //调用函数
cout<<s<<"整数部分是:"<<i<<endl;
cout<<s<<"小数部分是:"<<d<<endl;
return 0;
}
答:
姓名 学号 学院 专业 座位号
( 密 封 线 内 不 答 题 )
2. ……………………………………………………密………………………………………………封………………………………………线……………………………………线………………………………………
_____________ ________
…
诚信应考,考试作弊将带来严重后果!
《C++程序设计试卷》
参考答案
注意事项:1. 考前请将密封线内填写清楚;
2. 所有答案请答在试卷的答案栏上;
3.考试形式:闭卷;
4. 本试卷共 五 大题,满分100分, 考试时间120分钟。
题 号
一
二
三
四
五
总分
得 分
评卷人
六、 单项选择题:(每题2分,共20分)
答案栏:
1、__C__ 2、_D_ 3、_C__ 4、_C__5、_D_ 6、__B_7、_A_ 8、A__9、D_ 10、__C__
七、 简答题:(共17分)
9. yB\xB
x
10. b=-1
11. 6,4
12. &a[5], a+5
13. void fun(double (*p)[22]);
14. class[2].name[0]
15. p->data; p=p->next;
void array_max_min(int *, int , int *,int *);
八、 阅读程序,写出运行结果:(每小题4分,共20分)
《 C++程序设计》试卷第 25 页 共 25 页
6. 106
7. aabcde
8. 2,1
3,1
3,2
2,6
6,6,6
九、 程序填空题:(每空2分,共22分)
1. a= =b && b= =c
2. a= =b || a= = c || b= =c
3. i <10
4. j % 3
5. &a, &b
6. *x
7. isprime(i) && isprime(n-i)
8. !( m%i)
9. return 1;
10. m=i;n=j;
11. cout<<m<<"-"<<n<<"="<<m-n<<endl;
十、 编程题:(18分)
1. #include <iostream>
using namespace std;
#include<iomanip>
int main()
{ int a,b,i;
cout<<" 请输入第一个整数(>=0):";
cin>>a;
cout<<" 请输入第二个整数(>第一个整数):";
cin>>b;
cout<<setw(10)<<"N"<<setw(10)<<"平方"<<setw(10)<<"立方"<<endl;
for( i=a;i<=b;i++)
cout<<setw(10)<<i<<setw(10)<<i*i<<setw(10)<<i*i*i<<endl;
return 0;
}
2. #include <iostream>
using namespace std;
const int n=10;
int f(int *, int); //f函数原型
int main()
{int a[n], i, max;
for(i=0; i<n; i++)
cin>>a[i];
max=f(a,n);
cout<<"max="<<max;
return 0;
}
int f(int *a,int n)
{int i,max;
max=a[0];
for(i=1;i<n;i++)
if(a[i]>max) max=a[i];
return max;
}
3. #include<iostream>
using namespace std;
void separate(char *,int & ,int &); //separate函数原型
int main()
{ char s[20]={'\0'};
int i=0, d=0;
cout<<"请输入一个浮点数: ";
cin>>s;
separate(s, i, d); //调用函数
cout<<s<<"整数部分是:"<<i<<endl;
cout<<s<<"小数部分是:"<<d<<endl;
return 0;
}
void separate(char *s, int &a, int &b)
{int i;
for(i=0;s[i]!='.';i++)
a=a*10+s[i]-48;
for(i++;s[i]!=0;i++)
b=b*10+s[i]-48;
}
姓名 学号 学院 专业 座位号
( 密 封 线 内 不 答 题 )
3. ……………………………………………………密………………………………………………封………………………………………线……………………………………线………………………………………
_____________ ________
…
诚信应考,考试作弊将带来严重后果!
期末考试
《C++程序设计》试卷
注意事项:1. 考前请将密封线内填写清楚;
2. 所有答案请答在试卷的答案栏上;
3.考试形式:闭卷;
4. 本试卷共 五 大题,满分100分, 考试时间120分钟。
题 号
一
二
三
四
五
总分
得 分
评卷人
十一、 单项选择题:(每小题2分,共20分)
11. 一个C++程序从编写源程序到得到运行结果要经历的四个步骤是( )。
A)编译、编辑、连接、运行
B)编辑、编译、汇编、运行
C)编辑、编译、连接、运行
D)编译、连接、汇编、运行
12. 下列选项中,哪个是C++语言的合法变量名( )?
A) file.cpp B) _123 C)9abc D) new
13. 对于语句序列:int x=5,y=6,z; float a=2.5; z=y/x+y/a+a; 执行后z的值为( )。
A) 5 B) 6 C) 6.1 D) 5.9
14. 以下选项中可作为C语言合法常量的是( )。
A)-8e1.0 B)-080 C)-80. D)-80.0e
15. 设有int a=1,b=2,c=3,d=4; 则条件表达式a<b?a:c<d?c:d的值为( )。
A) 1 B) 2 C) 3 D) 4
16. 以下程序的运行结果是( )。
int main()
{ unsigned short a=65535;
short b;
b=a;
cout<<b;
return 0;
}
A) -32768 B) 32767 C) 65535 D) -1
17. 设有定义语句: int a[6] = {2,4,6,8,5,7}, *p = a, i ;
要求依次输出a数组6个元素的值,不能完成此操作的语句是( )。
A) for(i=0;i<6;i++)cout<<*(p++)<<endl;
B) for(i=0;i<6;i++) cout<<*(p+i)<<endl;
C) for(i=0;i<6;i++) cout<<*p++<<endl;
D) for(i=0;i<6;i++) cout<<(*p)++<<endl;
18. 设有定义语句:char a[10], *p=a; 以下赋值语句中,正确的是( )。
A) a="hello"; B) a[10]= "hello";
C) strcpy(a, "hello"); D) *p="hello";
19. 下列带有默认参数的函数原型声明中,正确的是( )。
A) float fun(int a=5 , int b=5, int c, int d);
B) float fun(int a=5, int b=5, int c, int d=5);
C) float fun(int a=5, int b , int c , int d );
D) float fun(int a ,int b , int c=5, int d=5);
20. 设有定义语句:struct point{ int x; int y; } v1; 则以下语句中正确的是( )。
A) point.x=10; B) point v2; v2.x=10;
C) struct v2; v2.x=10; D) v1 v2; v2.x=10;
答案栏:
1、____ 2、____ 3、____ 4、____5、____ 6、____7、___ 8、____9、___ 10、____
二、程序填空题(每空2分,共20分)
答案栏:
1、___________________________________ 2、___________________________________
3、___________________________________ 4、__________;___________;____________;_
5、__________________;_______________;_ 6、___________________________________
7、___________________________________ 8、___________________________________
9、___________________________________ 10、___________________________________
1、以下函数的功能是:用递归法将一个整数n转换成字符串。例如:输入整数1635,应输出字符串"1635"。n的位数不确定,可以是任意位数的整数。请填空。
void fun(int n)
{ if(n<10) 【1】 //递归出口
else
{ 【2】 //递归体
putchar(n%10+48);
}
}
2、以下程序的功能是:用起泡法对6个整数排序(按从小到大顺序)。请填空。
#include <iostream>
using namespace std;
void main( )
{
int a[6], i, j, t;
for(i=0; i<6; i++)
cin>>a[i];
for(j=0; j<5; j++)
for(i=0; 【3】 ; i++)
if(a[i]>a[i+1])
{
【4】 //交换两个数的位置,使小数上浮
}
for(i=0; i<6; i++)
cout<<a[i]<<" ";
}
3、以下程序的功能是:定义一个结构体类型,内含学生学号和一门课的成绩,并开辟动态内存存放一个学生的数据:学号1001,成绩90。执行该程序后运行结果如下:
1001, 90↙
请填空。
#include<iostream>
using namespace std;
struct Student
{
int n; //学号
double s; //成绩
};
void main( )
{
Student *p;
p=new Student;
【5】 //赋值
【6】 //输出
delete p;
}
4、以下程序的功能是:执行该程序后运行结果如下:
43↙
请填空。
#include<iostream>
using namespace std;
void f1( 【7】 )
{
x=x+2;
y=y+2;
}
void main( )
{
int a=2, b=3;
f1(a,b);
cout<<a<<b<<endl;
}
5、以下程序的功能是:输出二维数组各元素值。请填空。
#include<iostream>
using namespace std;
void main( )
{
void output(int (*)[3]);
int a[2][3]={1, 2, 3, 4, 5, 6};
output( 【8】 );
}
void output( int (*p)[3] )
{
int i, j;
for(i=0; i<2; i++)
{
for(j=0; j<3; j++)
cout<< 【9】 <<" ";
cout<<endl;
}
}
6、以下函数的功能是:判断一个数是否为素数。请填空。
bool prime(int m)
{
int i;
bool prime1;
prime1=true;
for(i=2; i<=sqrt(m); i++)
if(m%i==0) 【10】
return prime1;
}
三、阅读程序,写出运行结果。(每小题3分,共24分)
答案栏:
1、_____________________________ 2、_____________________________
3、_____________________________ 4、_____________________________
5、_____________________________ 6、_____________________________
7、_____________________________ 8、_____________________________
1、#include<iostream>
using namespace std;
void main( )
{
int i=3, j=6, m, n;
m=i++ - ++j;
n=++i+j--;
cout<<m<<' '<<n<<' '<<i<<' '<<j;
}
2、#include <iostream>
using namespace std;
void main( )
{
double x=-0.5, y;
if(x<1)
if(x<-1)y=x+1;
else if(x>0)y=x+2;
else y=x+4;
else y=x+5;
cout<<"x="<<x<<",y="<<y;
}
3、#include <iostream>
using namespace std;
void main( )
{
int x=2, y=1;
while(x<4)
switch(x++)
{
case 3: y++;
case 1: y+=3;break;
case 2: y--;
}
cout<<y;
}
4、
#include <iostream>
using namespace std;
void p(float f)
{
cout<<"float"<<f;
}
void p(double d)
{
cout<<"double"<<d;
}
void p(int i)
{
cout<<"int"<<i;
}
void main()
{
p(2);
p('A');
p(2.1);
}
5. #include <iostream>
using namespace std;
int x, y;
void fun(int a)
{ int y , b;
b=a+2; x=x+a; y=x-b;
cout<<x<<" "<<y<<" "<<a<<" "<<b<<endl;
}
int main( )
{ int a=1, b=2;
x=3, y=4;
fun(b);
cout<<x<<" "<<y<<" "<<a<<" "<<b<<endl;
return 0;
}
6. #include <iostream>
using namespace std;
int f(int a)
{ int b=0;
static int c=3;
b++; c++;
return (a+b+c);
}
int main( )
{ for (int i=0; i<3; i++) cout<<f(i)<<" " ;
return 0;
}
7. #include <iostream>
using namespace std;
int main( )
{ char ch[ ]="STUDY" ;
cout<<ch<<endl<<ch[2]<<e
展开阅读全文