C程序设计试题及答案一

由于小编要期末考试,最近未按时发送,抱歉。答案在后面。点击阅读原文加入扣扣群,一起交流学习。

一、单项选择题

1.下面正确的字符常量是:

A)“c”B)‘\\’’C)‘W’D)‘’

2.下列字符串中不能作为C++标识符使用的是:

A)WHILEB)userC)_1varD)9stars

3.执行语句序列的输出结果是______。

inti=0;

while(i25)

i+=3;

couti;

A)24B)25C)27D)28

4.下列符号中可以用作C++标识符的是______。

A)radiusB)foo~barC)elseD)3room

5.若a是int类型变量,则表达式a=25/3%3的值是:

A)3B)2C)1D)0

6.以下叙述中不正确的是:

A)在不同的函数中可以使用相同名字的变量

B)函数中的形参是局部变量

C)在一个函数内部定义的变量只在本函数范围内有效

D)在一个函数内部定义的变量在所有函数内部有效

7.变量的引用,其含义是指该变量的:

A.值B.类型C.别名D.地址

8.已知定义:chars[10];则下面不表示s[1]的地址的是:

A.s+1B.*(s+1)C.s[0]+1D.s[1]

9.通常拷贝构造函数的参数是:

A.对象B.对象的成员C.对象的引用D.对象的指针

10.派生类对象的构造顺序是先调用:

A.派生类的构造函数B.基类的构造函数

C.内嵌对象的构造函数D.友元类的构造函数

二、填空题

1.动态多态是指在___(1)____时才确定使用哪个___(2)___函数的方式。

2.友员不是类的成员,但必须在___(3)__予以声明,它具有存取类的__(4)__成员的特权。

3.C++中class与struct的主要区别是在缺省访问权限时,__(5)_的成员为私有的,而__(6)_的成员为公有的。

4.若有定义语句:inta=3,b=2;,则表达式ab?a:b的值是___(7)____。

5.表达式:26%3=___(8)____,32/5=___(9)____。

6.类的成员包括__(10)__和___(11)___两种,在面向对象的术语中,前者称为属性、后者称为方法。其访问权限有三种,由符号__(12)___、__(13)__和_(14)_指定,其中具有__(15)_权限的成员只有类中的成员函数才能访问、而具有__(16)__权限的成员在任何函数中都可访问。

7.对基类成员的初始化,必须在派生类构造函数的(17)中进行。

8.C++源程序文件的扩展名是(18),头文件的扩展名是(19)。

9.若n为整型,则表达式n=(float)2/3的值是(20)。

三、写出程序的运行结果

1.写出程序运行结果

includeiostream.h

#includestring.h

classCRect

{

private:

charcolor[10];

intleft;

inttop;

intlength;

intwidth;

public:

CRect();

CRect(char*c,intt,intlef,intlen,intwid);

voidSetColor(char*c);

voidSetSize(intl=,intw=);

voidMove(intt,intl);

voidDraw();

};

CRect::CRect()

{

strcpy(color,Black);

top=0;

left=0;

length=0;

width=0;

}

CRect::CRect(char*c,intt,intlef,intlen,intwid)

{

strcpy(color,c);

top=t;

left=lef;

length=len;

width=wid;

}

voidCRect::SetColor(char*c)

{

strcpy(color,c);

}

voidCRect::SetSize(intl,intw)

{

length=l;

width=w;

}

voidCRect::Move(intt,intl)

{

top=t;

left=l;

}

voidCRect::Draw()

{

cout矩形左上角坐标为(left,top)endl;

cout矩形长和宽分别为length,widthendl;

cout矩形的颜色是colorendl;

}

voidmain()

{

CRectr;

r.SetColor(Red);

r.Move(10,20);

r.SetSize(,);

r.Draw();

r.Move(50,50);

r.SetColor(Blue);

r.Draw();

}

2.写出程序运行结果

#includeiostream.h

classA

{

intx,y;

public:

A()

{

x=0;

y=0;

}

A(inta,intb)

{

x=a;

y=b;

}

~A()

{

if(x==y)

coutx=yendl;

else

coutx!=yendl;

}

voidDisplay()

{

coutx=x,y=yendl;

}

};

voidmain()

{

Aa1,a2(2,3);

a1.Display();

a2.Display();

}

3.写出程序运行结果

#includeiostream.h

classA

{

private:

intn;

public:

A(inti)

{

n=i;

}

operator++()

{

n++;

}

operator++(int)

{

n+=2;

}

voidDisplay()

{

coutn=nendl;

}

};

voidmain()

{

Aa(2),b(2);

a++;

++b;

a.Display();

b.Display();

}

4.写出程序运行结果

#includeiostream.h

intfunc1(intn);

intfunc2(intn);

voidmain()

{

intsum;

sum=func2(5);

coutsumendl;

}

intfunc1(intn)

{

if(n==1)

return1;

else

returnn*func1(n-1);

}

intfunc2(intn)

{

ints=0;

for(inti=1;i=n;i++)

s+=func1(i);

returns;

}

四、编程(根据要求填上程序缺少的部分)

1.完成如下的程序,使得输出为:

#includeiostream.h

#includemath.h

classA

{

private:

________(1)___________

protected:

________(2)__________

public:

A(inta,intb,intc)

{

X=a;

Y=b;

Z=c;

}

intGetX()

{

returnX;

}

intGetY()

{

returnY;

}

intGetZ()

{

returnZ;

}

};

classB_____(3)_______

{

private:

intK;

public:

B(inta,intb,intc,intd)_______(4)_________

{

K=d;

}

voidShow()

{

coutGetX()GetY()ZKendl;

}

};

voidmain()

{

Bb(1,2,3,4);

b.Show();

}

2.在主函数中定义有30个元素的数组s,函数func1()的功能是将2、4、6、…、56、58、60分别赋给数组元素s[0]、s[1]、s[2]、…、s[27]、s[28]、s[29]。函数func2()的功能是按顺序将数组的每5个元素求平均值并存入数组w中(即将s[0]、s[1]、s[2]、s[3]、s[4]的平均值存入w[0]中,s[5]、s[6]、s[7]、s[8]、s[9]的平均值存入w[1]中,…)。请填空使程序正确运行。

#includeiostream.h

#includemath.h

voidfunc1(doubles[])

{

inti,k;

for(k=2,i=0;i30;i++)

{

s[i]=k;

______(5)_________

}

}

voidfunc2(doubles[],doublew[])

{

doublesum;

inti,k;

for(k=0,i=0,sum=0;i30;i++)

{

_________(6)___________

if((i+1)%5==0)

{

w[k]=sum/5;

_________(7)__________

k++;

}

}

}

voidmain()

{

doubles[30],w[6];

func1(s);

func2(s,w);

}

C++程序设计试题及答案(一)答案

一、单项选择题

1.C

2.D

3.A

4.A

5.B

6.D

7.D

8.A

9.c

10.b

二、填空题

1.执行,成员

2.类中,私有

3.class,struct

4.2

5.2,6

6.数据成员,函数成员

7.public,private,protect,私有,公共

8..cpp.h

9.0.5

三、写出程序的运行结果

1.矩形左上角坐标为50,50矩形长和宽分别为,矩形的颜色是Blue

2.x=0,y=0x=2,y=3x=yx!=y

3,n=3n=4

4,5

四、编程(根据要求填上程序缺少的部分)

1.intX,Y;

2.intZ;

3.publicA

4.:A(a,b,c)

5.k+=2;

6.sum+=s[i];

7.sum=0;









































早期白癜风要怎么治疗
早期白癜风要怎么治疗



转载请注明:http://www.nydjfy.com/bcxx/1368.html