C语言试卷与答案

申明敬告: 本站不保证该用户上传的文档完整性,不预览、不比对内容而直接下载产生的反悔问题本站不予受理。

文档介绍

C语言试卷与答案

1999~2000学年“程序设计基础--C”试卷考试时间:2000年6月23日上午8:30-10:30注:请将答案写在答卷纸上试题一、单项选择题(每小题2分,共20分)1.C语言程序的三种基本结构是顺序结构、选择结构和______结构。A、循环B、递归C、转移D、嵌套2.若x是单精度实型变量,表达式(x=10/4)的值是_____。A、2.5B、2.0C、3D、23.表达式!x等价于_____。A、x==0B、x==1C、x!=0D、x!=14.若变量已正确定义且k的值是4,计算表达式(j=k--)后,_____。A、j=3,k=3B、j=3,k=4C、j=4,k=4D、j=4,k=35.算术运算符、赋值运算符和关系运算符的运算优先级按从高到低的顺序依次为_____。A、算术运算、赋值运算、关系运算B、关系运算、赋值运算、算术运算C、算术运算、关系运算、赋值运算D、关系运算、算术运算、赋值运算6.C语言中,形参的缺省的存储类说明是______。A、autoB、staticC、registerD、extern7.下列语句定义pf为指向float类型变量f的指针,_____是正确的。A、floatf,*pf=f;B、floatf,*pf=&f;C、float*pf=&f,f;D、floatf,pf=f;8.表达式sizeof(“key”)的值是______。A、1B、2C、3D、49.将程序prog.c编译后运行:prog–nlahelloworld则*(*(argv+2))是______。A、’p’B、‘-‘C、‘h’D、‘w’10.若a是整型变量,表达式~(a^~a)等价于______。A、~aB、1C、0D、2 试题二、填空题(每小题3分,共24分)1.写出语句printf("%d,%o,%x",0x12,12,012);的输出结果。 2.写出下列程序段的输出结果。#defineF(x)x-2#defineD(x)x*F(x)printf("%d,%d",D(3),D(D(3))); 3.写出下列程序段的输出结果。intk;char*s=“ABC”;for(k=10;k!=0;k--);printf("%d",k);while(*s++)putchar(*s);4.写出下列程序的输出结果。voidfun(intk){staticinta;printf(”%d,”,a);a+=k;}main()n{intk;for(k=1;k<=3;k++)fun(k);} 5.写出下列程序的输出结果。main(){structst{intx,y;}a[4]={{10,20},{30,40},{50,60},{70,80}};structst*p=a;printf("%d,",p++->x);printf("%d,",++p->y);printf("%dn",(a+3)->x);} 6.写出下列程序的输出结果。#include"stdio.h"voidmain(){intk,j,m,n,a[6][6],b[6];m=3,n=2;for(k=0;klongfib(intg){switch(g){case0:return(0);case1:case2:return(2);}printf("g=%d,",g);return(fib(g-1)+fib(g-2));}main(){longk;k=fib(4);printf("k=%ldn",k);}n8.用typedef定义一个长度为10的整型数组NUM。 试题三、程序阅读题(每题5分,共20分)1.写出下列程序的输出结果。#includeintx,y,z;voidp(int*x,inty){--*x;y++;z=*x+y;printf("%d,%d,",*x,y);}voidmain(){x=5;y=2;z=0;p(&x,y);printf("%d,%d,%d--",x,y,z);p(&y,x);printf("%d,%d,%dn",x,y,z);}2.写出下列程序的输出结果,并指出该程序的功能。#includevoidmain(){intx,k;x=14;for(k=0;x!=0;x&=x-1)++k;printf("%dn",k);}3.写出下列程序的输出结果。#includevoidmain(){intv[]={6,3,4,1,2};intn=5;intgap,j,k,temp;for(gap=n/2;gap>1;gap/=2)for(j=gap;j=0&&v[k]>v[k+gap];k-=gap){temp=v[k];v[k]=v[k+gap];v[k+gap]=temp;}for(k=0;k<5;k++)printf("%d,",v[k]);}4.输入123450后,写出下列程序的输出结果。#defineLENsizeof(structline)#defineNULL0structline{intnum;structline*next;}main(){structline*p1,*p2,*head;intj,k=0;p1=p2=head=(structline*)malloc(LEN);scanf("%d",&p1->num);nwhile(p1->num!=0){p1=(structline*)malloc(LEN);scanf("%d",&p1->num);if(p1->num==0)p2->next=NULL;else{p2->next=p1;p2=p1;}k++;}p2->next=head;p1=head->next;p1=p1->next;for(j=1;j<=k;j++){printf("-->%d",p1->num);p1=p1->next;}} 试题四、程序填空题(每空2分,共20分) 1.输入10个点的坐标(设坐标为整数值),输出距原点最远的点(设唯一)的坐标及该点距原点的距离.#includevoidmain(){struct{intx,y;floatlength;}point[10];intk,sub=0;for(k=0;k<10;k++){scanf("%d%d",&point[k].x,&point[k].y);point[k].length=(1)}for(k=1;k<10;k++)if((2))(3)printf("(%d,%d)%fn",point[sub].x,point[sub].y,point[sub].length);}2.将输入的n个字符串连接成1个字符串,然后输出该字符串.#include#includevoidmain(){charstr[10][20],string[200];intk,n;scanf("%d",&n);for(k=0;k=’0’&&s[k]<=’9’||s[k]>=’a’&&s[k]<=’f’){if(s[k]>='0'&&s[k]<='9')(7)if(s[k]>='a'&&s[k]<='f')(8)k++;}returnd;} 4.函数month_day(year,yearday,&month,&day)的功能是将给定的某年第几天转换为相应的月和日。例如:调用month_day(1988,60,&m,&d)后,m的值是2,d的值是29(即1988年的第60天是1988年2月29日)。判别闰年的条件:能被4整除但不能被100整除或能被400整除。voidmonth_day(intyear,intyearday,int*pmonth,int*pday){staticintday_tab[2][13]={{0,31,28,31,30,31,30,31,31,30,31,30,31},{0,31,29,31,30,31,30,31,31,30,31,30,31}};intk,leap;leap=year%4==0&&year%100!=0||year%400==0;for(k=1;yearday>day_tab[leap][k];k++)(9)*pmonth=(10)*pday=yearday;} 试题五、编程题(共16分)1.[程序说明](5分)请编写函数sum_fact(number),返回number的因子之和。(能整除number的数就是它的因子,number定义为int)例如sum_fact(6)=12(因为1,2,3,6是6的因子) 2.[程序说明](11分)文本文件input.txt中存放了一批整数,将其中每个数的因子之和顺序写入文件output.txt。(必须调用第1小题中定义的函数sum_fact(number)计算number的因子之和)例:若文件input.txt中存放了1,2,6,则每个数的因子之和分别是1312,它们被写入文件output.txt。 n1999~2000学年下《程序设计基础—C》答卷下框由教师填写:题号一二三四五总分得分      阅卷人      试题一、选择题(每小题2分,共20分)题号12345678910选择ABADCABDCC试题二、填空题(每小题3分,共24分)118,14,A27,-1330BC40,1,3,510,41,7063,7,11,7g=4,g=3,k=68typedefintNUM[10]试题三、程序阅读题(每小题5分,共20分)14,3,4,2,7—1,5,4,1,623统计二进制数中1的数目32,1,4,3,6,(4,1,2,3,6,j)4à3à4à5à1à2试题四、程序填空题(每空2分,共20分)(1)sqrt(point[k].x*point[k].x+point[k].y*point[k].y);(2)point[k].length>point[sub].length(3)sub=k;(4)strcpy(string,str[0]);(5)strcat(string,str[k]);(6)chars[]或char*s(7)d=d*16+s[k]-'0';(8)d=d*16+s[k]-'a'+10;(9)yearday-=day_tab[leap][k];(10)k试题五、编程题(第1题5分,第2题11分,共16分)n1、intsum_fact(intnumber)定义、变量说明、返回2分{intk,sum=0;if(number<0)number=-number;for(k=1;k<=number;k++)1分if(number%k==0)sum+=k;2分returnsum;}2、#include头文件、变量说明1分main(){FILE*fpin,*fpout;intx;if((fpin=fopen("input.txt","r"))==NULL){2分printf("Can'topenfileinput.txtn");exit(0);}if((fpout=fopen("output.txt","w"))==NULL){2分printf("Can'tcreatfileoutput.txt");exit(0);}while(!feof(fpin)){1分fscanf(fpin,"%d",&x);2分fprintf(fpout,"%d",sum_fact(x));2分}fclose(fpin);关闭文件1分fclose(fpout);}2000~2001学年《程序设计基础-C》试卷考试时间:2000年12月31日上午8:30-10:30注:请将答案写在答卷上 一、单项选择题(每小题2分,共30分)1.1.        C语言程序中可以对程序进行注释,注释部分必须用符号_____括起来。A、‘{‘和’}’B、‘[‘和’]’C、“/*”和”*/”D、“*/”和”/*”2.2.        下列运算符中,优先级最低的是_____。A、*B、!=C、+D、=3.3.        若变量已正确定义,表达式(j=3,j++)的值是_____。A、3B、4C、5D、04.4.        已知字符‘a’的ASCII码为97,执行下列语句的输出是_____。printf("%d,%c",’b’,’b’+1);nA、98,bB、语句不合法C、98,99D、98,c1.5.        表达式strcmp(“box”,“boss”)的值是一个_______。A、正数B、负数C、0D、不确定的数2.6.        数组定义为inta[3][2]={1,2,3,4,5,6},数组元素_____的值为6。A、a[3][2]B、a[2][1]C、a[1][2]D、a[2][3]3.7.        要调用数学函数时,在#include命令行中应包含_____。A、”stdio.h”B、”string.h”C、”math.h”D、”ctype.h”4.8.        判断i和j至少有一个值为非0的表达式是_____。A、i!=0&&j!=0B、i*j!=0[yh1]C、!(i==0||j==0)D、i&&j5.9.        若a是基本整型变量,c是单精度实型变量,输入语句______是错误的。A、scanf(”%d,%f”,&a,&c);B、scanf(”d=%d,c=%f”,&a,&c);C、scanf(”%d%f”,&a,&c);D、scanf(”%d%f”,a,c);6.10.    若变量已正确定义并且指针p已经指向某个变量x,则(*p)++相当于_____。A、p++B、x++C、*(p++)D、&x++7.11.    若p1、p2都是整型指针,p1已经指向变量x,要使p2也指向x,_____是正确的。A、p2=p1;B、p2=**p1;C、p2=&p1;D、p2=*p1;8.12.    下列程序段的输出是_____。intc[]={1,3,5};int*k=c+1;printf("%d",*++k);A、3B、5C、4D、69.13.    不正确的赋值或赋初值的方式是______。A、charstr[]="string";B、charstr[7]={'s','t','r','i','n','g'};C、charstr[10];str="string";D、charstr[7]={'s','t','r','i','n','g',’’};10.14.    对于以下结构定义,++p->str中的++加在_____。struct{intlen;char*str;}*p;A、指针str上B、指针p上C、str指的内容上D、以上均不是11.15.    对于如下说明,语法和语义都正确的赋值是_____。intc,*s,a[]={1,3,5};A、c=*s;B、s[0]=a[0];C、s=&a[1];D、c=a; 二、填空题(每小题2分,共20分)1.1.        写出下列程序段的输出结果。floatx1,x2;x1=3/2;x2=x1/2;printf("%d,%.1f",(int)x1,x2);2.2.        表达式(7<<1>>2^2)的值是_______。3.3.        写出下列程序段的输出结果。#defineA10n#defineB(A0)?2*f(n-1)+f(n-2):-1);}5.8.        写出下列程序段的输出结果。charstr[]="hellotworldn";printf("%d,%cn",sizeof(str),*(str+10));6.9.        输入12345#后,写出下列程序的输出结果。voidmain(){charc;for(c=getchar();getchar()!=’#’;c=getchar())putchar(c);}7.10.   执行程序find–nxouldtext.txt时,*++argv[1]的值是_______。三、程序阅读题(每题5分,共20分)1.1.        若输入3123<回车>,则以下程序的输出结果是_______。#include"stdio.h"voidmain(){inti,j,n,a[10];scanf("%d",&n);for(i=0;i,则以下程序的输出结果是_______。#include"stdio.h"#defineN10main(){intx[N],y1[N],y2[N];inti,j,n1,n2,t,p;n1=n2=0;for(i=0;iy1[j+1]){t=y1[j];y1[j]=y1[j+1];y1[j+1]=t;}for(i=0;iintz;voidp(int*x,inty){++*x;y--;z=*x+y+z;printf("%d,%d,%d#",*x,y,z);}voidmain(){intx=1,y=5,z=9;p(&x,y);printf("%d,%d,%d#",x,y,z);}2.4.        若输入-6+15*3/5=<回车>,则以下程序的输出结果是_______。#includevoidmain(){intm=0,sum=0;charc,oldc='+';do{c=getchar();if(c<='9'&&c>='0')m=10*m+c-'0';else{if(oldc=='+')sum+=m;elsesum-=m;m=0;oldc=c;printf("%d,",sum);n}}while(c!='=');} 四、程序填空题(每空2分,共20分)1.1.        下列程序的功能是创建单向链表。#include#includestructlink{charname[10];intmark;structlink*next;};voidinsert(char*,int);structlink*head=NULL;main(){charname[10];intmark;structlink*t;while(1){scanf("%s%d",name,&mark);if(strcmp(name,"#")==0)break;______(1)_______;}for(t=head;______(2)_______)printf("<%s>:%dn",t->name,t->mark);}voidinsert(char*name,intmark){structlink*p;p=______(3)_______;strcpy(p->name,name);p->mark=mark;______(4)_______;if(head!=NULL)______(5)_______;head=p;}2.2.        prog.c是带命令行参数的程序。运行progfilename时,将文本文件filename中的内容原样输出;运行prog-ufilename时,将文本文件filename中的所有字母以大写方式输出(其余字符原样输出);运行prog-dfilename时,只将文本文件filename中的数字输出。#include#includevoidmain(intargc,char*argv[]){FILE*fp;char*filename,ch;inttag=0;if(argc<2||argc>3){printf(“Argumenterrorn”);return;}if(argc==3){if(strcmp(argv[1],“-u”)==0)tag=1;elseif(strcmp(argv[1],“-d”)==0)tag=2;nelse{printf(“Optionerrorn”);return;}______(6)_______;}elsefilename=argv[1];if(______(7)_______){printf(“Cannotopenfilen”);return;}while(______(8)_______){ch=fgetc(fp);if(tag==0)putchar(ch);elseif(tag==1)______(9)_______;elseif(______(10)_______)putchar(ch);}fclose(fp);} 五、编程题(共10分,混合班学生做第2题,其他学生做第1题)1.1.        [程序说明](10分)输入m和n(m≥n≥0)后,计算下列表达式的值并输出。m!n!*(m-n)!要求将计算阶乘的运算写成函数fact(n),函数返回值的类型为float。 2.2.        [程序说明](10分)编写函数strcmp(char*s1,char*s2)。若s1和s2均是数字串(包括+/-号),则按照十进制整数大小进行比较;否则按照Ascii序进行比较。s1大于/等于/小于s2时,分别输出1/0/-1。可以直接调用atoi函数。2000~2001学年《程序设计基础C》参考答案 一、单项选择题(每小题2分,共30分)题号123456789101112131415选择CDADABCBDBABCAC二、填空题(每小题2分,共20分) 11,0.5213-34c>=’a’&&c<=’z’||c>=’A’&&c<=’Z’55,1060,-17-17813,d913510‘n’三、程序阅读题(每题5分,共20分) n11,2,3,2,3,1,3,1,2,24,8,16,24,76,89,35,25,11,9,32,4,6#2,5,9#40,-6,9,6,1,四、程序填空题(每空2分,共20分) (1)(1)        insert(name,mark)(2)(2)        t!=NULL;t=t->next(3)(3)        (structlink*)malloc(sizeof(structlink))(4)(4)        p->next=NULL(5)(5)        p->next=head(6)(6)        filename=argv[2](7)(7)        (fp=fopen(filename,“r”))==NULL(8)(8)        !feof(fp)(9)(9)        putchar(toupper(ch))(10)(10)    tag==2&&isdigit(ch)五、编程题(共10分,混合班学生做第2题,其他学生做第1题)1、#includefloatfact(int);main(){intm,n;floatval;scanf(“%d%d”,&m,&n);val=fact(m)/(fact(n)*fact(m-n));printf(“Value:%fn”,val);}floatfact(intn){if(n<=0)return1;return(fact(n-1)*n);}floatfact(intn){inti;floatf=1;for(i=1;i<=n;i++)f=f*i;returnf;} 2、#include#include#includeintstrcmp(char*s1,char*s2){inttoken=0,val1,val2,n,i; for(i=0;s1[i]!=’’;i++)if(s1[i]!=’+’&&s1[i]!=’-‘&&!isdigit(s1[i])){token=1;break;}for(i=0;!token&&s2[i]!=’’;i++)if(s2[i]!=’+’&&s2[i]!=’-‘&&!isdigit(s2[i])){token=1;break;}if(token==0)n{val1=atoi(s1);val2=atoi(s2);if(val1>val2)n=1;elseif(val1s2[i])return1;elseif(s1[i]D、_isw2.2.       若变量已正确定义并赋值,表达式______不符合C语言语法。A、a*b/c;B、3.14%2C、2,bD、a/b/c3.3.       _____是不正确的字符常量。A、'n'B、'1'C、"a"D、'101'4.4.       在C程序中,用_____表示逻辑值“真”。A、1B、非0的数C、非1的数D、大于0的数5.5.       ______把x、y定义成float类型变量,并赋同一初值3.14。A、floatx,y=3.14;B、floatx,y=2*3.14;C、floatx=3.14,y=x=3.14;D、floatx=y=3.14;6.6.       若变量已正确定义,表达式______的值不是2。A、2&3B、1<<1C、a==2D、1^37.7.       若变量已正确定义,语句“if(a>b)k=0;elsek=1;”和______等价。A、k=(a>b)?1:0;B、k=a>b;C、k=a<=b;D、a<=b?0:1;8.8.        设变量定义为“inta,b;”,执行下列语句时,输入_____,则a和b的值都是10。scanf("a=%d,b=%d",&a,&b);A、1010B、10,10C、a=10b=10D、a=10,b=109.9.        下列程序的输出结果是_____。fun(inta,intb,intc){c=a*b;}main()n{intc;fun(2,3,c);printf(”%dn”,c);}A、0B、1C、6D、无法确定1.10.   在C程序中,若对函数类型未加说明,则函数的隐含类型为_____。A、intB、doubleC、voidD、char2.11.   表达式strlen(”hello”)的值是_______。A、4B、5C、6D、73.12.   设变量定义为“inta[4];”,则表达式______不符合C语言语法。A、*aB、a[0]C、aD、a++4.13.    设变量定义为“intx,*p=&x;”,则&*p相当于_____。A、pB、*pC、xD、*&x5.14.   若变量已正确定义,____不能使指针p成为空指针。A、p=EOFB、p=0C、p=’’D、p=NULL6.15.    对于以下的变量定义,表达式______不符合C语言语法。structnode{intlen;char*pk;}x={2,"right"},*p=&x;A、p->pkB、*p.pkC、*p->pkD、*x.pk 二、填空题(每题2分,共20分)1.1.        表示“x≥y≥z”的C表达式是_______。2.2.        下列程序段的输出是______。printf("%on",010<<1^1);3.3.        下列程序段的输出是______。intk;floats;for(k=0,s=0;k<7;k++)s+=k/2;printf("%d,%fn",k,s);4.4.       下列程序段的输出是_______。#definef(a,b,x)a*x+bprintf("%d,%dn",f(3,2,1),f(6,5,f(3,2,1)));5.5.        下列程序段的输出是_____。main(){intk;for(k=1;k<5;k++){if(k%2)printf("*");elsecontinue;printf("#");}}6.6.       下列程序段的输出是_____。#includeintf(intx){staticy=1;y++;nx+=y;returnx;}voidmain(){intk;k=f(3);printf("%d%dn",k,f(k));}1.7.       下列程序段的输出是_____。int*p,*q,k=5,j=6;q=&k;p=q;q=&j;printf("%d",*p);2.8.       函数voidf(chars[],chart[]){intk=0;while(s[k]=t[k])k++;}等价于voidf(char*s,char*t){while(_____);}3.9.       下列程序段的输出是_____。char*a[2]={"one","two"},**p=a;printf("%s,",*(p++)+1);printf("%cn",**p-1);4.10.   用typedef定义一个文件指针FP。 三、程序阅读题(每题4分,共16分)1.1.       输入elephant后,下列程序的输出结果是_______。#includevoidmain(){inti=0,k,ch;staticintnum[5];charalpha[]={'a','e','i','o','u'},in[80];gets(in);while(in[i]){for(k=0;k<5;k++)if(in[i]==alpha[k]){num[k]++;break;}i++;}for(k=0;k<5;k++)if(num[k])printf("%c%d",alpha[k],num[k]);printf("n");} 2.2.       下列程序的输出结果是_______。n#includevoidf(intn){if(n<0){putchar('-');n=-n;}if(n/10)f(n/10);putchar(n%10+'0');}main(){f(-610);} 1.3.       输入01027后,下列程序的输出结果是_______。#includevoidp(intv[])2000~2001学年下《程序设计基础C》答案一、单项选择题(每题2分,共30分) 1D  2B  3C  4BA 5C   6C  7C  8D  9D  10A   11B  12D  13A  14A  15B   二、填空题(每题2分,共20分)1x>=y&&y>=z(错1项扣1分)22137,9(错1个数字扣1分)45,25(错1个数字扣1分)5*#*#(错2个字符扣1分)65,8(错1个数字扣1分)758*s++=*t++9Ne,s(ne给1分,s给1分)10typedefFILE*FP(错1项扣1分)  三、程序阅读题(每题4分,共16分)1A1e2(错1个扣2-610(错1个扣1n1分)分)310,7,2,0(错1个扣1分)4zoo  四、程序填空题(每空2分,共20分)(1)1或k>n/2或k>=n/2+1(2)prime(k)&&prime(i-k)(错1项扣1分)(3)&a,&b或&b,&a(错1项扣1分)(4)int*x,int*y或其它变量名(错1项扣1分)(5)T=*x(6)*x=*y(7)*y=t(8)p!=NULL或p!=0或p(9)q=p->next(10)p=q注:(4)--(7)作为一个整体评分,实现*x和*y交换 n五、编程题(第1题4分,第2题10分,共14分)1、#include"math.h"头文件1分main(){floatx,y;变量说明、输入、输出1分scanf("%f",&x);if(x>10)y=exp(x);2分elseif(x==10)y=0;elsey=3*x+5;printf("%fn",y);}  2、#include头文件、变量说明1分main(){FILE*fpin;charch;if((fpin=fopen("input.txt","r"))==NULL){2分printf("Can'topenfileinput.txtn");exit(0);}while(!feof(fpin)){2分ch=fgetc(fpin);2分if(ch!='')putchar(ch);2分}fclose(fpin);关闭文件1分} 或while((ch=fgetc(fpin))!=EOF)4分(读2分,判断2分)if(ch!='')putchar(ch);2分浙江大学“程序设计基础-C”试题卷2001-2002学年春季学期考试时间:2002年6月30日下午2:00-4:00注意:答题内容必须写在答题卷上,写在本试题卷上无效一.单项选择题(每题2分,共20分)1.1.       C语言程序的三种基本结构是顺序结构、选择结构和______结构。A.递归B.转移C.循环D.嵌套2.2.       下列运算符中,优先级最高的是_____。A.->B.++C.&&D.=3.3.       表达式!(x>0&&y>0)等价于_____。A.!(x>0)||!(y>0)B.!x>0||!y>0C.!x>0&&!y>0D.!(x>0)&&!(y>0)4.4.       表达式sizeof("nsum=%dn")的值是______。A.8B.9C.10D.11n1.5.       以下选项中不正确的实型常量是_____。A.0.23EB.2.3e-1C.1E3.2D.2.3e02.6.       表达式______的值不是1。A.0?0:1B.5%4C.!EOFD.!NULL3.7.       数组定义为inta[11][11],则数组a有_____个数组元素。A.12B.144C.100D.1214.8.       对于以下的变量定义,表达式______是不正确的。structnode{intj,k;}x,*p=&x;A.p->k=2B.(*p).k=2C.x.k=2D.x->k=25.9.       不正确的赋值或赋初值的方式是______。A.charstr[]="string";B.charstr[10];str="string";C.char*p="string";D.char*p;p="string";6.10.   设程序中不再定义新的变量,写出在函数main中可以使用的所有变量______。#includeintz;intp(intx){staticinty;return(x+y);}voidmain(){inta,b;printf("%dn",p(a));……}A.a,bB.a,b,zC.a,b,y,zD.a,b,x,y,z二、填空题(每题2分,共30分)1.1.       设字符型变量ch的值是小写英文字母,将它转换为相应大写字母的表达式是______。2.2.       表达式1<0<5的值是______。3.3.       表达式~(10>>1^~5)的值是______。4.4.       下列程序段的输出结果是______。inta=1;longb=a*0x12345678;printf("%xn",b);5.5.       设变量已正确定义,在程序段B中填入正确的内容,使程序段A和程序段B等价。程序段Afor(k=s=0;k<=10;s+=a[k++])if(a[k]<0)break;程序段Bfor(k=0,s=0;;s+=a[k++])if(______)break;6.6.       下列程序段的输出结果是_____。#defineT10#defineMD3*Tprintf("%d",30/MD);7.7.       下列程序段的输出结果是_____。intk,s;for(k=s=0;k<10&&s<=10;s+=k)k++;printf("k=%d,s=%d",k,s);8.8.       下列程序的输出结果是_____。n#includeintf(intm){staticintk=2;k++;returnk+m;}voidmain(){ints;s=f(2);printf("%d,%dn",s,f(s));}1.9.        对于以下递归函数f,调用f(3)的返回值是_____。f(intn){return((n<=0)?n:f(n-1)+f(n-2));}2.10.   下列程序段的输出结果是_____。intc[]={10,0,-10};int*k=c+2;printf("%d",*k--);3.11.   下列程序段的输出结果是_____。intk=1,j=2,*p=&k,*q=p;p=&j;printf("%d,%d",*p,*q);4.12.   下列程序的输出结果是_____。#includechar*f(char*str){returnstr="fun";}voidmain(){char*str="one";printf("%s,%cn",f(str)+1,*f(str)+1);}5.13.   下列程序的输出结果是_____。#includevoidp(int*x,inty){++*x;y--;}voidmain(){intx=0,y=3;p(&x,y);printf("%d,%d",x,y);}6.14.   下列程序的输出结果是_____。#include"stdio.h"voidmain(){intj,k,a[2][2];for(k=1;k<3;k++)for(j=1;j<3;j++)a[k-1][j-1]=(k/j)*(j/k);for(k=0;k<2;k++)for(j=0;j<2;j++)printf("%d,",a[k][j]);n}1.15.   用typedef定义一个指向函数的指针类型FP,该函数的返回值为整型。 三、程序阅读题(每题5分,共15分)1.1.       输入c2470f?<回车>后,下列程序的输出结果是_______,该程序的主要功能是_______。#include"stdio.h"intmain(){charch;longnumber=0;while((ch=getchar())<'0'||ch>'6');while(ch!='?'&&ch>='0'&&ch<='6'){number=number*7+ch-'0';printf("%ld#",number);ch=getchar();}}2.输入4<回车>后,下列程序的输出结果是_______。#include"stdio.h"intmain(){intj,k,n;floatf,s; scanf("%d",&n);s=0;f=1;for(k=1;k<=n;k++){for(j=1;j后,下列程序的输出结果是_______。#include#includestructlink{intmark;structlink*next;};voidf(structlink**);main(){structlink*head,*p;head=(structlink*)malloc(sizeof(structlink));head->mark=0;head->next=NULL;f(&head);for(p=head;p!=NULL;p=p->next)printf("%d#",p->mark);}voidf(structlink**head){intmark;structlink*p;scanf("%d",&mark);if(mark<=0){(*head)->mark++;nreturn;}else{p=(structlink*)malloc(sizeof(structlink));p->mark=mark;p->next=*head;*head=p;f(head);}} 四、程序填空题(每空2分,共20分)1.1.       输入10个浮点数,调用函数f(x)计算并输出相应的函数值。f(x)的功能是计算分段函数:x2+2x+1x<0y=0x=03x>0#include⑴{if(x<0)⑵;else⑶;elsereturn3;}voidmain(){intk;floatx;for(k=1;k<=10;k++){scanf("%f",&x);printf("y=%fn",f(x));}}2.2.       函数sort使用冒泡法将一维整型数组中各元素按值从大到小排序。voidsort(inta[],intn){intk,j,t;for(k=1;kvoidmain(){intdigit;longin,s;scanf("%ld",&in);⑺;⑻;while(in>0){⑼;s=s+digit*digit;⑽;}nprintf("sum=%ldn",s);} 五、编程题(共15分)1.函数ff(str,ch)的功能是统计并返回字符ch在字符串str中出现的次数。例如:调用ff("every",'e')后返回2,因为在字符串"every"中字符'e'出现了2次。 2.从键盘输入一个字符ch,输出该字符在文本文件input.txt的每一行中出现的次数。(必须调用第1题定义的函数ff统计ch的出现次数)。例如:如果文件input.txt中存放了下列数据:every121Iamastudent.运行程序,并输入'e'后,输出201浙江大学“程序设计基础-C”参考答案2001-2002学年春季学期(2002年6月30日)一、单项选择题(每题2分,共20分) 1C  2A  3A  4B  5AC   6C  7D  8D  9B  10B  二、填空题(每题2分,共30分)1ch-'a'+'A'或toupper(ch)213045678或123456785a[k]<0||k>10(错1项扣1分)61007k=5,s=15(错1项扣1分)85,9(错1个数字扣1分)9-210-10112,1(错1个数字扣1分)12un,g(un1分,g1分)131,3(错1个数字扣1分)141,0,0,1(错1个数字扣1分)15typedefint(*FP)();(错1项扣1分)  三、程序阅读题(每题5分,共15分)n12#18#七进制转换十进制错1项扣2分31#3#21#1173#错1项扣2分370#63#92#55#0#错1项扣1分四、程序填空题(每空2分,共20分)(1)floatf(floatx)(2)returnx*x+2*x+1(3)if(x==0)return0(4)j=0(5)a[j]/*头文件、变量说明、打开关闭文件、输入ch共2分*/#includevoidmain(){charch,c,s[80];intk;FILE*fp;if((fp=fopen("input.txt","r"))==NULL){printf("errorn");exit(0);}ch=getchar(); k=0;while(1){/*循环+beaak1分*/c=fgetc(fp);/*读入1分*/if(c=='n'||c==EOF){/*共6分*/s[k]='';k=0;printf("%dn",ff(s,ch));if(c==EOF)break;}elses[k++]=c;} fclose(fp);}或k=0;while(!feof(fp)){/*循环判断1分*/c=fgetc(fp);/*读入1分*/if(c!='n')/*共5分*/s[k++]=c;else{s[k]='';k=0;printf("%dn",f(s,ch));}}if(c!='n'){/*处理最后1行1分*/s[k]='';printf("%dn",ff(s,ch));}n《CProgramming》TESTPAPERTime:8:30-10:30am.June20,2003Importantnote:youranswersmustbewrittenontheanswersheet Section1:SingleChoice(1markforeachitem,total10marks)1.1.       Theprecedenceofoperator_____isthelowestone.A.<0||y>0)isequivalentto_____.A.!(x>0)&&!(y>0)B.!x>0&&!y>0C.!x>0||!y>0D.!(x>0)||!(y>0)4.4.       Thevalueofexpression______isn’t0。A.1/2B.!’’C.!EOFD.NULL5.5.       Ifxisafloatvariable,thevalueofexpression(x=10/4)is_____。A.2.5B.2.0C.3D.26.6.       Ifvariablesaredefinedandassignedcorrectly,theexpression______iswrong.A.a&bB.a^bC.&&xD.a,b7.7.       Accordingtothedeclaration:inta[10],*p=a;theexpression______iswrong.A.a[9]B.p[5]C.*p++D.a++8.8.       ______iswrong.A.charstr[10];str="string";B.charstr[]="string";C.char*p="string";D.char*p;p="string";9.9.       Ifallvariableshavebeendefinedanddeclaredinthefollowingprogram,allthevariableswhichcanbeusedinfunctionfun()are______.#includevoidfun(intx){staticinty;……return;}intz;voidmain(){inta,b;fun(a);……}A.x,yB.x,y,zC.a,b,y,zD.a,b,x,y,z10.10.   Accordingtothedeclaration:intp[5],*a[5];theexpression______iscorrect.A.p=aB.p[0]=aC.*(a+1)=pD.a[0]=2 Section2:Fillintheblanks(2markforeachitem,total30marks)1.1.       Accordingtothedeclaration:inta[2][3][4],thenumberofelementsofarrayais_____.2.2.       Writingconditionalexpression______tocalculatethevalueofy.1x>0y=0x=0-1x<03.3.       Thevalueofexpression1<10<5is_____.4.4.       Thevalueofexpression~(10<<1)&4is______.5.5.       Thevalueofexpressionsizeof(“hello”)is______.6.6.       Theoutputofthefollowingstatementsis_____.intk,s;nfor(k=1,s=0;k<10;k++){if(k%2==0)continue;s+=k;}printf("k=%ds=%d",k,s);1.7.       Theoutputofthefollowingstatementsis_____.#defineMM(x,y)(x*y)printf("%d",MM(2+3,15));2.8.       Theoutputofthefollowingstatementsis_____.intk=1,s=0;switch(k){case1:s+=10;case2:s+=20;break;default:s+=3;}printf("k=%ds=%d",k,s);3.9.       Theoutputofthefollowingprogramis_____.#includeintf(){staticintk;return++k;}voidmain(){intk;for(k=0;k<3;k++)printf("%d#",f());}4.10.   Theoutputofthefollowingprogramis_____.f(intx){if(x<=1)return1;elsereturnf(x-1)+f(x-2);}voidmain(){printf("%d",f(4));}5.11.   Theoutputofthefollowingstatementsis_____.intk=1,j=2,*p,*q,*t;p=&k;q=&j;t=p;p=q;q=t;printf("%d,%d",*p,k);6.12.   Theoutputofthefollowingstatementsis_____.intc[]={10,30,5};int*pc;for(pc=c;pcvoidp(int*x,inty){++*x;y=y+2;}voidmain()n{intx=0,y=3;p(&y,y);printf("%d,%d",x,y);}1.15.   Writingthedeclaration_____withtypedef,whichmakesCPasynonymforacharacterpointerarray,10elements. Section3:Readeachofthefollowingprogramsandanswerquestions(5marksforeachitem,totalmarks:30)1.1.       Theoutputofthefollowingprogramis_____.#includevoidmain(){intk,x,s,t; x=3;s=0;t=x;for(k=1;k<=3;k++){t=t*10+x;s=s+t;printf("%d#",s);}}2.2.       Wheninput:730303129760,theoutputis_______.#includevoidmain(){intj,k,sub,x;inta[5]; for(j=1;j<=2;j++){for(k=0;k<5;k++)scanf(“%d”,&a[k]);scanf(“%d”,&x);sub=-1;for(k=0;k<5;k++)if(a[k]==x)sub=k;printf("%d#",sub);}}3.3.       Theoutputofthefollowingprogramis_____.#includevoidmain(){longnumber,wt,x;x=number=10230;wt=1;while(x!=0){wt=wt*10;x=x/10;}wt=wt/10;while(number!=0){printf("%d#",number/wt);number=number%wt;wt=wt/10;}}n1.4.       Wheninput:398653712421234,theoutputis_______.#include"stdio.h"voidmain(){intflag,i,j,k,col,n,ri,a[6][6]; for(ri=1;ri<=2;ri++){scanf("%d",&n);for(i=0;i=n){printf("a[%d][%d]=%d#",i,col,a[i][col]);flag=1;}}if(!flag)printf("NO#");}}2.5.       Wheninput:howareyou?,theoutputis_______.#includevoidmain(){intword;charch;word=0;while((ch=getchar())!='?'){if(ch=='')word=0;elseif(word==0){word=1;if(ch<='z'&&ch>='a')ch=ch-'a'+'A';}putchar(ch);}}3.6.       Ifthefollowingdataarestoredintextfilea.txtone?two?1234?output?Andthefollowingdataarestoredintextfileb.txtone?two?1204?input?Theoutputofthefollowingprogramis_____.#include#includevoidmain(){intcount;charch1,ch2;FILE*f1,*f2; if((f1=fopen("a.txt","r"))==NULL){printf("Can'topenfile:%sn","a.txt");nexit(0);}if((f2=fopen("b.txt","r"))==NULL){printf("Can'topenfile:%sn","b.txt");exit(0);}count=0;while(!feof(f1)||!feof(f2)){ch1=fgetc(f1);ch2=fgetc(f2);if(ch1!=ch2){printf("%c#%c#",ch1,ch2);printf("%d#",count);break;}if(ch1=='?')count++;}fclose(f1);fclose(f2);} Section4:Accordingtothespecification,completeeachprogram(2markforeachblank,total:30marks) 1.Ifinputisn(n>0),calculatethevalueofs=1/1!+1/2!+1/3!+……+1/n!.#includevoidmain(){intj,k,n;floatf,s;scanf("%d",&n);(1);for(k=1;k<=n;k++){(2);for(j=1;(3);j++)(4);s=s+1.0/f;}printf("sum=%fn",s);} 2.Wheninputisn(n>0),ifit’saprimenumber,print"YES",otherwiseprint"NO".(Primenumbers:anintegralnumbernotdivisiblewithoutaremainderbyanyintegralnumberotherthanitselfandone.Oneisn’taprimenumber,buttwoisaprimenumber)。#include#includevoidmain(){intn;intprime(intm);scanf("%d",&n);if((5))printf("YESn");elseprintf("NOn");}n intprime(intm){inti,n; if(m==1)return0;n=sqrt(m);for(i=2;i<=n;i++)if((6))return(7);(8);} 3.Thedefinitionoffunctionf2()isequivalenttothedefinitionoffunctionf1().intf1(chars[]){intk=0;while(s[k]!='')k++;returnk;}intf2(char*s){char*ss;(9);while(*s++!=(10));return(11);} 4.Wheninputisthenamesandscoresof30students,printthenameandscoreofthestudentwhohavethehighestscore.#includestructstudent{charname[10];intscore;};voidmain(){intk;structstudents[30],*ptr;structstudent*f(structstudent*stud,intn); for(k=0;k<30;k++)scanf("%s%d",s[k].name,&s[k].score);(12);printf("%s%dn",ptr->name,ptr->score);}structstudent*f(structstudent*stud,intn){structstudent*pmax,*p;(13);for(p=stud;pscore>pmax->score)(14);return(15);} n浙江大学“C程序设计及实验”参考答案2002-2003学年春季学期(2003年6月20日) 一、单项选择题(每题1分,共10分) 1D  2D  3A  4B  5B   6C  7D  8A  9A  10C   二、填空题(每题2分,共30分)1242x>0?1:(x==0)?0:-13140566k=10s=25(错1项扣1分)7478k=1s=30(错1项扣1分91#2#3#(错1个数字扣1分)105112,1(错1个数字扣1分)1210#30#(错1个数字扣1分)13FOUR,P(错1个项扣1分)140,4(错1个数字扣1分)15typedefchar*CP[10];错1项扣1分)   三、程序阅读题(每题5分,共30分)133#366#3699#错1项扣2分33#-1#错1项扣2分31#0#2#3#错1项扣2分4a[0][0]=9#a[1][2]=7#a[1][1]=4错1项扣2分5HowAreYou错1项扣2分63#0#2#错1项扣2分 n四、程序填空题(每空2分,共30分)(1)s=0(2)f=1(3)j<=k(4)f=f*j(5)prime(n)(6)m%i==0(7)0(8)return1(9)ss=s(10)''(11)s-ss-1(12)ptr=f(s,30)(13)pmax=stud(14)pmax=p(15)pmax   提醒:考试失败你将仍有机会;警告:考试作弊你将失去学位。《CProgramming》TESTPAPERTime:8:30-10:30am.Jan15,2004Importantnote:youranswersmustbewrittenontheanswersheetSection1:SingleChoice(2markforeachitem,total20marks)1.Thedeclaration_____declaresthevariablewhichcanholdthelargestpositivenumber.A.inta;B.charb;C.floatc;D.doubled;2.The_____operatorcannotbeusedasaunary-operator.A.+B.-C.*D./3._____iswrongifitisusedasacharacterconstant.A.”1”B.’1’C.1D.’n’4.Ifcisacharactervariable,itsvalueis1or0afterevaluatingtheexpression______。A.c=getchar()!=EOFB.(c=getchar())!=EOFC.c=’0’D.c=c^~c5.Accordingtothedeclaration:inta[10],*p=&a[1];thelastelementofthearrayais______.A.a[10]B.p[8]C.p[9]D.p[10]6.Theprecedenceofoperator_____isthehighestone.A.+=B.[]C.?:D.++7.Thefollowingcodefragmentprintsout_____.chars[]="student";printf(“%s%d”,s+3,sizeof(s));A.student7B.dent7C.dent8D.student88.Accordingtothefollowingdeclaration,______isvalid.structnode{chars[10];intk;}p[5];nA.p.k=2B.p[0]->k=2C.(p->s)[0]=‘a’D.p[0].s=“a”1.Accordingtothedeclaration:int(*p)[10],pisa______.A.pointerB.arrayC.functionD.elementofaarray2.Assumethattheuserofaprogramisaskedtoenteradaynumber(1-7)intoanintegervariablecalledday.Inthefollowingwhileloops,______canbeusedtovalidatethedayentered.A.while(day>=1||day<=7){printf("ERROR1-7only,enteragain:");scanf(”%d”,&day);}B.while(day>=1&&day<=7){printf("ERROR1-7only,enteragain:");scanf(”%d”,&day);}C.while(day<1||day>7){printf("ERROR1-7only,enteragain:");scanf(”%d”,&day);}D.while(day<=1||day>=7){printf("ERROR1-7only,enteragain:");scanf(”%d”,&day);}Section2:Fillintheblanks(2markforeachitem,total30marks)1.Writeaconditionalexpression______tocalculatethemaximumofxandy.2.Thevalueofexpression7*7/9*4%3is_____.3.Thevalueofexpression10==10==10is_____.4.Thevalueofexpression255&128is______.5.Thefollowingcodefragmentprintsout_____.inta=3,b;if(a>5)b=7;elseif(a<2)b=6;elseb=5;printf("%d",b);6.Thefollowingcodefragmentprintsout_____.inta=13;intb=3;floatx;x=a/b;printf("%.1f",x);7.Thefollowingcodefragmentprintsout_____.intj=1,k=2;printf("%d,%d#",j++,++k);printf("%d,%d",--j,k--);8.Thefollowingcodefragmentprintsout_____.intx,y;x=y=3;switch(y){case1:x=0;break;case2:x=17;break;case4:x=9;break;}printf(“%d”,x);n1.Thefollowingcodefragmentprintsout_____.intarray[]={2,8,6,11,13,2,8,2};intk;inttotal=0;for(k=0;k<8;k++){if(array[k]%2)break;total+=array[k];}printf(“%d”,total);2.Theoutputofthefollowingstatementsis_____.#defineMM(x,y)((x)*(y))printf("%d",MM(4,2+3)-8);3.Theoutputoftheprogramgivenbelowis_____.#includeintmain(void){intk=1;inta_function(intj);k=a_function(k);printf("%d",k);}inta_function(intj){if(j<3){j++;j=a_function(j);}printf("%d#",j);return(j);}4.Thefollowingcodefragmentprintsout_____.inta=1;intb=2;int*x,*y;x=&a;y=&b;*x=*x+1;x=y;*x=*x+1;printf("%d,%d",a,b);5.Toexecutethecommand:progatmyhome,thevalueof*(++argv)[1]is______.6.Theprogramgivenbelowprintsout_____.voidmelon(intg,int*h);intmain(void){inta=1,b=2;melon(a,&b);printf("a=%d,b=%d",a,b);}voidmelon(intb,int*c){b++;*c=*c+b;}7.Writeawhileloopthatisequivalenttotheforloopgivenbelow.for(k=0;k<10;k++)sum=sum+k;Section3:Readeachofthefollowingprogramsandanswerquestions(6marksforeachitem,totalmarks:30)n1.Theoutputofthefollowingprogramis_____.#include"stdio.h"voidmain(){intj,k;for(j=0;j<3;j++){for(k=0;k<3;k++)printf("%2d",(k+j+1)%3?(k+j+1)%3:3);printf("#");}}2.Theoutputofthefollowingprogramis_____.#includevoidmain(){inti,k,m;inta[8]={60,75,63,92,87,64,79,21},s[10];for(k=0;k<10;k++)s[k]=0;for(i=0;i<8;i++){k=a[i]/10;s[k]++;}m=s[0];k=1;while(k<10){if(s[k]!=0)printf("%d#",s[k]);if(s[k]>m)m=s[k];k++;}printf("%d",m);}3.Theoutputofthefollowingprogramis_____.#include#includeintprime(intn){inti,m;if(n==1)return0;m=sqrt(n);for(i=2;i<=m;i++)if(n%i==0)break;returni>m;}voidmain(){intnum,i;num=20;for(i=2;i<=num;i++){while(prime(i)&&(num%i==0)){printf("%d",i);num/=i;}}}4.Theoutputofthefollowingprogramis_____.#includevoidmain(){intj,k;ncharc,s1[80]=”a-b-e8-9”,s2[80];j=k=0;while((c=s1[j++])!=’’)if(s1[j]==’-‘&&s1[j+1]>=c){j++;while(c#includevoidmain(){charch;FILE*fp;if((fp=fopen("a.txt","r"))==NULL){printf("Can'topenfile:%sn","a.txt");exit(0);}while(!feof(fp)){ch=fgetc(fp);if(ch>=’0’&&ch<=’9’)putchar(ch-‘0’+’A’);}fclose(fp);}Section4:Accordingtothespecification,completeeachprogram(2markforeachblank,total:20marks)1.Theprogramwrittenbelowprintsoutthenumbersbetween1and1000,whichequalthesumofthecubesofthedigits.Forexample,oneofthenumbersprintedoutcouldbe153,because153=1*1*1+5*5*5+3*3*3.#includevoidmain(){intdigit,k,m,s;for(k=1;k<=1000;k++){(1);m=k;while(m!=0){digit=m%10;s=s+digit*digit*digit;(2);}if((3))printf("%d",s);}}2.Thefunctionwrittenbelowisabinarysearchonewhichdecidesifaparticularvaluexoccursinthesortedarrayv.Theelementsofvareinincreasingorder.Thefunctionnreturnstheposition(anumberbetween0andn-1)ifxoccursinv,and–1ifnot.intbinasearch(intx,intv[],intn){intlow,high,mid;low=0;high=n-1;while((4)){mid=(low+high)/2;if((5))high=mid-1;elseif((6))low=mid+1;elsereturnmid;}(7);}3.Thedefinitionoffunctionf2()isequivalenttothedefinitionoffunctionf1().intf1(chars[],chart[]){intj,k;j=k=0;while(s[j]!='')j++;while((s[j++]=t[k++])!='');}intf2(char*s,char*t){while((8))(9)++;while((10));}Section5:OnlyforthestudentsofthecollegeofZKZReadthefollowingprogramandanswerquestion(10marks)Theoutputofthefollowingprogramis_____.#include#include#defineLENsizeof(structline)structline{intnum;structline*next;};voidmain(){intk;structline*p,*head;head=NULL;for(k=1;k<10;k++){p=(structline*)malloc(LEN);p->num=k;p->next=head;head=p;}while((p=p->next)!=NULL){printf("%d,",p->num);p=p->next;}}n《CProgramming》ANSWERTime:8:30-10:30am.June20,2003Section1:SingleChoice(2markforeachitem,total20marks)1D2D3A4A5B6B7C8C9A10CSection2:Fillintheblanks(2markforeachitem,total30marks)1x>y?x:y223041285564.071,3#1,3839161012113#3#3#3#122,313m14a=1,b=415nSection3:Readeachofthefollowingprogramsandanswerquestions(6marksforeachitem,totalmarks:30)1123#231#31221#3#2#1#1#332254abcde895CDEFSection4:Accordingtothespecification,completeeachprogram(2markforeachblank,total:20marks)(1)s=0(2)m=m/10(3)k==s(4)lowv[mid](7)return-1(8)*s!=‘’(9)s(10)*s++=*t++Section5:OnlyforstudentsofcollegeofZKZReadeachofthefollowingprogramsandanswerquestions(10marks)8,6,4,2,
查看更多

相关文章

您可能关注的文档