1、要使以下程序的输出结果为10,则a和b应满足的条件是( )。 #include<iostream> using namespace std; void main() { int s, t, a, b; cin >> a >> b; s = t = 5; if(a > 0) s += 2; if(a > b) t = s + t; else if(a == b) t = 5; else t = 2 * s; cout << t << endl; } A、a>0并且a<b B、a<0并且a<b C、a>0并且a>b D、a<0并且a>b
2、执行以下程序段后,a,b,c的值分别是( )。 int a, b = 20, c, x = 9, y = 8; a = (--x == y)? --x : y++; if(x < 9) b = x++; c=y; A、7, 8, 8 B、7, 7, 8 C、7, 8, 9 D、8, 9, 9
2.2 选择结构程序设计(if-else)随堂测验
1、当从键盘输入10 20时,以下程序段的输出结果是 。 (区分==和=) #include<iostream> using namespace std; void main() { int num1, num2; cout<<"Please input num1 and num2:\n"; cin>>num1>>num2; if(num1 = num2) cout<<"num1=num2"<<endl; else cout<<"num1!= num2"<<endl; }
2、当输入1 0 0时,以下程序段的输出结果是 。 (易错的关系表达式) #include<iostream> using namespace std; void main() { int a, b, c; cout<<"Please input a, b and c:\n"; cin>>a>>b>>c; if(a<=b<=c) { cout<<"min=a="<<a<<endl; cout<<"max=c="<<c<<endl; } }
3、以下程序运行后的输出结果是 。 (条件表达式的多样性) #include<iostream> using namespace std; void main() { int a = 3, b = 4, c = 5, t = 99; if(b) if(a) cout<<a<<b<<t<<endl; else cout<<a<<b<<c<<endl; }
4、以下程序运行后的输出结果是 。 #include<iostream> using namespace std; void main() { int a=5, b=8 , c=4 , d=3 , m=1, n=0, p; if( (m = a > b) && (n = c > d)) p = m + n; else p = m – n; cout<<"m="<<m<<", n="<<n<<", p="<<p<<endl; }
2.3 选择结构程序设计(switch)随堂测验
1、若a和b均是整型变量,以下正确的switch语句是( )。 A、switch(a/b) {case 0:case1.5:y=a+b;break; case 2:case 3:y=a-b;} B、switch(a*a+b*b); { case0:y=a+b;break; case1:y=b-a;break; } C、switch a {case 0:x=a+b; case 1:y=a-b;break;} D、switch(a+b) {case 0:x=a+b;break; case 1:y=a-b;break;}
2.3 选择结构程序设计(switch)随堂测验
1、当从键盘输入'c'时,以下程序段的输出结果是 。 cin>>n; switch(n) { default: cout<<"error\n";break; case 'a': case 'A':case 'b':case 'B':cout<<"good\n";break; case 'c': case 'C':cout<<"pass\n"; case 'd': case 'D':cout<<"warn\n"; }
2、以下程序的输出结果是 。 #include<iostream> using namespace std; void main( ) { int i=4; switch(i%4) { case 0: case 1: cout<<'D'+i<<endl; case 2: cout<<'E'+i<<endl; break; default: cout<<"\n"; } }
3、以下程序的运行结果是 。 #include<iostream> using namespace std; void main() { int x=1,y=0,a=0,b=0; switch(x) { case 1: switch(y) { case 0: a++; case 1: b++; } case 2: a++;b++; break; case 3: a++;b++; } cout<<"a="<<a<<", b="<<b<<"\n”; }
2.4 循环结构程序设计(while语句)随堂测验
1、有以下程序段: int k = 0; while(k = 1) k++; 则while循环执行的次数是()。 A、无限次 B、有语法错,不能执行 C、一次也不执行 D、执行1次
2.4 循环结构程序设计(while语句)随堂测验
1、以下程序的输出结果是 。 #include<iostream> using namespace std; void main() { int num = 2; while(num-- ) ; cout<<num<<endl; }
2、以下程序的功能是:从键盘上输入若干个学生的成绩,统计并输出最高成绩和最低成绩,当输入负数时结束输入,请填空。(答案中请不要带空格,答案之间用3个空格键间隔) #include<iostream> using namespace std; void main() { float x,max,min; cin>>x; max=x; min=x; while( ) { if ( ) max=x; if( ) min=x; cin>>x; } cout<<"\nmax="<< max <<"\nmin="<< min<<"\n"; }
3、以下程序的输出结果是 。 #include<iostream> using namespace std; void main() { int x=5,y=9,a=0; while (x++!=y--) a+=1; cout<<"x="<<x<<",y="<<y<<",a="<<a<<"\n"; }
2.5 循环结构程序设计(do-while语句)随堂测验
1、有以下程序段: #include<iostream> using namespace std; void main() { int k = 0; do { k--; cout<<k<<endl; }while(k >= 0); } 则do-while循环执行的次数是()。 A、无限次 B、有语法错,不能执行 C、一次也不执行 D、执行1次
2.5 循环结构程序设计(do-while语句)随堂测验
1、以下程序的输出结果是 。 #include<iostream> using namespace std; void main() { int x = 4; do { x -= 3; cout<<x<<endl; }while(!( --x )); }
2.6 循环结构程序设计(for语句)随堂测验
1、以下程序的执行结果是( )。 #include<iostream> using namespace std; void main() { int i, sum=0; for(i=1;i<=5;sum++) sum+=i; cout<<sum<<endl; } A、6 B、5 C、无限循环 D、0
2.6 循环结构程序设计(for语句)随堂测验
1、以下程序的输出结果是 。 #include<iostream> #include<iomanip> using namespace std; void main() { int i; for (i=1;i>=0;) cout<<i--; }
2、下述程序计算Fibonacci数列的前20个数,且每行输出5个数,请填空。(答案中请不要带空格,答案之间用3个空格键间隔) #include<iostream> #include<iomanip> using namespace std; void main() { int f,f1=1,f2=1; int i; cout<<setw(6)<<f1; cout<<setw(6)<<f2; for (i=3;i<=20;i++) { f = ; cout<<setw(6)<<f; if( ) cout<<endl; f1=f2; ; } }
3、下述程序计算从键盘输入的两个数的最大公约数,请填空。(答案中请不要带空格,答案之间用3个空格键间隔) #include<iostream> #include<iomanip> using namespace std; void main() { int x, y, r, gcd; cout<<"Enter two number: \n "; cin>> x>>y; r= ; while ( ) { x=y; y=r; r= ; } cout<<"The result is "<<y<<endl; }
2、以下程序的运行结果是()。 #include<iostream> using namespace std; int func(int a, int b) { static int m, i=2; i+=m+1; m=i+a+b; return m; } void main() { int k=4, m=1, p; p=func(k, m); cout<<p<< “, ”; p=func(k, m); cout<<p<< endl; } A、8, 17 B、8, 16 C、8, 20 D、8, 8
3、以下程序的运行结果是()。 #include<iostream> using namespace std; int fun (int x) { static int a=3; a+=x; return a; } void main() { int k=2, m=1, n; n=fun(k); n=fun(m); cout<<n<< endl; } A、3 B、4 C、6 D、9
4、以下四个程序片段中各定义了一个变量a,其中在a变量作用域开始时才为其分配内存存储区,并在其作用域结束时就收回其存储区的是()。 A、int fun( ) { static int a; int b; ...... } B、float fun( ) { int a; int b; ...... } C、int a; int fun( ) { int b; ...... } D、static int a; int fun( ) { int b; ...... }
5、以下程序的输出是 。 #include<iostream> using namespace std; void swap(int *p1; int *p2) { int *t; t=p1; p1=p2; p2=t; } void main( ) { int a=3, b=5, *p1=&a, *p2=&b; swap(p1, p2); cout<<*p1<<*p2<<endl; }
5.5 指针与函数(返回指针的函数及指向函数的指针)随堂测验
1、设有以下语句: int add(int a, int b); int fun(int (*p)(int, int), int &a, int &b); int x, y, (*f)(int, int)=add; 则对函数fun的正确调用形式是()。 A、fun(f, x, y) B、fun(f, &x, &y) C、fun((*f)(x, y), x, y) D、fun(add, &x, &y)
2、对下面的程序段下面的说法正确的是()。 class A { int x, y; public: A(int a, int b) { x=a; y=b;} void show() { cout<<x<< “,”<<y<<endl; } void set(int a, int b) { x=a; y=b; } } void main() { A obj; obj.set(3, 5); obj.show(); } A、编译时报错,程序无法运行 B、编译无错,运行正常,输出3,5 C、编译无错,运行时报错 D、编译时报警告错,但运行正常,输出3,5
3、下列程序运行结果是 。 #include <iostream.h> class E{ int x; static int y; public: E(int a) {x=a; y+=x;} void show() { cout<<x<<’\t’<<y<<endl; } }; int E::y=100; void main() { E e1(10); e1.show(); E e2(50); e2.show(); e1.show(); }
1、下列程序运行结果是 。 class test{ long x; static int num; public: test (long m=0) {x=m;} test operator++(); test operator++(int); void Show() { cout<<"The num is: "<<num<<",x="<<x<<endl; } }; test test::operator++() { ++x; cout<<"The num is: "<<num++<<",x="<<x<<endl; return *this; } test test::operator++(int) { cout<<"The num is: "<<num++<<",x="<<x<<endl; test temp(*this); x++; return temp; } int test::num=1; void main() { test a(1),b; b=a++; ++a; a.Show(); b.Show(); }
2、下列程序运行结果是 。 #include <iostream.h> class A { long x; public: A(long m) {x=m;} void Show() { cout<<"count="<<count<<",x="<<x<<endl; } A& operator++(); A& operator++(int); static int count; }; A& A::operator++() { ++x; cout<<"count="<<count<<",x="<<x<<endl; return *this; } A&A::operator++(int) { cout<<"count="<<count++<<",x="<<x<<endl; A temp(*this); x++; return temp; } int A::count=1; void main() { A a(1); a.operator++(1); a.operator++(); a.Show(); }
3、下列程序运行结果是 。 class sample{ int x , y ; public : sample( ) { x=y=0 ; } sample( int i , int j) {x=i ; y=j ;} void copy( sample &obj) { *this=obj ; } sample operator--( ) { x-- ; y-- ; return *this; } void print( ){ cout << x << " " << y<< " " ; } } ; void main( ) { sample c1(1 , 2) , c2 ; --c1; c1.print( ) ; c2.copy(c1) ; --c2; c2.print( ) ; }
1、下列程序运行结果是 。 class a { public: a(char *nm){cout <<nm <<endl;} }; class b: public a { a aa1 ; public: b(char* n1,char*n2):aa1(n1),a(n2){}}; void main() { a ademo("how"); b bdemo("hello","world"); }
2、下列程序运行结果是 。 #include <iostream.h> class A { public: void fun(){cout<< “A::fun”<<endl;} }; class B : public A { public: void fun(){cout<< “B::fun”<<endl;} }; void main() { B b; A *pa=&b; pa->fun(); b.fun(); }
10.6 虚函数和纯虚函数随堂测验
1、下列程序运行结果是 。 #include <iostream.h> class A { public: virtual void fun(){cout<< “A::fun”<<endl;} }; class B : public A { public: void fun(){cout<< “B::fun”<<endl;} }; void main() { B b; A *pa=&b; pa->fun(); b.fun(); }
2、下列程序运行结果是 。 class A{ public: virtual void funcl(){cout<< "A1";} void func2(){cout<< "A2";} }; class B: public A{ public: void func1(){cout<< "B1";} void func2(){cout<< "B2";} }; int main(){ A *p=new B; p->funcl(); p->func2(); return 0; }
3、下列程序运行结果是 。 #include <iostream.h> class base { public: int n; base(int x) { n = x;} virtual void set(int m) { n = m; cout << n <<' ';} }; class deriveA:public base { public: deriveA(int x):base(x) { } void set(int m) { n += m; cout << n <<' ';} }; class deriveB:public base { public: deriveB(int x):base(x) { } void set(int m) { n +=m; cout <<n << ' ';} }; void main() { deriveA dl(1); deriveB d2(3); base *pbase; pbase = &dl; pbase->set(1); pbase = &d2; pbase->set(2); }
4、下列程序运行结果是 。 class A{ int a; public: A():a(12){} virtual void print(){cout<<a;}; }; class B:public A{ char b; public: B(){b= 'T';} void print(){cout<<b;} }; void show(A &X){X.print();} void main(){ A d1,*p; B d2; p=&d2; d1.print(); d2.print(); p->print(); show(d1); show(d2); }
5、下列程序运行结果是 。 class Base { public: void fun1(){cout<<"Basen";} virtual void fun2(){cout<<"Basen";} }; class Derived:public Base { public: void fun1(){cout<<"Derivedn";} void fun2(){cout<<"Derivedn";} }; void f(Base &b){ b.fun1(); b.fun2(); } void main(){ Derived obj; f(obj); }
6、下列程序运行结果是 。 class A { public: A() { cout<<"the constructor of class A"<<endl; f(); } virtual void f() {cout<<"A::f()"<<endl;} void h() { f(); } }; class B:public A { public: void f() {cout<<"B::f()"<<endl;} }; void main() { A a; B b; b.h(); }
8、下列程序运行结果是 。 class AA { int a; public: AA(int i) { a=i; cout<<"AA="<<a<<endl;} virtual ~AA() { cout<<"~AA="<<a<<endl;} }; class BB:public AA { int b; public: BB(int i,int j):AA(i) { b=j; cout<<"BB="<<b<<endl;} ~BB() { cout<<"~BB="<<b<<endl;} }; void main() { AA *pa=new AA(8); delete pa; AA *pb=new BB(6,9); delete pb; }
9、下列程序运行结果是 。(考点:纯虚函数和抽象类) class Pet{ char name[10]; public: Pet(char*name){strcpy(this->name, name);} const char*getName()const {return name;} virtual void call()const=0; }; class Dog: public Pet{ public: Dog(char*name):Pet(name){} void call()const{cout<<"汪汪叫";} }; class Cat:public Pet{ public: Cat(char*name):Pet(name){} void call()const{cout<<"喵喵叫";} }; int main(){ Pet*pet1=new Dog("哈克"), *pet2=new Cat("吉米"); cout<<pet1->getName();pet1->call(); cout<<end1; cout<<pet2->getName();pet2->call(); cout<<end1; return 0; }
10、若定义了一个类,该类只能用作基类,而不能定义该类的对象,这种类被称为 。
10.7 多重继承和多继承随堂测验
1、下列程序运行结果是 。 class a{ public: virtual void print(){cout<<"a prog..."<<endl;} }; class b: public a { int x; public: b(){x=0;} void print(int y){x = y;cout<<"b prog..."<<endl;} }; class c : public b { public: void print(){cout<<"c prog..."<<endl;} }; void show(a *p) { (*p).print(); } void main() { a x; b y; c *z = new c; show(&x); show(&y); show(z); }
2、下列程序运行结果是 。 class B1 {public: B1(int i) {cout<<"constructing B1 "<<i<<endl;} ~B1(){cout<<"destructing B1"<<endl;} }; class B2 {public: B2(int j) {cout<<"constructing B2 "<<j<<endl;} ~B2(){cout<<"destructing B2"<<endl;} }; class B3 {public: B3(){cout<<"constructing B3 *"<<endl;} ~B3(){cout<<"destructing B3"<<endl;} }; class C: public B2, public B1, public B3 { public: C(int a, int b, int c, int d): B1(a),memberB2(d),memberB1(c),B2(b) {} private: B1 memberB1; B2 memberB2; B3 memberB3;}; void main() { C obj(1,2,3,4); }
3、下列程序运行结果是 。 #include <string.h> class Base{ char msg[30]; protected : int n; public : Base(char s[],int m=0) : n(m) { strcpy(msg,s); } void output(void) { cout<<n<<endl<<msg<<endl; } }; class Derived1 : public Base{ int n; public : Derived1(int m=1) : Base("Base",m-2) { n=m; } void output(void) { cout<<n<<endl; Base::output( ); } }; class Derived2 : public Derived1{ int n; public : Derived2(int m=20) : Derived1(m-1) { n=m; } void output( ) { cout<<n<<endl; Derived1::output( ); } }; void main( ) { Derived2 D; D.output( ); }
4、下列程序运行结果是 。 #include <iostream.h> class A { public: A( ) { cout << "A"; } }; class B { public: B( ) { cout << "B"; } }; class C : public A { public: C( ) { cout << "C"; } private: B b; }; void main( ) { C obj; }
5、下列程序运行结果是 。 class A1 {public: A1(int i) {cout<<" A1的构造函数 "<<i<<endl;} ~A1(){cout<<"A1的析构函数"<<endl;} }; class A2 {public: A2(int j) {cout<<" A2的构造函数"<<j<<endl;} ~A2(){cout<<" A2的析构函数"<<endl;} }; class A3 {public: A3(){cout<<" A3的构造函数"<<endl;} ~A3(){cout<<" A3的析构函数"<<endl;} }; class C: public A2, public A1, public A3 { public: C(int a, int b, int c, int d): A1(a),memberA1(d),memberA2(c),A2(b) {} private: A2 memberA2; A1 memberA1; A3 memberA3;}; void main() { C obj(5,6,7,8); }