中国地质大学(北京)《C++程序设计》期末考试必备题集
奥鹏期末考核
88839–中国地质大学(北京)《C++程序设计》奥鹏期末考试题库合集
单选题:
(1)下列为合法的c++常量的是( ).
A.'105'
B.'Be'
C."x-y"
D.07ff
答案问询微信:424329
(2)表达式18/4*sqrt(4.0)/8值的数据类型为( ).
A.int
B.float
C.double
D.不确定
答案问询微信:424329
(3)以下语句中无法正确赋值的是( ).
A.charb[10]={'H','e','l','l','o','!'}
B.charb[10];b="Hello!"
C.char*b;b="Hello!"
D.charb[10]="Hello!"
答案问询微信:424329
(4)以下程序的输出结果是( ).#includeiostream.hvoid main(){ int i,j,x=0; for(i=0;i2;i++) { x++; for(j=0;j3;j++) { if(j%2) continue; x++; } x++; } coutxendl;}
A.4
B.8
C.6
D.12
答案问询微信:424329
(5)设有定义:unsigned op=2,则与语句op=1等价的语句为( ).
A.op*=2
B.op/=2
C.op*=4
D.op/=4
答案问询微信:424329
(6)c++语言源程序文件的扩展名是( ).
A."cpp"
B."obj"
C."exe"
D."c++"
答案问询微信:424329
(7)c++语言程序中可以对程序进行注释,注释部分必须用符合( )括起来.
A."{"和"}"
B."["和"]"
C."(*"和"*)"
D."/*"和"*/"
答案问询微信:424329
(8)已知字符'b'的ascII码为98,执行printf("%d,%c",'b','b'+1);语句后输出为( ).
A.98,b
B.语句不合法
C.98,99
D.98,c
答案问询微信:424329
(9)以下各项中,为非法常量数值的是( ).
A.11
B.18
C.0xabcd
D.5L
答案问询微信:424329
(10)下列程序运行后,输出结果是( ).#include iostream.hvoid main(){ int i=0,j=0,a=6;if((++i0)||(++j0)) a++;cout"i="i",j="j",a="aendl;}
A.i=0,j=0,a=6
B.i=1,j=1,a=7
C.i=1,j=0,a=7
D.i=0,j=1,a=7
答案问询微信:424329
(11)表达式-1? (0?0:-1):(10?10:1)的值为( ).
A.10
B.1
C.0
D.-1
答案问询微信:424329
(12)以下关于函数的叙述正确的是( ).
A.函数中必须要有return语句
B.函数中可以有多个return语句,但只有一个被执行
C.return语句中必须有一个表达式
D.函数中定义的变量不能与其它函数中的变量同名
答案问询微信:424329
(13)若有以下的定义和语句,则值为5的表达式是( ).int a[]={1,2,3,4,5,6,7,8,9,10};
A.a[5]
B.a[a[4]]
C.a[a[3]]
D.a[a[5]]
答案问询微信:424329
(14)设有以下结构体定义,则以下能输出字符串"grape"的语句为( ).struct ww{ int no; char name[10]; }cc[4]={1, "apple",2, "banana",3, "grape",4, "orange"};
A.coutcc[2]&name[0]endl
B.coutcc[2]name[0]endl
C.coutcc[2]&nameendl
D.coutcc[2]nameendl
答案问询微信:424329
(15)下面程序段的输出结果是( ).char p1[10]= "abc", p2[]="ABC", str[50]= "xyz";strcpy(str+2, strcat(p1,p2));coutstrendl;
A.xyabcABC
B.ZabcABC
C.YzabcABC
D.xyzabcABC
答案问询微信:424329
(16)以下选项中不正确的实型常量是( ).
A.2.3
B.0.23
C.1E3.6
D.3.2
答案问询微信:424329
(17)若a,b都是int类型变量,( )不是正确的赋值语句.
A.a=b=60
B.a=30
C.a=1,3
D.b=0
答案问询微信:424329
(18)下列各语句定义了数组,( )是不正确的.
A.inta[1][3]
B.intx[2][2]={1,2,3,4}
C.intx[2][]={1,2,4,6}
D.intm[][3]={1,2,3,4,5,6}
答案问询微信:424329
(19)a,b均为double型变量,且a=5.5,b=2.5,则(int)a+b/b的值是( ).
A.6.5
B.6
C.5.5
D.6.0
答案问询微信:424329
(20)以下各标识符中,正确的标识符是( ).
A.a-b
B.123_
C._123
D.signed
答案问询微信:424329
(21)以下不能用作标识符的是( ).
A._26
B.cout
C.case
D.Double
答案问询微信:424329
(22)满足x为奇数时值为真,x为偶数时值为假的表达式是( ).
A.!(x%2==1)
B.x%2==0
C.x%2
D.!(x%2)
答案问询微信:424329
(23)在定义int * p [3];中,标识符p( ).
A.是一个指向整型变量的指针
B.是一个指针数组,包含3个指向整型数据的指针变量
C.是一个函数指针,可以指向返回值类型为int型的函数
D.是一个指针变量,它可以指向一个具有三个元素的一维数组
答案问询微信:424329
(24)以下程序的输出结果是( ). char ss[16]= "test abcnt"; coutstrlen(ss)’,’sizeof(ss)endl;
A.4,16
B.11,16
C.11,11
D.4,11
答案问询微信:424329
(25)以下程序段的输出结果为( ).union u{char *name; int age; int income;} s;s.name="WangLing";s.age=28;s.income=1000; couts.ageendl;
A.28
B.1000
C.0
D.WangLing
答案问询微信:424329
(26)在c++语言程序中,用关键字( )定义无符号型变量.
A.unsigned
B.float
C.double
D.char
答案问询微信:424329
(27)以下语句不正确的语句是( ).
A.if(xy)1+2
B.if(x=y)&&(x!=0)x+=y
C.if(x!=y)cinx;elseciny
D.if(xy){x++;y++;}
答案问询微信:424329
(28)若变量已正确定义且k的值是4,计算表达式(j=k–)后,( )是正确的.
A.j=4,k=4
B.j=4,k=3
C.j=3,k=4
D.j=3,k=3
答案问询微信:424329
(29)以下叙述中不正确的是( ).
A.在函数中,通过return语句传回函数值
B.在函数中,可以有多条return语句
C.在c++中,主函数main后的一对圆括号中可以带行参
D.在c++中,调用函数,必须在一条独立的语句中完成
答案问询微信:424329
(30)x为整型变量,执行语句x=(x=5,x*4,x+5)10?20:(x+5);后,x的值为( ).
A.10
B.20
C.25
D.30
答案问询微信:424329
问答题:
(1)标识符的命名规则是什么?
答案问询微信:424329
(2)在C++语言中,重载有哪几种方式?
答案问询微信:424329
(3)说明在派生类定义中,基类前public,private,protected是什么意思?
答案问询微信:424329
(4)#includeiostream.hvoid main(){ int n=3,r(1); do{ r*=n; n–; }while(n=1); coutrendl; }
答案问询微信:424329
(5)#include iostream.hclass CPoint{ public: CPoint(int xx=0, int yy=0) {X=xx; Y=yy; countP++; } ~CPoint(){ countP–; } void GetC() {cout"countP="countPendl;}private: int X,Y; static int countP;};int CPoint::countP=0;void main(){ CPoint A(4,5),B; A.GetC(); { CPoint C(1,2);C.GetC(); } B.GetC();}
答案问询微信:424329
countP=3
countP=2
(6)#includeiostream.hvoid main(){ int n,r(1); for(n=4;n=1;n–) r*=n; coutrendl; }
答案问询微信:424329
(7)#include iostream.htemplate typename T,typename Pclass CPlus{public: CPlus(T m1,P n1){m=m1;n=n1;} P plus(); T m; P n;};template typename T,typename PP CPlusT,P::plus(){ P s; s=(P)(m+n); return(s);}void main(){ CPlusdouble,int a(12.345,6); cout"a="a.plus()endl; CPlusint,float b(14,2.56f); cout"b="b.plus()endl;}
答案问询微信:424329
b=16.56
(8)如果下面程序输入5,输出是什么?#includeiostream.hvoid main(){ int n,r(1); cinn; while(n=1) { r*=n; n–; } coutrendl; }
答案问询微信:424329
(9)class类类型中,拷贝构造函数应具有哪些特征?
答案问询微信:424329
(1) 该函数名与类名相同,因为它也是一种构造函数,并且该函数也不被指定返回类型;(1分)
(2) 该函数只有一个参数,并且是对同类的某个对象的引用;(1分)
(3) 每个类都必须有一个拷贝构造函数;(1分)
(4) 在新建对象时,对对象进行初始化时调用,对象之间赋值不调用.(1分)
(10)C++语言标准数据类型(即已有的)有哪些?
答案问询微信:424329
(11)类定义体中,有些函数之前有关键字virtual的意义是什么?
答案问询微信:424329
(12)#includeiostream.hint change(int x,int &y,int &z,int *dp){ z=x–+–y; *dp=x-y–; return(x+y);}void main(){ int a=4,b=4,c=4,d=4; a=change(a,b,c,&d); couta','b','c','dendl;}
答案问询微信:424329
(13)#includeiostream.hvoid main(){ int i,n=6,r(1); for(i=1;i=n;i++) r*=i; coutrendl; }
答案问询微信:424329
(14)#includeiostream.hint change(int x,int &y,int &z,int *dp){ z=x–+–y; *dp=x-y–; return(x+y);}void main(){ int a=8,b=8,c=8,d=8; a=change(a,b,c,&d); couta','b','c','dendl;}
答案问询微信:424329
(15).#includeiostream.hclass CPoint{ private:int X,Y; public: CPoint(int i, int j) {X=i; Y=j;} virtual int Area(){ return 0;}};class CRectangle:public CPoint{ private:int W,H; public: CRectangle(int i, int j, int k, int l) :CPoint(i,j) {W=k; H=l; } int Area() {return W*H;}};void fun(CPoint &s) { cout"Area="s.Area()endl; }void main() { CRectangle奥鹏期末考核 r(2, 3, 6, 8); fun(r); }
答案问询微信:424329
(16)#include iostream.hint fun(int n){ int r; if(n==1||n==2) r=1; else r=fun(n-1)+fun(n-2); return r;}void main(){ coutfun(6)endl; }
答案问询微信:424329
(17)什么叫类?什么叫对象?
答案问询微信:424329
对象是类的一个实例(2分).
(18)在C++语言中,请说明结构体和联合体的异同点.
答案问询微信:424329
(19)函数之间的参数传递的形式有哪几种?
答案问询微信:424329
(20)#include iostream.hclass B{ public: B(int i) { cout"constructing B "iendl; }};class C: public B{ public: C(int a, int b,int c):B(a),memberB(b) { cout"constructing C "cendl;}private: B memberB;};void main(){ C obj(1,2,3); }
答案问询微信:424329
constructing B 2
constructing C 3
(21)#includeiostream.hclass CPoint{ private:int X,Y; public: CPoint(int i, int j) {X=i; Y=j;} virtual int Area(){ return 0;}};class CTriangle:public CPoint{ private:int W,H; public: CTriangle (int i, int j, int k, int l) :CPoint(i,j) {W=k; H=l; } int Area() {return W*H/2;}};void fun(CPoint &s) { cout"Area="s.Area()endl; }void main() { CTriangle r(9, 8, 7, 6); fun(r); }
答案问询微信:424329
(22)#include iostream.hclass myclass{private: int a,b; static int s; public: myclass(int x,int y){ a=x; b=y; s++; } ~myclass(){s–;} void print(){ cout s endl; }} ;int myclass::s=0;void main(){ myclass m1(1,2),m2(4,5); m1.print(); { myclass m3(6,7); m3.print(); } m2.print();}
答案问询微信:424329
3
2
(23)#include iostream.hclass point { private:int x,y; public: point(int a=0,int b=0){ cout "A object is created"endl; } ~point(){ cout"A object is destroyed"endl; } };void main(){ point a1,a2,*p;p= new point;}
答案问询微信:424329
A object is created
A object is created
A object is destroyed
A object is destroyed
(24)#includeiostream.hint t(int x,int &y,int *cp,int z){ –x; ++y; *cp=–x+y++; z=x+*cp ; return z; }void main(){ int a=3, b=3,c(3),d(3); d+=t(a,b,&c,d); coutabcdendl; }
答案问询微信:424329