2005-2006学年第一学期期末考试
课程名称 《面向对象程序设计》(C++) B卷 评分标准
| 题 号 | 一 | 二 | 三 | 合 计 |
| 满 分 | 30 | 54 | 16 | 100 |
| 实得分 |
多选、错选不给分
1、执行以下语句后{int a=3,b=4,c=0;a=b=(c==1);},a与b 的值分别为(A)
A)0,0 B) 4,0 C) 3,4 D)不确定
2、for( T1 ; T2 ; T3;)可以省略的是(D)
A)T1 B)T2 C)T3 D)A,B,C
3、语句:func(); 不可能是(D )
A)函数原型B)函数声明C)函数调用 D)函数定义
4、以下对递归的评价正确的是(C)
A) 效率高,可读性好B)效率高,可读性差C)效率底,可读性好D)效率低,可读性差
5、对于语句:int a[10];与表达式a[2]=0不等价的是(A)
A)*a+2=0 B) (a+1)[1]=0 C) *(a+2)=0 D) (a+2)[0]=0
6、以下程序中有语法错误的是(D)
A) int *p;p++;
B) int (*p)[10];p++
C) int p;p++
D) void I,*p;p++;
7、以下哪个”hello”不是字符串常量(A)
A) char buffer[]=”hello”
B) char *P=”hello”
C) if(”hello”==”hello”) …
D) char *p[10]={ ”hello”,”aa”,…}
8、对于struct STR{ int i;};STR a,b;STR *p=&a; 以下语句不合法的是( A)
A) STR.i=0; B) a.i=0; C) a=b; D)p->i=0;
9.在C++中,数据封装要解决的问题是( D )。
A.数据规范化排列 B.数据高速转换
C.避免数据丢失 D.保证数据完整性
10、造函数与析构函数带参数情况是(C)
A)都可以带 B)都不可以带 C)只有构造函数可以带 D)只有析构函数可以带
二、阅读以下程序并选择输出结果正确的一组(共54分)
多选、错选不给分
1、(8分)
void swap1(int,int);
void swap2(int *,int *);
void main()
{
int x=4,y=5;
swap1(x,y);
cout<<”after swapping1”< cout<<”after swapping2< void swap1(int x,int y) { int temp=x; x=y; y=temp; } void swap2(int *x,int *y) { int temp=*x; *x=*y; *y=temp; } 运行以上程序,输出结果为以下内容(abcd)中若干项的组合: a、after swapping1... b、x:4 y:5 c、after swapping2... d、x:5 y:4 其中组合正确的是:(A ) A) abcd B) adcb C)acbd D)cdab 2(10) #include "iostream.h" #include "string.h" class A { public: A(int id=0) { value=id; cout<<"Assigning A id "< ~A() { cout<<"Destructing id "< protected: int value; }; class B { public: B(char *s="NoName",int ssid=0) { cout<<"Constructing B "< A id(ssid); } protected: char name[20]; A id; }; void main() { B s("Randy",2005); } 运行以上程序,输出结果为以下内容(abcde)中若干项的组合: a、Constructing B Randy b、Assigning A id 0 c、Assigning A id 2005 d、Destructing id 0 e、Destructing id 2005 其中组合正确的是:(C) A)abcde B)cbade C)baced D)edcba 3、(10) #include "iostream.h" #include "string.h" class B { public: B(char *s="NoName") { cout<<"Constructing new B "< } B(B & b) { cout<<"Constructing copy of "< strcat(name,b.name); } ~B() {cout<<"Destructing "< char name[20]; }; class C { public: C(B &b):b() { cout<<"Constructing C\\n"; } protected: B b; }; void fn( C c) {cout<<"In function fn()\\n";} void main() { B b("Tom"); C c(b); cout<<"Calling fn\\n"; fn(c); cout<<"Retrurned from fn\\n"; } 运行以上程序,输出结果为以下内容(abcdefghij)中若干项的组合: a、Constructing new B Tom b、Constructing new B NoName c、Constructing C d、Calling fn e、Constructing copy of NoName f、In function fn() g、Destructing copy of NoName h、Retrurned from fn i、Destructing NoName j、Destructing Tom 其中组合正确的是:(A) A)abcdefghij B)acdbefghij C)abcdefgji D)bcdfeghij 4、(10分) #include "iostream.h" #include "string.h" class M { public: M(char *s); ~M(); protected: static M *pFirst; M * pNext; char name[40]; }; M* M::pFirst=0; M::M(char *s) { strcpy(name,s); pNext=pFirst; pFirst=this; } M::~M() { cout< { pFirst=pNext; return; } for(M*t=pFirst;t;t=t->pNext) if(t->pNext==this) { t->pNext=pNext; return; } } M *fn() { M* pm=new M("TOM"); M fm("JIKE"); return pm; } void main() { M m("JONE"); M *pm=fn(); M n("ROSE"); delete pm; } 运行以上程序,输出结果为以下内容(abcd)中若干项的组合: a、TOM b、JIKE c、ROSE d、JONE 其中组合正确的是:(B) A) badc B)bacd C) abcd D)cadb 5、(8分) #include "iostream.h" class Shape { protected: double x,y; public: void setda(double I,double j){x=I;y=j;} virtual void disparea() { cout<<"No area display"< }; class Triangle:public Shape { public: void disparea() { cout<<"Triangle area "< }; void main() { Shape *p,s; Triangle t; p=&t; p->setda(2.0,5.0); p->disparea(); p=&s; p->setda(2.0,4.0); p->disparea(); } 运行以上程序,输出结果为以下内容(abc)中若干项的组合: 运行以上程序,输出结果为以下内容的组合, a、Triangle area 5 b、No area display c、Triangle area 4 其中组合正确的是:(A) A) ab B) bc C) ac D) ba 6、(8分) #include "iostream.h" class OBJ { public: OBJ(){cout<<"OBJ\\n";} }; class VISUAL_BASE { public: VISUAL_BASE(){cout<<"VISUAL_BASE\\n";} ~VISUAL_BASE(){cout<<"DEL VISUAL_BASE\\n";} }; class BASE { public: BASE(){cout<<"BASE\\n";} ~BASE(){cout<<"Del BASE\\n";} }; class Child:virtual public VISUAL_BASE,public BASE { public: Child(){} protected: OBJ obj; }; void main() { Child c; } 运行以上程序,输出结果为以下内容(abcde)中若干项的组合: a、BASE b、Del BASE c、OBJ d、DEL VISUAL_BASE e、VISUAL_BASE 其中组合正确的是:( B) A) abcde B) eacbd C) eacdb D) aecbd 三、程序设计(16分) 编写一个类(stack),实现简单的栈(提示:用链表结构实现)。数据的操作按先进后出的顺序。 成员函数为: void stack::put(int item);//将数据item插入栈中 int stack::get();//从栈中取一个数据 数据成员为: 一个指向链首的指针 链表结构为: struct Node { int a; Node *next; } 使用对象的过程为: stack st; st.put(10); st.put(12); st.put(14); cout< { int a; Node *next; }; class STACK { Node *top; public: STACK(){top=0;} void put(int item) { Node *tp=new Node; tp->a=item; tp->next=top; top=tp; } int get() { int i=-1; if (top) { Node *tp=top; i=top->a; top=tp->next; delete tp; } return i; } }; void main() { STACK st; st.put(10); st.put(12); st.put(14); cout< strcpy(name,s); strcpy(name,s);