《C语言》自测题B答案
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
电子科技大学网络教育考卷(B 卷)
(20 年至20 学年度第 学期)
考试时间 年 月 日(120分钟) 课程 C 语言 教师签名_____
一、单项选择题(每小题1分,共30分)
1、以下程序的运行结果是:________②_________。
#include "stdio.h" #include "string.h" void main() {char s[]="abcd";
printf("%d,%d\n",strlen(s),sizeof(s)); }
①5,4 ②4,5 ③4,4 ④5,5
2、以下程序的运行结果是:________③_________。
#include "stdio.h" #include "string.h" void main() {char s[20]="abcd";
printf("%d,%d\n",sizeof(s),strlen(s)); }
①4,5 ②20,20 ③20,4 ④4,20
3、若变量已正确说明为int 类型,要给a 、b 、c 输入数据,以下正确的输入语句是:_____④_____ ①read(a,b,d); ②scanf("%d%d%d",a,b,c) ③scanf("%D%D%D",&a,&b,&c); ④scanf("%d%d%d",&a,&b,&c);
4、以下程序段的输出是:_____③_____ printf("|%10.5f|\n",12345.678);
①|1234.67800| ②|12345.6780| ③|12345.67800| ④|12345.678| 5、在C 语言中,要求运算数是整型的运算符是_____①_____ ①% ②/ ③!= ④*
6、有输入语句:scanf("a=%d, b=%d, c=%d", &a, &b, &c); 为使变量a,b,c 的值分别为1,2,3,从键盘上输入数据的正确形式应当是_____②_____
①123〈回车〉 ②a=1,b=2,c=3〈回车〉 ③a=1 b=2 c=3 <回车> ④1,2,3 〈回车〉
7、int x=3; if(1<=2)if(0)x=4;else x=5;else x=6; 执行上述语句后x 的值是_____③_____ ①3 ②4 ③5 ④6
8、在C 语言中表达式(2/5+3/5)的值是_________①_________。
①0 ②0.4 ③0.6 ④1
9、设int a=2, b=3, c; c=(a*=b); 则执行上述语句后c 的值是_________④_________。
①2 ②3 ③5 ④6 10、若有代数式
xy
x 3
2 , 则以下能够正确表示该代数式的C 语言表达式为:________④__________。
①2x+3/xy ②2*x+3/x*y ③(2*x+3)/x*y ④(2*x+3)/x/y
11、int a, b, c=5; a=c++; b=c; 执行上述语句后,则b 的值为_________③_________。
①4 ②5 ③6 ④7
12、已知:int a[][3]={1,2,3,4,5,6,7,8,9}; 此时数组元素a[2][1]的值为__________④________。
①4 ②5 ③7 ④8
13、以下叙述正确的是_____③_____ ①goto 语句只能用于退出多层循环。
②可以用continue 语句来终止switch 语句的执行。
③break 语句只能用在循环体内和switch 语句体中。
④在循环内使用break 语句和continue 语句的作用相同。
14、以下对二维数组a 的正确说明是_____③_____
①int a[3][ ]; ②float a(3,4); ③int a[3][4]; ④float a(3)(4); 15、字符串"uestc"在内存占用_________②_________字节。
①5 ②6 ③10 ④12
16、程序段double x=12.3456789; printf("x=%-10.2f",x); 执行后输出的结果是_________③_________。
(用大写字母U 表示空格)
①x=12.34UUUUU ②x=UUUUU12.34 ③x=12.35UUUUU ④x=UUUUU12.35 17、有以下程序:#include "stdio.h" #include "stdlib.h" void main() {char *p,*q;
p=(char *)malloc(20*sizeof(char));q=p; scanf("%s %s",p,q);printf("%s %s\n",p,q); }
若从键盘输入:abc def<回车>,则输出结果是:______①______。
姓名__________________ 专业名称__________________ 班号________________学号__________________教学中心_________________
……………… …… … … … … …密………………… …… … … … … ……封……………… …… … … …线… … … …… … … ……………………
①def def ②abc def ③abc d ④d d
18、以下程序的输出结果是:_____③_____。
#include <stdio.h>
void f(int n)
{if(n==0)return;
f(n-1);
printf("%d",n);
}
void main()
{f(5);printf("\n");}
①1234 ②4321 ③12345 ④54321
19、int x; x=2>3?4:5<=6?7:8; 则执行上述语句后x的值为_________③_________。
①0 ②4 ③7 ④8
20、int a=2,b=3; a*=a+b; 则执行上述语句后a的值为_________③_________。
①5 ②7 ③10 ④15
21、设有定义:double x=4.9;则表达式(int)(x)%3/4的值是_________④_________。
①4.9 ②0.5 ③0.25 ④0
22、设变量x为float型且已经赋值,则以下语句中能够将x中的数值保留到小数点后面两位,并将第三位四舍五入的是:______③______。
①x=x*100+0.5/100.0 ②x=(x*100+0.5)/100.0
③x=(int)(x*100+0.5)/100.0 ④x=(x/100+0.5)*100.0
23、设有定义:float a=2,b=4,h=3;,以下C语言表达式中与代数式(a+b)h/2计算结果不相符的是:_____②
_______。
①(a+b)*h/2 ②(1/2)*(a+b)*h ③(a+b)*h*1/2 ④h/2*(a+b)
24、有以下程序段:
int k=0;
while(k=1)k++;
while循环执行的次数是_________①________。
①无限次②有语法错,不能执行
③一次也不执行④执行1次
25、以下程序的输出结果是:________①_________。
#include "stdio.h"
void main()
{printf("%d\n",NULL);}
①0 ②1 ③-1 ④NULL没定义,出错
26、以下程序的运行结果是:________②_________。
#include "stdio.h"
typedef struct
{char x,y;
}data;
void main()
{data m={'A','B'};
printf("%c%c\n",m.x,m.y);
}
①AA ②AB ③BA ④BB
27、设有如下程序段:int x=2008, y=2009;printf("%d\n",(x,y));
则以下叙述中正确的是______④______。
①输出语句中格式说明符的个数少于输出项的个数,不能正确输出
②运行时产生出错信息
③输出值为2008
④输出值为2009
28、以下程序的输出结果是:_________①_________。
#include "stdio.h"
#include "string.h"
void main()
{char s[]="abcd",t[10]="abcd";
printf("%d,%d,%d,%d\n",sizeof(s),sizeof(t),strlen(s),strlen(t));
}
①5,10,4,4 ②5,5,4,4 ③4,4,5,10 ④4,4,4,10
29、一个C程序的执行是从_____①_____
①本程序的main函数开始,到main函数结束。
②本程序的第一个函数开始,到本程序最后一个函数结束。
③本程序的main函数开始,到本程序文件的最后一个函数结束。
④本程序的第一个函数开始,到本程序main函数结束。
30、下面的四个选项中,不合法的用户自定义标识符是_____③_____
①Do ②_123 ③2ab ④INT
二、判断题(正确的划√,错误的划×,每小题1分,共10分)
( × )1、在同一个数组中可以存储许多不同类型的值。
( × )2、C语言中,变量名用大小写是等价的。
( × )3、在C语言程序中,不允许变量同名。
( × )4、switch选择结构中必须有default子句。
( × )5、强制类型转换,如(double)x,只改变原变量的类型,而不改变其值。
( × )6、C 语言的所有循环语句都要求至少要执行一次循环体。
( × )7、在一个switch 开关语句中可以有一个或多个default 子句。
( × )8、一个函数float div(float x,float y),其函数原型说明为float div(float x,float y)。
( √ )9、以下程序的运行结果为:555 #include "stdio.h"
void main(){int x=5,* p,* * q;p=&x;q=&p;printf("%d%d%d\n",x,*p,**q);} ( √ )10、C 语言中,注释可以出现在源程序中空格可以出现的任何位置。
三、程序填空题(请在横线上填入合适的语句,将程序补充完整。
每小题5分,共15分) 1、用迭代法求a x =。
求平方根的迭代公式为⎪⎪⎭
⎫
⎝⎛+=+n n n x a x x 211。
要求前后两次求出的x 的差的绝对值小于10-6。
#include <stdio.h> #include <math.h> void main() {float a,x0,x1;
printf("Enter a positive number:"); scanf("%f",&a); x0=a/2; x1=(x0+a/x0)/2; do {x0=x1; x1=(x0+a/x0)/2;
}while (________________fabs(x0-x1)>=1e-6________________); printf("The square root of %5.2f is %9.6f\n",a,x1); }
2、从键盘读入两个数,求其最大者。
#include "stdio.h" int max(int,int); void main() {int a,b,c;
printf("a=");scanf("%d",&a); printf("\nb=");scanf("%d",&b); c=max(a,b);
printf("\nmax is %d\n",c); }
int max(int x,int y)
{____________________return x>y?x:y;_____________________________ }
3、从键盘读入一个字符串,求该字符串的长度。
#include <stdio.h> int len(char *); void main() {char str[80];
printf("Please input a string:"); scanf("%s",str);
printf("The length of the string is %d.\n",len(str)); }
int len(char * p) {int n=0; while(*p!='\0') {n++;
____________________p++;____________________ } return (n); }
四、程序分析题(请将程序运行后的输出结果写在横线上。
每小题5分,共25分) 1、#include <stdio.h> void main()
{int a[3][3]={{3,2,2},{4,5,6},{-2,9,2}}; int i,j,s=0; for(i=0;i<3;i++) for(j=0;j<3;j++)
if(i==2-j) s=s+a[i][j]; printf("s=%d\n",s); }
运行结果:_______________________s=5____________________ 2、#include <stdio.h> int f(int n) {switch(n) {case 0:
case 1:return 1; }
return n+f(n-1)+f(n-2);
}
void main()
{printf("%d\n",f(3));
}
运行结果:_____________8_____________ 3、#include <stdio.h>
int f(int a)
{auto int b=0;
static int c=3;
b++;
c++;
return (a+b+c);
}
void main()
{int a=2,i;
for (i=0;i<3;i++)
printf("%d", f(a));
printf("\n");
return;
}
运行结果:___________789_______________ 4、#include <stdio.h>
void main()
{FILE *fp;
char ch1='A',ch2='B',ch3,ch4;
fp=fopen("d.txt","w");
fputc(ch1,fp);
fputc(ch2,fp);
fclose(fp);
fp=fopen("d.txt","r");
ch3=fgetc(fp);
ch4=fgetc(fp);
printf("%c,%c\n",ch3,ch4);
fclose(fp);
} 运行结果:______________A,B____________________
5、#include "stdio.h"
void main()
{unsigned char a,b;
a=4|3;
b=4&3;
printf("%d,%d\n",a,b);
}
运行结果:__________________7,0_______________
五、编程题(每小题10分,共20分)
1、从键盘读入一批整数,当读到0时结束,统计其中正数和负数的个数。
#include <stdio.h>
void main()
{int x,m=0,n=0;
printf("Enter the number:");
scanf("%d",&x);
while(x!=0)
{if(x>0)m++;
else n++;
scanf("%d",&x);
}
printf("\nm=%d,n=%d\n",m,n);
}
2、百鸡问题:鸡翁一,值钱五;鸡母一,值钱三;鸡雏三,值钱一。
百钱买百鸡,问鸡翁、母、雏各多少?答:方法一:
#include <stdio.h>
void main()
{int i,j,k;
for(i=0;i<=100;i++)
for(j=0;j<=100;j++)
for(k=0;k<=100;k++)
if(i+j+k==100&&15*i+9*j+k==300)
printf("i=%d,j=%d,k=%d\n",i,j,k);
}
方法二:
#include <stdio.h>
void main()
{int i,j,k;
for(i=0;i<=100;i++)
for(j=0;j<=100;j++)
{k=100-i-j;
if(15*i+9*j+k==300&&k>=0)
printf("i=%d,j=%d,k=%d\n",i,j,k); }
}。