复习题及答案1.docx
《复习题及答案1.docx》由会员分享,可在线阅读,更多相关《复习题及答案1.docx(8页珍藏版)》请在冰豆网上搜索。
复习题及答案1
一、选择题
(11)在下列原型所示的C++函数中,按“传值”方式传递参数的是()。
A)voidf1(intx);B)voidf2(int*x);C)voidf3(constint*x);D)voidf4(int&x);
(12)在C++中,编译系统自动为一个类生成缺省构造函数的条件是( )。
A)该类没有定义任何有参构造函数B)该类没有定义任何无参构造函数
C)该类没有定义任何构造函数D)该类没有定义任何成员函数
(13)在一个派生类的成员函数中,试图调用其基类的成员函数“voidf();”,但无法通过编译。
这说明( )。
A)f()是基类的私有成员C)派生类的继承方式为私有
B)f()是基类的保护成员D)派生类的继承方式为保护
(14)下列关于运算符重载的叙述中,错误的是( )。
A)有的运算符可以作为非成员函数重载B)所有的运算符都可以通过重载而被赋予新的含义
C)不得为重载的运算符函数的参数设置默认值D)有的运算符只能作为成员函数重载
(17)下列字符串中不能作为C++标识符使用的是( )。
A)WHILEB)userC)_lvarD)9stars
(18)下列语句中错误的是( )。
A)constinta;B)constinta=10;C)constint*point=0;D)constint*point=newint(10);
(19)有如下程序:
#include
usingnamespacestd;
intmain(){
intsum;
for(inti=0;i<6;i+=3){
sum=i;
for(intj=i;j<6;j++)sum+=j;
}
cout<return0;
}
运行时的输出结果是( )。
A)3 B)10C)12D)15
(20)下列语句中,正确的是( )。
A)char*myString="Hello-World!
";B)charmyString="Hello-World!
";
C)charmyString[11]="Hello-World!
";D)charmyString[12]="Hello-World!
";
21)若已经声明了函数原型“voidfun(inta,doubleb=0.0);”,则下列重载函数声明中正确的是( )。
A)voidfun(inta=90,doubleb=0.0);B)intfun(inta,doubleB);
C)voidfun(doublea,intB); D)boolfun(inta,doubleb=0.0);
(22)有如下程序:
#include
usingnamespacestd;
classSample{
public:
Sample(){}
~Sample(){cout<<'*';}
};
intmain(){
Sampletemp[2],*pTemp[2];
return0;
}
执行这个程序输出星号(*)的个数为( )。
A)1B)2C)3D)4
(23)下列选项中,与实现运行时多态性无关的是( )。
A)重载函数 B)虚函数 C)指针 D)引用
(24)下列运算符函数中,肯定不属于类Value的成员函数的是()。
A)Valueoperator+(Value);B)Valueoperator-(Value,Value);
C)Valueoperator*(int);D)Valueoperator/(Value);
(26)在语句“cout<<'A';”中,cout是( )。
A)类名B)对象名C)函数名D)C++的关键字
(27)有如下程序:
#include
usingnamespacestd;
classMyClass{
public:
MyClass(inti=0){cout<<1;}
MyClass(constMyClass&x){cout<<2;}
MyClass&operator=(constMyClass&x){cout<<3;return*this;}
~MyClass(){cout<<4;}
};
intmain(){
MyClassobj1
(1),obj2
(2),obj3(obj1);
return0;
}
运行时的输出结果是( )。
A)112444B)11114444C)121444D)11314444
(28)有如下程序:
#include
usingnamespacestd;
classMyClass{
public:
MyClass(intx):
val(x){}
voidSet(intx){val=x;}
voidPrint()const{cout<<"val="<private:
intval;
};
intmain(){
constMyClassobj1(10);
MyClassobj2(20);
obj1.Print(); //语句1
obj2.Print(); //语句2
obj1.Set(20); //语句3
obj2.Set(30); //语句4
return0;
}
其主函数中错误的语句是()。
A)语句1B)语句2C)语句3D)语句4
(29)在类声明中,紧跟在“public:
”后声明的成员的访问权限是( )。
A)私有B)公有C)保护D)默认
(30)对于通过公有继承定义的派生类,若其成员函数可以直接访问基类的某个成员,说明该基类成员的访问权限是( )。
A)公有或私有B)私有C)保护或私有D)公有或保护
(31)定义派生类时,若不使用关键字显式地规定采用何种继承方式,则默认方式为( )。
A)私有继承B)非私有继承C)保护继承D)公有继承
(32)建立一个有成员对象的派生类对象时,各构造函数体的执行次序为( )。
A)派生类、成员对象类、基类B)成员对象类、基类、派生类
C)基类、成员对象类、派生类D)基类、派生类、成员对象类
(33)如果表达式a>=b中的“>=”是作为非成员函数重载的运算符,则可以等效地表示为( )。
A)a.operator>=(b)B)b.operatotr>=(a)
C)operator>=(a,b)D)operator>=(b,a)
(35)在一个抽象类中,一定包含有( )。
A)虚函数 B)纯虚函数C)模板函数 D)重载函数
二、填空题
(6)当使用关键字__void_______作为函数返回类型时,该函数不返回任何值。
(7)已知数组a中有n个元素,下列语句将数组a中从下标xl开始的k个元素移动到从下标x2开始的k个元素中,其中0<=xlfor(inti=xl+k-1;i>=xl;i--)
a[__i+x2-x1___]=a[i];
(8)有如下程序:
#include
usingnamespacestd;
intfun1(intx) {return++x;}
intfun2(int&x){return++x;}
intmain(){
intx=1,y=2;
y=fun1(fun2(x));
cout<return0:
}
程序的输出结果是___2,3________。
(9)有如下程序:
#include
usingnamespacestd;
classpumpkin{
public:
pumpkin(){++count;}
~pumpkin(){--count;}
staticvoidtotal_count(){
cout<}
private:
staticintcount;
};
intpumpkin:
:
count=0;
intmain(){
pumpkinpl[10];
pumpkin:
:
total_count();
return0;
}
这个程序的输出结果是__10pumpkin(s)__。
(10)有如下递归函数:
intFun(intn){
if(n<=1) return1;
_____elsereturnn*fun(n-1);_________
请补充完整,使得函数Fun能够正确计算形参n的阶乘。
(11)请在下列程序中的空格处填写正确的语句:
classSample{
public:
Sample(){}
~Sample(){}
voidSetData(intdata){//将Sample类成员变量data设置成形参的值
___this->data=data________
}
private:
intdata;
};
(12)有如下类定义,请将Sample类的拷贝构造函数补充完整。
classSample{
public:
Sample(){p=newint(0);}
~Sample(){if(p)deletep;)
Sample(constSample&s){
____p=newint(s.*p)_______
}
voidSetData(intdata) {p=newint(data);}
private:
int*p;
};
(13)重载加法运算符“+”,其函数名是__operator+______。
(14)有如下程序:
#include
usingnamespacestd;
classWages{ //“工资”类
doublebase; //基本工资
doublebonus; //奖金
doubletax; //税金
public:
Wages(doubleCBase,doubleCBonus,doubleCTax):
base(CBase),bonus(CBonus),tax(CTax){}
doublegetPay()const; //返回应付工资额
Wagesoperator+(Wagesw)const; //重载加法
};
doubleWages:
:
getPay()const{returnbase+bonus-tax;}
WagesWages:
:
operator+(Wagesw)const{
returnWages(base+w.base,bonus+w.bonus,tax+w.tax);
}
intmain(){
Wageswl(2000,500,100),w2(5000,1000,300);
cout<<(wl+w2).getPay0<retun0;
}
程序的输出结果是_____8100______
(15)有如下程序:
#include
usingnamespacestd;
classPet{
charname[10];
public:
Pet(char*name){strcpy(this->name,name);}
constchar*getName()const{returnname;}
virtualvoidcall()const=0;
};
classDog:
publicPet{
public:
Dog(char*name):
Pet(name){}
voidcall()const{cout<<"汪汪叫";}
};
classCat:
publicPet{
public:
Cat(char*name):
Pet(name){}
voidcall()const{cout<<"喵喵叫";}
};
intmain(){
Pet*pet1=newDog("哈克"),*pet2=newCat("吉米");
cout<getName();pet1->call();cout<cout<getName();pet2->call();cout<return0;
}
哈克汪汪叫
吉米喵喵叫