计算机类二级C++

2014年全国计算机等级《二级C++》考前押密试卷(2)

43 题含答案C++
共 43 题返回本类
1单选题1 / 43
关于运算符重载,下列表述中正确的是(  )。
查看答案
C. 在类型转换符函数的定义中不需要声明返回类型
2单选题2 / 43
在类的定义中,用于为对象分配内存空间,对类的数据成员进行初始化并执行其他内部管理操作的函数是( )。
查看答案
C. 构造函数
3单选题3 / 43
软件设计中模块划分应遵循的准则是( )。
查看答案
B. 高内聚低耦合
4单选题4 / 43
如果类A被说明成类B的友元,则( )。
查看答案
D. 类B不一定是类A的友元
5单选题5 / 43
有如下类定义:
class Foo
{
public:
Foo(int v):value(V){}//①
~Foo(){}//②
private:
Foo(){}//③
int value=0://④
};
其中存在语法错误的行是(  )。
查看答案
D. ④
6单选题6 / 43
下面关于数组的描述错误的是( )。
查看答案
D. 数组只能通过值参数和引用参数两种方式传递给函数
7单选题7 / 43
运算符重载是对已有的运算符赋予多重含义,因此( )。
查看答案
C. 只能重载c++中已经有的运算符,不能定义新运算符
8单选题8 / 43
有如下类和对象的定义:
class Constants{
public:
static double getPI(){return 3.1416;}
};
Constants constants:
下列各组语句中,能输出3.1416的是( )。
查看答案
D. cout<<CONSTANTS.GETPI();和COUT<
9单选题9 / 43
程序流程图中带有箭头的线段表示的是( )。
查看答案
C. 控制流
10单选题10 / 43
下列程序的执行结果为( )。
#include
void main()
{
int a=3,b=0;
int*P =&a;
b=+a++;
cout<<*P<<”,”<<B<
}
查看答案
B. 4,3
11单选题11 / 43
对长度为n的线性表排序,在最坏情况下,比较次数不是n(rt一1)/2的排序方法是( )。
查看答案
D. 堆排序
12单选题12 / 43
字面常量42、4.2、42L的数据类型分别是(  )。
查看答案
C. int、double、long
13单选题13 / 43
下列叙述中正确的是(  )。
查看答案
D. 有序线性表既可以采用顺序存储结构,也可以采用链式存储结构
14单选题14 / 43
不能作为重载函数的调用的依据是(  )。
查看答案
D. 函数名称
15单选题15 / 43
下列语句中,错误的是(  )。
查看答案
D. double*eonst point:
17单选题17 / 43
有如下说明:
int a[10]={1,2,3,4,5,6,7,8,9,10},* P=a; 则数值为9的表达式是( )。
查看答案
B. 木(P+8)
18单选题18 / 43
执行下列语句段后,输出字符“*”的个数是( )。
for(int i=50;i>1;i-=2)Cout<<'*':
查看答案
B. 25
19单选题19 / 43
算法的有穷性是指( )。
查看答案
A. 算法程序的运行时间是有限的
20单选题20 / 43
下面关于break语句的描述中,不正确的是( )。
查看答案
D. break语句可用于if条件判断语句内
21单选题21 / 43
有如下类定义:
class Point
{
int xx.yy;
public:
Point():xx(0),yy(0){}
Point(int x,int Y=0):xx(X),YY(Y){}
};
若执行语句
Point a(2),b[3],幸c[4];
则Point类的构造函数被调用的次数是(  )。
查看答案
C. 4次
22单选题22 / 43
有如下程序:
#include
using namespace std;
class Basel
{
public:
BaseI(int D.{cout<<D;}
一Basel(){}
};
class Base2
{
public:
Base2(int D.{tout<<D;}
~Base2(){}
};
class Derived:public Basel,Base2
{
public:
Derived(int a,int b,int c,int D.:Basel(B.,Base2(A.,bl(D.,b2(C.{}
private:
int bl:
int b2:
};
int main()
{
Derived d(1,2,3,4);
return 0;
}
执行这个程序的输出结果是(  )。
查看答案
D. 21
23单选题23 / 43
以下叙述正确的是( )。
查看答案
D. 函数可以嵌套调用但不可以嵌套定义
24单选题24 / 43
必须用一对大括号括起来的程序段是( )。
查看答案
D. 函数的函数体
25单选题25 / 43
软件按功能可以分为应用软件、系统软件和支撑软件(或工具软件)。下面属于应用软件的是( )。
查看答案
A. 学生成绩管理系统
26单选题26 / 43
下面程序的运行结果为( )。
#include
void main()
{
for(int a=0,x=0;!x&&a<=10;a++)
{
a++:
}
cout<<A<
}
查看答案
C. 12
27单选题27 / 43
有如下程序:
#include
using namespace std;
int i=1;
class Fun{
public:
static int i;
int value(){return i—l;}
int value()const{return i+1;}
};
int Fun::i=2;
int main(){
int i=3;
Fun funl;
const Fun fun2:
__________________
return 0;
}
若程序的输出结果是:
123
则程序中下画线处遗漏的语句是(  )。
查看答案
A. cout<<FUNL.VALUE()<<FUN::I<<FUN2.VALUE();
28单选题28 / 43
下列有关模板的叙述中,正确的是( )。
查看答案
B. 函数模板的一个实例就是一个函数定义
29单选题29 / 43
运算符重载时不需要保持的性质是( )。
查看答案
B. 操作数类型
30单选题30 / 43
下列程序的输出结果为( )。
#include
{
if(n<1)return l;
else return n+time(n一1); return 0;
}
void main() {
cout<<FUNC(5)<
}
查看答案
D. 16
31单选题31 / 43
有如下程序:
#include
using namespace std;
class MyClass{
public:
MyClass(int i=0){cout<<1;}
MyClass(const MyClass&x){cout<<2;}
MyClass&operator=(const MyClass&x){cout<<3;retum$this;}
~MyClass()}cout<<4;}
};
int main()
{
MyClass objl(1),obj2(2),obj3(objl);
retum 0;
{
执行这个程序的输出结果是( )。
查看答案
A. 112444
32单选题32 / 43
在下列模式中,能够给出数据库物理存储结构与物理存取方法的是( )。
查看答案
B. 内模式
33单选题33 / 43
有如下程序:
#inClude using namespaCe std; Class Base{
publiC:
void output(){Cout<<1;}
virtual void Print(){Cout<<'B’;} };
Class Derived:publiC Base{ publiC:
void output(){Cout<<2;} void Print(){Cout<<’D ';} };
int main() {
Base * ptr=new Derived; ptr一>output();
ptr一>Print(); delete ptr;
retum0; }
执行这个程序的输出结果是( )。
查看答案
B. lD
34单选题34 / 43
以下程序中,错误的行为是( )。
①#include
②class A
③{
④public:
⑤ int n=2;
⑥ A(int val){cout<<VAL<
⑦ -A(){};
⑧};
⑨void main() ⑩{
? A a(0);
?}
查看答案
A. ⑤
35单选题35 / 43
下列数据结构中,属于非线性结构的是( )。
查看答案
C. 二叉树
36单选题36 / 43
有如下程序:
#inClude using namespaCe std; Class A{
publiC:
statiC int a;
void init(  ){。a=1;}
A(int a=2){init(  );a++;} };
int A::a:0: A obj;
int main(  ) {
Cout<<OBJ.A:&NBSP;&NBSP;&NBSP;
}
运行时输出的结果是(  )。
查看答案
B. 1
37单选题37 / 43
软件详细设计产生的图如下:
  
该图是( )。
查看答案
C. 程序流程图
38单选题38 / 43
秘序调试的任务是( )。
查看答案
D. 诊断和改正程序中的错误
39单选题39 / 43
有如下程序:
#include
using namespace std;
class A{
public:
virtual void fancl(){cout<<”Al”;}
void func2(){cout<<”A2”;}
};
class B:public A{
public:
void funcI(){tout<<”Bl”;}
void func2(){tout<<”B2”;}
};
intmain()}
A*P=new B:
P—>funcl();
P—>func2();
delete p;
return 0;
}
执行这个程序的输出结果是(  )。
查看答案
C. B1A2
41主观题41 / 43
请使用VC6或使用【答题】菜单打开考生文件夹projl下的工程projl,其中有枚举DOGCOLOR、狗类Dog和主函数main的定义。程序中位于每个“//ERROR ****found****”下的语句行有错误,请加以改正。改正后程序的输出结果应该是:
There is a white dog named Hoh0. There is a blaCk dog named Haha. There is a motley dog named Hihi. 注意:只修改每个//ERROR****found****女料下的那一行,不要改动程序中的其他内容。
#inClude using namespaCe std; //狗的颜色:黑、白、黄、褐、花、其他
enum DOGCOLOR{BLACK,WHITE,YELLOW,BROWN,PIEBALD,OTHER}; Class Dog{//狗类
DOGCOLOR Color; Char name[20]; statiC int Count; publiC:
Dog(Char nalTle[],DOGCOLOR Color){ strCpy(this一>name,name);
//ERROR**********found**********、
strCpy(this一>Color,Color);
} 1 DOGCOLOR getColor()Const{retum Color;}
//ERROR**********found**********
Const Char * 9etName()Const{return*finin9;} Const Char * 9etColorString()Const{
switCh(Color){
Case BLACK:return”blaCk“; Case WHITE:return”white”:
Case YELLOW:return’"yellow”: Case BROWN:return”brown”; ease PIEBALD:return”piebald”:
}
return”motley”;
}
void show()Const{ . Cout<<”There is a”<<GETCOLORSTRING()<
} }; int main(){
//ERROR**********found**********
Dog dogl(”Hoh0”,WHITE),dog2(”Haha”,BLACK);d0g3(”Hihi”,0THER); d091.show();
dos2.show(); dos3.show(); return 0;
}
查看答案
(1)this->Color=Color:
(2)ConsL ChAr GEtNAmE()Eonst{rEturn*nAmE;}
(3)DoG DoGl(”Hoho”,WHITE)。DoG2(”HAhA”,BLACK),DoG3(”Hihi”,OTHER);
42主观题42 / 43
请使用VC6或使用【答题】菜单打开考生文件夹proj2下的工程proj2。此工程中包含一个源程序文件main.cpp,其中有“房间”类Room及其派生出的“办公室”类0ffice的定义,还有主函数main的定义。请在程序中“//****found****”下的横线处填写适当的代码并删除横线,以实现上述类定义。此程序的正确输出结果应为:
办公室房间号:308
办公室长度:5.6
办公室宽度:4.8
办公室面积:26.88
办公室所属部门:会计科
注意:只能在横线处填写适当的代码,不要改动程序中的其他内容,也不要删除或移动“///料料found??料”。
#include
using namespaee std;
class Room{ //“房间”类
int room_n0;//房间号
double length;//房间长度(m)
double width;//房间宽度(n1)
public:
Room(int the—room—n0,double the~length,double the—width):loom—no(the—room—no),length(the—
length),width(the—width){}
int theRoomNo()eonst{return iDonl—n0;}
//返回房间号
double theLength()const{return length;}//返回房间长度
double theWidth()const{return width;}//返回房间宽度
//**********found**********
double theArea()const{_______________}//返回房间面积(矩形面积)
};
class Office:public Room{//“办公室”类
char*depart;//所属部门
public:
Office(int the_room_n0,double the_length,double the_width,eonst char木the_depart)
//**********found**********
:_______________{
depart=new char[strlen(the depart)+1];
//**********found**********
strcpy(_______________);
}
~Office(){delete[]depart;}
const char*theDepartment()const{return depart;}//返回所属部门
};
int main(){
//**********found**********
Office_______________;
cout<<”办公室房间号:”<<AU_OFFICE.THEROOMNO()<<ENDL
<<”办公室长度:”<<<endl
<<”办公室宽度:”<<<endl
<<”办公室面积:”<<<endl
<<”办公室所属部门:”<<ALL—OFFICE.THEDEPARTMENT()<
return 0;
}
查看答案
(1)rEturtl lEnGth*wiDth;
(2)Room(thE_room_no,thE_lEnGih,thE_wiDth)
(3)DEpArt,thE_DEpArt
(4)An_oFFiCE(308,5.6,4.8,”会汁科”)
43主观题43 / 43
请使用VC6或使用【答题】菜单打开考生目录proj3下的工程文件proj3,此工程中包含一个源程序文件proj3.cpp,补充编制c++程序proj3.cpp,其功能是读取文本文件in.dat中的全部内容,将文本存放到doc类的对象myDoc中。然后将myDoc中的字符序列反转,并输出到文件out.dat中。文件in.dat的长度不大于1000字节。
要求:
补充编制的内容写在“//**********”与“//**********”两行之间。实现将myDoc中的字符序列反转,并将反转后的序列在屏幕上输出。不得修改程序的其他部分。
注意:程序最后已将结果输出到文件0ut.dat中,输出函数writeToFile已经给出并且调用。
//proj3.cpp
#include
#include
#include
using namespace std;
class doc
{
private:
char*sir;//文本字符串首地址
int length;//文本字符个数
public:
//构造函数,读取文件内容,用于初始化新对象,filename是文件名字符串首地址
doc(char*filename);
void reverse();//将字符序列反转
一doc();
void writeToFile(char*filename);
};
doc::doc(char}filename)
{
ifstream myFile(filename);
int len=1001,tmp;
atr=new char[1en];
length=0;
while((trap=myFile.get())!=EOF)
{
str[1ength++]=trap;
}
str[1ength]=‘\0’;
myFile.close();
}
void doc::reverse(){
//将数组atr中的length个字符中的第一个字符与最后一个字符交换,第二个字符与倒数第二个
//字符交换……
//*************333*************
//*************666*************
}
doc::~doe()
{
delete[]str;
}
void doc::writeToFile(char*filename)
{
Ofstream outFile(filename);
outFile<
outFile.close();
}
void main()
{
doc myDoc(”in.dat”);
myDoc.reveme();
myDoc.writeToFile(”out.dat”);
}
查看答案
int j,J;//定义两个整数l临时变量i.j.
for(i=0,j=length一1;i<j;i++,j-)
chartemp=str[i];
//把str[i]中的值保存在临时变量temp
str[i]=str[j];
//把Str[j]值赋给Str[i],实现字符前后替换
str[j]=temp;
//把保存在临时变量temp中的值再赋值给Str[j]
}
← 上一卷下一卷 →