当前位置:
文档之家› C++程序设计单元测试01(第三章控制结构)
C++程序设计单元测试01(第三章控制结构)
C++程序设计单元测试卷(第 3 章)
1. 选择题
(1)为了避免在嵌套的条件语句 if-else 中产生二义性,C++规定:else 子句总是与( 配对。 A.缩排位置相同的 if C.和其之后最近 if (2)下面程序输出的结果是( #include<iostream.h> void main() { int x=2, y=-1, z=2; if(x<y) if(y<0) z=0; else z+=1; cout<<z<<endl; } A.3 int n=1; while(n++<=2); A.2 (4)有以下程序段: int b=0; while(b=1) b++; while 循环执行次数是( A.无限次 C.一次也不执行 (5)以下描述正确的是( ) 。 ) 。 B.有语法错,不能执行 D.执行 1 次 cout<<n; B.3 C.4 D.语法有错 B.2 ) 。 C.1 D.0 (3)下面程序的运行结果是( B.和其之前最近 if D.同一行上的 if ) 。 )
(10)下面程序输出的结果是(
A.39
81
B.42
84
C.填空题
( 1 ) 在 C++ 语 言 中 常 使 用 的 循 环 语 句 有 ________________ , _______________ , 和 ________________三种。 (2)把以下嵌套的 if 语句改写成不嵌套的 if 语句是_________________________。 if w<0)k=0 else if(w<=100) k=1; else k=0; (3)下面的程序执行后,k 的值是_______。 #include<iostream.h> void main() { int num=26, k=1; do { k*=num%10; num/=10; } } (4)下面程序的功能是统计正整数的各位数字零的个数,并求各位数字中的最大数。完成 下面的程序填空。 #include<iostream.h> void main() { int n, count, max, t; count=max=0; cin>>n; do { t=____________; if(t= = 0) else n/=10; } (5)下面程序的功能是:从键盘上输入若干个学生的成绩,统计并输出最高和最低成绩, 档输入负数时结素输入,填充程序。 #include<iostream.h> void main() { float x, amax, amin; _____________; amax=x; amin=x; while(_____________) { if(x>amax) amax=x; } ++count; while(n); if(max<t) _____________; while(num); cout<<k;
A.goto 语句只能用于退出多层循环 B.swtich 语句中不能出现 continue 语句 C.只能用 continue 语句来终止本次循环 D.在循环终 break 语句不能独立出现 (6)下列程序是求 1 到 l00 的累加和,其中有 3 个能够完成规定的功能,有 1 个所完成的 功能与其他程序不同,它是( A.s=0; i=0; while(I<=100) s+=i++; C.s=0; i=0; ) 。 B.s=0; i=0; while(i++<100) s+=i; D.s=0; i=0;
{ y--;} while(--y); cout<<y--<<endl; } A.-1 #include<iostream.h> void main() { int a=5,b=6; if(a>5) { a-=1; b+=1;} else { a+=1; b-=1;} cout<<”a=”<<a<<endle; cout<<”b=”<<b; } A.5 7 include<iostream.h> void main() { int x, i; for(i=1; i<=100; i++) { x=i; if(++x%2= =0) if(++x%3= =0) if(++x%7= =0) cout<<x; } } B.a=5 b=7 ) 。 C.a=5 b=6 D.a=6 b=5 B.1 ) 。 C.8 D.0 (9)下面程序输出的结果是(
3. 看程序写结果
(1)#include<iostream.h> void main() { int a, b, c; int s, w, t; s=w=t=0; a=-1; b=3; c=3; if(c>0) s=a+b; if(a<=0) { if(b>0) if(c<=0 w=a-b);
} else if(c>0) w=a-b; else t=t=c; cout<<s<<w<<t<<endl; } (2)#include<iostream.h> void main() { int x=1, y=0, a=0, b=0; switch(x) { case 1: switch(y) { } case 0:a++; break; case 1: b++; break;
while(i<100) s+=++i; (7)请阅读以下程序: #include<iostream.h> void main() { int a=5,b=0,c=2;
while(++i<=100) s+=i;
if (a=a+b) cout<<”***”<<endl; else cout<<”$$$”<<endl; 以上程序( C.输出*** (8)下面程序的运行结果是( #include<iostream.h> void main() { int do y =10; ) 。 ) 。 B.可以通过编译但不能通过连接 D.输出$$$ A.有语法错不能通过编译
case 2:
a++; b++; break;
} cout<<a=<<a<<b=<<b; } (3)#include<iostream.h> void main() { int y=10; for( ; y>0; y--) if(y%3= =0) { cout<< - -y; countine; } } (4)#include<iostream.h> void main() { int a=1, b=10; do { b-=a; a++;} while(b--<0); cout<<”a=”<<a<<” } (5)#include<iostream.h> void main() { int i, j, m=0, n=0; for(i=0; i<2; i++) for(j=0; j<2; j++) if(j>=i) m=1; n++; cout<<m<<endl; cout<<n; } ”<<”b=”<<b;
cout<<”count=”<<count<<”max=”<<max;
if(x<amin)
amin=x;
___________ } cout<<”amax is: ”<<amax<<andl; cout<<”amin is: ”<<amin; } (6)下面程序的功能是计算正整数 1234 的各位数字平方和,填充程序 #include<iostream.h> void main() { int n,sum=0; n=1234; do { sum=sum+____________; n=____________; } while(________); cout<<”sum=”<<sum; } )
4. 编程题
(1)由键盘输入 x 的值,用 switch 编程计算 y 的值输出。
(2)求解满足 1+2+3+4+5+…..+n>=500 的最小是值 n 和总和值。 (3)打印出 100 内的所有的勾股数。勾股数就是满足 x2+y2=z2 的自然数。最小是勾股数 5, 4,3。