做一些基本题
1、下面两个myfunc是重载还是覆盖?
namespace IBM
{
int myfunc(int a);
namespace SUN
{
int myfunc(double b);
}
}
2、假如运行环境int类型4bytes,short类型2bytes,long类型8bytes,存在代码:
unsigned short x=65530;
int a=myfunc(x,20.0);
会优先匹配以下哪一个重载函数?为什么?
A.int myfunc(double,double)
B.int myfunc(short,double)
C.double myfunc(int,float)
D.double myfunc(int,double)
3.
template <typename T>
inline T const & max(T const & a, T const & b)
...
下面哪个会引起编译错误,为什么?
a.max(4,7)
b.max(4,4.2)
c.max(static_cast<double>(4),4.2)
d.max<double>(4,4.2)
4.
#include <string>
template <typename T>
inline T max(T a,T b)
{
return a<b?b:a;
}
下面哪个调用会引起编译错误,为什么?
a.::max("apple","peach")
b.std::string s; ::max("apple",s)
5.下面程序正确的输出是哪个?
#include <iostream>
class BaseException
{
};
class DerivedException::public BaseException
{
};
void makeExcp()
{
BaseException& excp=DerivedException();
throw excp;
}
int main(int argc,char* argv[])
{
try
{
makeExcp();
}
catch(DerivedException&)
{
std::cout<<"caught DerivedException"<<std::endl;
}
catch(BaseException&)
{
std::cout<<"caught BaseException"<<std::endl;
}
catch(...)
{
std::cout<<"caught other Exception"<<std::endl;
}
return 0;
}
a.caught DerivedException
b.caught BaseException
c.caught other Exception
d.没有输出
6.对于名字空间以下哪个说法是错误的:
A.using声明可用于类的声明中
B.using指示具有传递性
C.如果某函数访问其自身所在名字空间中成员元素时,可以直接访问
C.通过使用using指示引入存在有相同成员元素的多个名字空间,在实际存取这些存在冲突的成员元素之前,也会导致当前作用域中
元素冲突的错误发生。
7.在编译过程中处理#include语句额阶段是:
A.预编译 B.词法分析 C.语法分析 D.语义分析 E.二进制代码生成
8.下面说法是否正确:
#include机制用于将源程序片段收集在一起,形成一个完整的编译单元作为编译器的输入。
9.下列初始化中,不合法的赋值有:
A.int i=-1;
B.const int ic=i;
C.const int *pic=
D.int * const cpi=
10.比较代码一和代码二的区别,选择错误说法:
//代码一
int *func(int, int)
{
...
return 0;
};
typedef int *(PF)(int, int);
PF getFunc()
{
return func;
}
int main()
{
PF pFunc = getFunc();
return 0;
}
//代码二
int func(int, int)
{
...
return 0;
};
typedef int (*PF)(int, int);
PF getFunc()
{
return func;
}
int main()
{
PF pFunc = getFunc();
return 0;
}
下列说法哪个错误:
A.两个代码都编译通过
B.代码一getFunc()编译错误,返回类型不能为函数类型
11.
int defaultFunc(const int &iTmp1, const int &iTmp2)
{
if(iTmp1 >= iTmp2)
{
return iTmp1;
}
else
{
return iTmp2;
}
}
int CompaerFunc(const int &iTmp1, const int &iTmp2)
{
if(iTmp1 < iTmp2)
{
return iTmp1;
}
else
{
return iTmp2;
}
}
typedef int (*PF)(const int &, const int &);
int call(int iTmp1, int iTmp2, PF pFunc = defaultFunc)
{
return pFunc(iTmp1, iTmp2);
}
int main(int argc, char *argv[])
{
int iTmp1 = 1;
int iTmp2 = 2;
PF pCompare = CompareFunc;
printf("result:%d\n",call(iTmp1, iTmp2));
printf("result:%d\n",call(iTmp1, iTmp2, pCompare));
printf("result:%d\n",call(iTmp1, iTmp2, CompareFunc));
return 0;
}
说法错误的是:
A.printf("result:%d\n",call(iTmp1, iTmp2));输出结果为result:2
B.printf("result:%d\n",call(iTmp1, iTmp2, pCompare));输出结果为result:1
C.call(iTmp1, iTmp2)中调用defaultFunc函数
D.call(iTmp1, iTmp2, pCompare)第三个参数传入CompareFunc()的函数指针
E.printf("result:%d\n",call(iTmp1, iTmp2, CompareFunc));此句编译错误
12.
下面这句是否正确:
函数执政可以用0来初始化赋值,以表示该指针不指向任何函数。
13.下面选项中哪个不能应用预赋值表达式处?
int main
{
int a[10];
int *p=NULL;
赋值表达式的位置;
return 0;
}
A.p=a;
B.p=&a;
C.p=&a[0];
D.p=&a[9];*p=1;
14.下面选项中哪个不能应用预赋值表达式处?
int main
{
int a[10];
int *p=NULL;
p=&a[4];
赋值表达式的位置;
printf("Hello World!\n");
return 0;
}
A.*(p++);
B.*(++p);
C.(*p)++;
D.*(p--);
15.
class CTest
{
public:
CTest(){}
~CTest(){}
};
定义一个类的内存分配函数错误的是:
A.static void *operator new(size_t sz);
B.void *operator new(size_t sz);
C.static void *operator new(size_t sz, char chFill);
C.void *operator new();
16.
class Base
{
public:
virtual void display(std::string strShow="I am Base class!")
{
std::cout<<strShow<<std::endl;
}
virtual ~Base(){}
};
class Derive:public Base
{
public:
virtual void display(std::string strShow="I am Derive class!")
{
std::cout<<strShow<<std::endl;
}
virtual ~Derive(){}
};
int main()
{
Base *pBase=new Derive();
Derive *pDerive=new Derive();
pBase->display();
pDerive->display();
delete pBase;
delete pDerive;
return 0;
}
程序输出结果是:
A.I am Base class!
I am Base class!
B.I am Derive class!
I am Derive class!
C.I am Base class!
I am Derive class!
D.I am Derive class!
I am Base class!
17.
class Base
{
public:
virtual void display() const //注意这里const
{
std::cout<<"I am Base class!"<<std::endl;
}
virtual ~Base(){}
};
class Derive:public Base
{
public:
virtual void display()
{
std::cout<<"I am Derive class!"<<std::endl;
}
virtual ~Derive(){}
};
int main()
{
Base *pBase=new Derive();
Derive *pDerive=new Derive();
pBase->display();
pDerive->display();
delete pBase;
delete pDerive;
return 0;
}
程序输出结果是:
A.I am Base class!
I am Base class!
B.I am Derive class!
I am Derive class!
C.I am Base class!
I am Derive class!
D.I am Derive class!
I am Base class!
18.下面这句说法是否正确:
子类可以访问父类的保护成员,子类的友元类也可以通过子类对象去访问父类的保护成员。
19.类的构造函数可不可以是虚函数,为什么?
20.下面说法是否正确:
全局类对象的构造函数总是在main函数之前执行的
21.下面说法正确的是:
A.纯虚函数是一种特殊的虚函数,它是个空函数
B.一个基类中说明有纯虚函数,其派生类一定要实现该纯虚函数
C.具有纯虚函数的类不能创建类对象
22.类B是通过public继承方式从类A派生而来的,且类A和类B都有完整的实现代码,那么下列说法正确的是:
A.类B中具有public可访问性的成员函数个数一定不少于类A中public成员函数的个数
B.一个类B的实例对象占用的内存空间一定不少于一个类A的实例对象占用的内存空间
C.只要类B中的构造函数都是public的,在main函数中就可以创建类B的实例对象
D.类A和类B中的同名虚函数的返回值类型必须完全一致
23.下面叙述正确的是:
A.当基类的析构函数是虚函数时,它的派生类的析构函数也是虚函数
B.析构函数调用虚函数采用动态联编
namespace IBM
{
int myfunc(int a);
namespace SUN
{
int myfunc(double b);
}
}
2、假如运行环境int类型4bytes,short类型2bytes,long类型8bytes,存在代码:
unsigned short x=65530;
int a=myfunc(x,20.0);
会优先匹配以下哪一个重载函数?为什么?
A.int myfunc(double,double)
B.int myfunc(short,double)
C.double myfunc(int,float)
D.double myfunc(int,double)
3.
template <typename T>
inline T const & max(T const & a, T const & b)
...
下面哪个会引起编译错误,为什么?
a.max(4,7)
b.max(4,4.2)
c.max(static_cast<double>(4),4.2)
d.max<double>(4,4.2)
4.
#include <string>
template <typename T>
inline T max(T a,T b)
{
return a<b?b:a;
}
下面哪个调用会引起编译错误,为什么?
a.::max("apple","peach")
b.std::string s; ::max("apple",s)
5.下面程序正确的输出是哪个?
#include <iostream>
class BaseException
{
};
class DerivedException::public BaseException
{
};
void makeExcp()
{
BaseException& excp=DerivedException();
throw excp;
}
int main(int argc,char* argv[])
{
try
{
makeExcp();
}
catch(DerivedException&)
{
std::cout<<"caught DerivedException"<<std::endl;
}
catch(BaseException&)
{
std::cout<<"caught BaseException"<<std::endl;
}
catch(...)
{
std::cout<<"caught other Exception"<<std::endl;
}
return 0;
}
a.caught DerivedException
b.caught BaseException
c.caught other Exception
d.没有输出
6.对于名字空间以下哪个说法是错误的:
A.using声明可用于类的声明中
B.using指示具有传递性
C.如果某函数访问其自身所在名字空间中成员元素时,可以直接访问
C.通过使用using指示引入存在有相同成员元素的多个名字空间,在实际存取这些存在冲突的成员元素之前,也会导致当前作用域中
元素冲突的错误发生。
7.在编译过程中处理#include语句额阶段是:
A.预编译 B.词法分析 C.语法分析 D.语义分析 E.二进制代码生成
8.下面说法是否正确:
#include机制用于将源程序片段收集在一起,形成一个完整的编译单元作为编译器的输入。
9.下列初始化中,不合法的赋值有:
A.int i=-1;
B.const int ic=i;
C.const int *pic=
D.int * const cpi=
10.比较代码一和代码二的区别,选择错误说法:
//代码一
int *func(int, int)
{
...
return 0;
};
typedef int *(PF)(int, int);
PF getFunc()
{
return func;
}
int main()
{
PF pFunc = getFunc();
return 0;
}
//代码二
int func(int, int)
{
...
return 0;
};
typedef int (*PF)(int, int);
PF getFunc()
{
return func;
}
int main()
{
PF pFunc = getFunc();
return 0;
}
下列说法哪个错误:
A.两个代码都编译通过
B.代码一getFunc()编译错误,返回类型不能为函数类型
11.
int defaultFunc(const int &iTmp1, const int &iTmp2)
{
if(iTmp1 >= iTmp2)
{
return iTmp1;
}
else
{
return iTmp2;
}
}
int CompaerFunc(const int &iTmp1, const int &iTmp2)
{
if(iTmp1 < iTmp2)
{
return iTmp1;
}
else
{
return iTmp2;
}
}
typedef int (*PF)(const int &, const int &);
int call(int iTmp1, int iTmp2, PF pFunc = defaultFunc)
{
return pFunc(iTmp1, iTmp2);
}
int main(int argc, char *argv[])
{
int iTmp1 = 1;
int iTmp2 = 2;
PF pCompare = CompareFunc;
printf("result:%d\n",call(iTmp1, iTmp2));
printf("result:%d\n",call(iTmp1, iTmp2, pCompare));
printf("result:%d\n",call(iTmp1, iTmp2, CompareFunc));
return 0;
}
说法错误的是:
A.printf("result:%d\n",call(iTmp1, iTmp2));输出结果为result:2
B.printf("result:%d\n",call(iTmp1, iTmp2, pCompare));输出结果为result:1
C.call(iTmp1, iTmp2)中调用defaultFunc函数
D.call(iTmp1, iTmp2, pCompare)第三个参数传入CompareFunc()的函数指针
E.printf("result:%d\n",call(iTmp1, iTmp2, CompareFunc));此句编译错误
12.
下面这句是否正确:
函数执政可以用0来初始化赋值,以表示该指针不指向任何函数。
13.下面选项中哪个不能应用预赋值表达式处?
int main
{
int a[10];
int *p=NULL;
赋值表达式的位置;
return 0;
}
A.p=a;
B.p=&a;
C.p=&a[0];
D.p=&a[9];*p=1;
14.下面选项中哪个不能应用预赋值表达式处?
int main
{
int a[10];
int *p=NULL;
p=&a[4];
赋值表达式的位置;
printf("Hello World!\n");
return 0;
}
A.*(p++);
B.*(++p);
C.(*p)++;
D.*(p--);
15.
class CTest
{
public:
CTest(){}
~CTest(){}
};
定义一个类的内存分配函数错误的是:
A.static void *operator new(size_t sz);
B.void *operator new(size_t sz);
C.static void *operator new(size_t sz, char chFill);
C.void *operator new();
16.
class Base
{
public:
virtual void display(std::string strShow="I am Base class!")
{
std::cout<<strShow<<std::endl;
}
virtual ~Base(){}
};
class Derive:public Base
{
public:
virtual void display(std::string strShow="I am Derive class!")
{
std::cout<<strShow<<std::endl;
}
virtual ~Derive(){}
};
int main()
{
Base *pBase=new Derive();
Derive *pDerive=new Derive();
pBase->display();
pDerive->display();
delete pBase;
delete pDerive;
return 0;
}
程序输出结果是:
A.I am Base class!
I am Base class!
B.I am Derive class!
I am Derive class!
C.I am Base class!
I am Derive class!
D.I am Derive class!
I am Base class!
17.
class Base
{
public:
virtual void display() const //注意这里const
{
std::cout<<"I am Base class!"<<std::endl;
}
virtual ~Base(){}
};
class Derive:public Base
{
public:
virtual void display()
{
std::cout<<"I am Derive class!"<<std::endl;
}
virtual ~Derive(){}
};
int main()
{
Base *pBase=new Derive();
Derive *pDerive=new Derive();
pBase->display();
pDerive->display();
delete pBase;
delete pDerive;
return 0;
}
程序输出结果是:
A.I am Base class!
I am Base class!
B.I am Derive class!
I am Derive class!
C.I am Base class!
I am Derive class!
D.I am Derive class!
I am Base class!
18.下面这句说法是否正确:
子类可以访问父类的保护成员,子类的友元类也可以通过子类对象去访问父类的保护成员。
19.类的构造函数可不可以是虚函数,为什么?
20.下面说法是否正确:
全局类对象的构造函数总是在main函数之前执行的
21.下面说法正确的是:
A.纯虚函数是一种特殊的虚函数,它是个空函数
B.一个基类中说明有纯虚函数,其派生类一定要实现该纯虚函数
C.具有纯虚函数的类不能创建类对象
22.类B是通过public继承方式从类A派生而来的,且类A和类B都有完整的实现代码,那么下列说法正确的是:
A.类B中具有public可访问性的成员函数个数一定不少于类A中public成员函数的个数
B.一个类B的实例对象占用的内存空间一定不少于一个类A的实例对象占用的内存空间
C.只要类B中的构造函数都是public的,在main函数中就可以创建类B的实例对象
D.类A和类B中的同名虚函数的返回值类型必须完全一致
23.下面叙述正确的是:
A.当基类的析构函数是虚函数时,它的派生类的析构函数也是虚函数
B.析构函数调用虚函数采用动态联编
...全文


