一、选择题
- C++语言属于( C )。
A) 自然语言 B) 机器语言 C)面向对象语言 D) 汇编语言
- 下面选项中不属于面向对象程序设计特征的是(C) 。
A)继承性 B)多态性 C)相似性 D)封装性
- 可用作C++语言用户标识符的一组标识符是( B )。
A) void define +WORD B) a3_b3 _123 YN
C) for -abc Case D) 2a DO sizeof
- 假定一个二维数组的定义语句为“int a[3][4]={{3,4},{2,8,6}};”,则元素a[2][1]的值为(A)。
A) 0 B) 4 C) 8 D) 6
- 下列情况中,哪一种情况不会调用拷贝构造函数 ( B )
A)用派生类的对象去初始化基类对象时
B)将类的一个对象赋值给该类的另一个对象时
C)函数的形参是类的对象,调用函数进行形参和实参结合时
D)函数的返回值是类的对象,函数执行返回调用者时
- 以下哪一关键字可用于重载函数的区分( C )
A)extern B)static C)const D)virtual
- 下列有关数组的叙述中,正确的是( B )
A)C++中数组的存储方式为列优先存储
B)数组名可以作为实参赋值给指针类型的形参
C)数组下标索引从1开始,至数组长度n结束
D)数组指针的语法形式为:类型名 *数组名[下标表达式];
- 下列有关继承和派生的叙述中,正确的是( C )
A)派生类不能访问通过私有继承的基类的保护成员
B)多继承的虚基类不能够实例化
C)如果基类没有默认构造函数,派生类就应当声明带形参的构造函数
D)基类的析构函数和虚函数都不能够被继承,需要在派生类中重新实现
- 实现运行时多态的机制是( A )
A)虚函数 B)重载函数 C)静态函数 D)模版函数
- 若有下面的函数调用:
fun(a+b, 3, max(n-1, b));
其中实参的个数是( A )
A)3 B)4 C)5 D)6
- 下列关于this指针的说法正确的是( B )
A)this指针存在于每个函数之中
B)在类的非静态函数中this指针指向调用该函数的对象
C)this指针是指向虚函数表的指针
D)this指针是指向类的函数成员的指针
- 在下列关于C++函数的叙述中,正确的是( C )
A)每个函数至少要有一个参数 B)每个函数都必须返回一个值
C)函数在被调用之前必须先声明 D)函数不能自己调用自己
- 下列运算符中,不能重载的是 ( C )
A)&& B)!= C). D)->
- 下面程序的输出结果是( B )
#include <iostream>
using namespace std;
int i = 0;
int fun(int n)
{
static int a = 2;
a++;
return a+n;
}
void main()
{
int k = 5;
{
int i = 2;
k += fun(i);
}
k += fun(i);
cout << k;
}
A)13 B)14 C)15 D)16
- 下面的程序段的运行结果为( D )
char str[] = "job", *p = str;
cout << *(p+2) << endl;
A)98 B)无输出结果 C)字符’b’的地址 D)字符’b’
- 下面程序的输出结果是( C )
#include <iostream>
using namespace std;
class A
{
public:
A (int i) { x = i; }
void dispa () { cout << x << “,”; }
private :
int x ;
};
class B : public A
{
public:
B(int i) : A(i+10) { x = i; }
void dispb() { dispa(); cout << x << endl; }
private :
int x ;
};
void main()
{
B b(2);
b.dispb();
}
A)10,2 B)12,10 C)12,2 D)2,2
- 下面程序的输出结果是( C )
#include <iostream>
using namespace std;
class Base
{
public:
Base(int i) { cout << i; }
~Base () { }
};
class Base1: virtual public Base
{
public:
Base1(int i, int j=0) : Base(j) { cout << i; }
~Base1() {}
};
class Base2: virtual public Base
{
public:
Base2(int i, int j=0) : Base(j) { cout << i; }
~Base2() {}
};
class Derived : public Base2, public Base1
{
public:
Derived(int a, int b, int c, int d) : mem1(a), mem2(b), Base1(c),
Base2(d), Base(a)
{ cout << b; }
private:
Base2 mem2;
Base1 mem1;
};
void main() { Derived objD (1, 2, 3, 4); }
A)134122 B)123412 C)14302012 D)143212
- 下面程序的输出结果是( C )
#include <iostream>
using namespace std;
class Base
{
public:
virtual void f() { cout << “f0+”; }
void g() { cout << “g0+”; }
};
class Derived : public Base
{
public:
void f() { cout << “f+”; }
void g() { cout << “g+”; }
};
void main() { Derived d; Base *p = &d; p->f(); p->g(); }
A)f+g+ B)f0+g+ C)f+g0+ D)f0+g0+
- 下面程序的输出结果是( C )
#include <iostream>
using namespace std;
class Sample
{
friend long fun (Sample s)
{
if (s.x < 2) return 1;
return s.x * fun(Sample(s.x-1));
}
public:
Sample (long a) { x = a; }
private:
long x;
};
void main()
{
int sum = 0;
for (int i=0; i<4; i++)
{
sum += fun(Sample(i));
}
cout << sum;
}A)12 B)16 C)10 D)34
- 以下程序的输出结果是:(D )
#include <iostream>
using namespace std;
int fun(char *s)
{ char *p=s;
while (*p!='\0') p++;
return (p-s);
}
void main(){
cout<<fun("abc")<<endl;
}
A.0 B. 1 C. 2 D. 3
- 有如下程序段:
int i=1;
while(1)
{
i++;
if (i==10) break;
if(i%2==0) cout<<’*’;
}
执行这个程序段输出字符*的个数是(C)
A. 10 B. 3 C. 4 D.5
- 下列关于虚基类的描述中,错误的是(C)
A. 使用虚基类可以消除由多继承产生的二义性
B. 构造派生类对象时,虚基类的构造函数只被调用一次
C. 声明 class B:virtual public A 说明类B为虚基类
D. 建立派生类对象时,首先调用虚基类的构造函数
- 有下类定义
Class A {
Char *a;
Public:
A():a(0){}
A(char *aa) {//把aa所指字符串拷贝到a所指向的存储空间
A=___________________;
Strcpy(a,aa);
~A() {delete [] a;}
};
正确的选择是(A)
A. new char[strlen(aa)+1] B. char[strlen(aa)+1]
C. char[strlen(aa)] D. new char[sizeof(aa)-1]
- 假定AA为一个类,a为该类私有的数据成员,GetValue( )为该类公有函数成员,它返回a的值,x为该类的一个对象,则访问x对象中数据成员a的格式为( X )。
A) x.a B) x.a() C) x->GetValue() D) x.GetValue( )
- 假定AA为一个类,int a()为该类的一个成员函数,若该成员函数在类定义体外定义,则函数头为( A )。
A) int AA::a( ) B) int AA:a()
C) AA::a() D) AA::int a()
- 有如下程序:
#include<iostream>
using namespace std;
class Test{
public:
Test(){}
~Test(){cout<<'#';}
};
int main(){
Test temp[2], *pTemp[2];
return 0;
}
执行这个程序输出星号(#)的个数为(B )。
A)1 B)2 C)3 D)4
- 有如下程序:
#include<iostream>
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; return *this;}
~MyClass(){cout<<4;}
};
int main(){
MyClass obj1(1),obj2(2),obj3(obj1);
obj1=obj2;
return 0;
}运行时的输出结果是( A)。
A)1123444 B)11114444 C)121444 D)11314444
- 有如下程序:
#include <iostream>
using namespace std;
class point
{
public:
static int number;
public:
point() { number++;}
~point() {number--;}
};
int point::number=0;
void main()
{ point *ptr;
point A,B;
{
point *ptr_point=new point[3];
ptr=ptr_point;
}
point C;
delete[] ptr;
cout<<point::number;
}
程序结果为(A)
A)3 B)4 C)6 D)7
- 友元运算符obj++被C++编译器解释为( D )。
A. operator++(obj) B. operator++(obj,0)
B. obj.operator++( ) D. obj.operator++(0)
二、程序填空
1.用递归法求从1到n的立方和:
f(1)=1 (n=1)
f(n)=f(n-1)+n3 (n>1)
#include <iostream>
using namespace std;
int f(int);
int main(){
int n,s;
cout<<"input the number n:";
cin>>n;
s= f(n) ;
cout<<"The result is "<<s<<endl;
return 0;
}
int f(int n){ //递归法求立方和函数
if ( n==1 ) return 1;
else return ( f(n-1)+n*n*n );
}
3. 请将下列程序补充完整,使得输出结果为“Destructor Derived Destructor Base”。
#include <iostream>
using namespace std;
class Base
{
public:
virtual ~Base () { cout << “Destructor Base”<< endl; }
};
class Derived : public Base
{
public:
~Derived(){ cout << “Destructor Derived” << endl; }
};
void main ()
{
Base *pBase = new Derived;
delete pBase ;
}
三、阅读程序,写出运行结果
1.
#include<iostream>
#include<cstring>
using namespace std;
class CD
{
char name[20];
int number;
public:
void Init(char* aa, int bb)
{
strcpy(name,aa);
number=bb;
}
char* Getname () {return name;}
int Getnumber () {return number;}
void Output() {cout<< name <<' '<< number <<endl;}
};
CD dx;
void main()
{
CD dy;
dx.Init("歌唱祖国",30);
dy.Init("义勇军进行曲",3*dx.Getnumber ()+5);
dx.Output();
dy.Output();
}
歌唱祖国 30
义勇军进行曲 95
2.#include <iostream>
using namespace std;
int f(int a){
return ++a;
}
int g(int& a){
return ++a;
}
void main(){
int m=0,n=0;
m+=f(g(m));
n+=f(f(n));
cout<<"m="<<m<<endl;
cout<<"n="<<n<<endl;
}
3. #include <iostream>
using namespace std;
class Demo
{
public:
Demo(){cout<<"default constructor\n";}
Demo(const Demo &x){cout<<"copy constructor\n";}
};
Demo userCode(Demo b){Demo c(b);return c;}
void main()
{
Demo a,d;
cout<<"calling userCode()\n";
d = userCode(a);
}
执行上面的程序的过程中,构造函数Demo()和Demo(const Demo &x)被调用的次数分别是多少次?