c/c++笔试题

C/C++笔试题

1、字符串比较,同strcmp的功能,以下为仿函数版本。
struct StringCmp
{
 int operator()( const char* lpszStr1, const char* lpszStr2 )
 {
  if ( NULL == lpszStr1 )
  {
   if ( NULL == lpszStr2 ) return 0;
   return -1;
  }
  if ( NULL == lpszStr2 ) return 1;

  for ( ; ( 0 != ( ( *lpszStr1 ) & ( *lpszStr2 ))); ++ lpszStr1, ++ lpszStr2 )
  {
   if ( *lpszStr1 < *lpszStr2 ) return -1;
   if ( *lpszStr1 > *lpszStr2 ) return 1;
  }
  if ( 0 != *lpszStr2 ) return -1;
  if ( 0 != *lpszStr1 ) return 1;
  return 0;
 }
};

2.
4、判断字符串是否为回环串,类似于"abcdcba"(?),这个可能挺简单,但是好像很多大公司出的
笔试题似乎都很简单,不知其考察点在于什么。。。
template < class Character = char >
struct IsCircleString
{
 typedef Character char_t;
 bool operator()( const char_t* pStr )
 {
  const char_t* pLast = pStr;
  for ( ; 0 != *pLast; ++ pLast ) {}
  if ( ( ( pLast - pStr ) & 0x1 ) == 0 ) return false;//偶数即返回false
  -- pLast;
  for ( ; pStr < pLast; ++ pStr, -- pLast )
  {
   if ( *pStr != *pLast ) return false;
  }
  return true;
 }
};

3.变量的声明和定义有什么区别? 

答: 
变量声明只是给编译器一个提示,和运行环境无关;而变量定义是分配了具体的内存空间。 
比如: 
extern int a; //变量声明 
int b; //变量定义 
4.请写出下面代码在 32 位平台上的运行结果,并说明 sizeof 的性质: 

#include <stdio.h> 
#include <stdlib.h> 

int main(void) 

char a[30]; 
char *b = (char *)malloc(20 * sizeof(char)); 
printf("%d/n", sizeof(a)); 
printf("%d/n", sizeof(b)); 
printf("%d/n", sizeof(a[3])); 
printf("%d/n", sizeof(b+3)); 
printf("%d/n", sizeof(*(b+4))); 
return 0 ; 

答: 
在32位系统下(如WIN32),指针长度为32位,并且, 
a是一个有30个元素的字符型数组;b是一个字符串指针;a[3]是字符型;b+3是指针;*(b+4)是字符型。 
因此输出: 
30、4、1、4、1 
一、 单项选择题(从四个备选答案中选择一个正确答案,每小题1分,共20分) 
1. C++中,关键字struct和class的区别仅仅在于( C )。 

(A)struct 用来定义结构体, 而class用来定义类; 

  (B)struct 用来定义类, 而class用来定义类结构体; 

(C)struct定义的类的缺省成员为公有的,而class定义的类的缺省成员为私有的; 

(D)struct定义的类的成员须全部为公有的,而class定义的类的成员可以为私有的; 

2. 以下程序执行后,输出结果为( C )

#i nclude 

int Var=3 ; 

void main(void) 

{ int Var=10; 

::Var++; 

cout<<”Var=”< 


(A)Var=11, ::Var=11 (B)Var=11, ::Var=3 

(C)Var=10, ::Var=4 (D)Var=4, ::Var=10 

3. 抽象基类是指( C )。 

(A)嵌套类      (B)派生类 

  (C)含有纯虚函数 (D)多继承类 

4.如果有#define AREA(a,b)a+b 则语句int s=AREA(3,4)*AREA(3,4)执行后变量s值为( D )。 

(A) 24 (B)49 (C)144 (D)19 

5. C++中条件表达式的值为( C )。 

(A)–1或者+1  (B)–231~231 –1 (C)0或者1 (D) 0~231–1 

6. 现在有以下语句: 

struct MyBitType 

{ int a:3; 

unsigned b:3; 

unsigned c:20; 

int d; 

}; 

int sz=sizeof(MyBitType); 

则执行后,变量sz的值将得到( B)。 

(A)2  (B)8 (C)28 (D)58 

7. 假设有一个C++类名为Country, 则此类的析构函数为( C ). 

(A)::Country() (B)void ~Country(void) 

(C)~Country() (D)void ~Country() 

8. 如果定义一个C++类CDate, 语句“CDate *p = new CDate;”的含义是( A )。 

(A)调用CDate类的缺省构造函数从内存中分配一个CDate类型的对象,并将该对象地址赋值给指针p; 

  (B)调用CDate类的拷贝构造函数从内存中分配一个CDate类型的对象,并将该对象地址赋值给指针p; 

(C)调用CDate类的析构函数从内存中分配一个CDate类型的对象,并将该对象地址赋值给指针p; 

(D)从内存中释放指针p所指向的CDate类的对象; 

9.如果有一个类CRect及语句“CRect x1, x2;” 要使语句 “x1=x2;”合法,可在类中定义成员函数( C )。 

 (A) int operator(x2)        (B)int operator=(x2) 

(C) void operator=(CRect &); (D) void operator=() 

10. 结构体变量S实际所占内存的大小为( A )字节。 

(A)sizeof(S)         (B)strlen(S) 

(C)结构体中最长成员的长度 (D)结构体中最短成员的长度 

11.在C++中,下列算符( D )不能重载。 

(A)<<  (B)>> (C)delete (D):: 

12.下列正确的是( D ) 

(A)结构不能有构造函数、析构函数; (B)缺省时类的成员是公有的; 

(C)类中如果定义了析构函数,则必须定义构造函数; 

(D)缺省时结构的成员是公有的; 

13. 下列关于静态数据成员正确的是( B ) 

(A)可以通过this指针访问静态数据; (B)可以用类名和作用域访问静态数据; 

(C)静态数据在类内声明并初始化; (D)只能由该类的静态成员函数访问; 

14. 下列关于友元正确的说法是(  D ) 

(A)友元只能在类的public区声明;(B)友元具有this指针; 

(C)类的成员函数不能声明为另一个类的友元; 

(D)一个函数如果被声明为一个类的友元,则该函数具有访问该类私有成员的权利。 

15. 基类的( A )在派生类内不能被访问。 

(A)私有成员 (B)保护成员 

(C)公有数据成员 (D)公有静态数据成员 

16. 下列关于运算符重载的描述中正确的是( D ) 

(A)运算符重载可以改变该运算符的优先级; 

  (B)运算符重载可以改变该运算符目数,即该算符运算的操作数个数; 

(C)运算符重载函数只能在类中定义; 

(D)new和delete允许重载; 

17.左值是指( A ) 

(A)赋值算符左边的变量;   (B)赋值算符左边的表达式的值; 

(D)出现在赋值算符右边的表达式的值; 

(E)二元算符左边表达式的值; 

18. 下列为纯虚函数的正确声明的是( B ) 

(A)void virtual print()=0;   (B)virtual void print()=0; 

(C)virtual void print(){ }; (D)virtual void print(); 

19. 如果在类对象a的类中重载运算符“+”,则a+5的显示调用方式为( C ) 

(A)a.operator(5)   (B)a->operator+(5); 

(C)a.operator+(5) (D)5.operator+(a) 

20.一个类如果有一个以上的基类就叫做( B )。                  

(A)循环继承    (B)单继承 

(C)非法继承 (D)多继承 

二、 多项选择题(从五个备选答案中选择2~5个正确答案,每小题2分,共10分) 
1. 如果已定义了一个C++类CMyList并有以下语句: 

CMyList list(3); 

以下说法正确的是( AC )。 

 (A)该语句会创建一个CMyList类的一个对象; 

 (B)该语句会创建一个CMyList类的3个对象; 

 (C)必须为类CMyList定义一个构造函数; 

(D)必须为类CMyList定义一个析构函数; 

(E) 必须定义函数CMyList list(int); 

2. 以下说法正确的是( ABCDE )。 

(A)内联(inline)函数改善了函数调用的执行效率。 

  (B)类A的友元(friend)函数可以访问类A的私有成员。 

(C)类A的友元(friend)类B可以访问类A的私有成员。 

(D)类A的静态数据成员为类A的所有对象所共享。 

(E)类A的静态成员函数没有传递this 指针作为参数。 

3.类B从类A派生,则类B可以访问类A中的( AC )成员。 

(A)public成员 (B)private成员 (C)protected成员 

(D)数据成员   (E)函数成员 

4. 面向对象的程序设计语言具有( ABE )等共同特性。 

(A)封装性 (B)多态性 (C)简单性 (D)复杂性 

(E)继承性 

5. 现有一个程序如下: 

#i nclude 

class A 

{ public: 

void f(){ cout<< "A::f()"< 
}; 

class B 

{ public: 

void f(){ cout<< "B:f()"< 
void g(){ cout<< "B:g()"< 
}; 

class C:public A, public B 

{ public: 

void g(){ cout<<"C::g()"< 
void h() 

{ cout<<"C::h()"< 
f(); //语句1 



}; 

void main() 

{ C obj; 

obj.f();    //语句2 

obj.A::f(); //语句3 

obj.B::f(); //语句4 

obj.g();   //语句5 



则编译时会产生错误的语句有( AB )。 

(A)语句1     (B)语句2   (C)语句3 

(D)语句4    (E)语句5 

三、 判断分析题(正确的画上√,错误的画上×,每小题1分,共10分) 
1.如果一个类的所有对象都共享某一个变量,则应当将该变量定义为该类的static成员。 ( √ ) 

2.语句“ typedef struct _MYSTRUC { int x; int y; double z; } MYSTRUC; ”是非法语句。 ( × ) 

3.语句“ int (*p)(int x, int y);”说明了p是一个指向整数的指针。 ( × ) 

4.Visual C++集成开发环境中,一个Workspace中只能创建一个Project。 ( × ) 

5.能访问一个类CMyClass中的private成员的可以是类CMyClass的成员函数,友元函数和友元类中的函数。 ( √ ) 

6. 所有的MFC应用程序都必须有一个且只有一个CWinApp对象。 ( √ ) 

7.C++中的多态性就是指在编译时,编译器对同一个函数调用,根据情况调用不同的实现代码。 ( × ) 

8.在for循环中,是先执行循环体后再判断循环条件。 ( × ) 

9.C++中,如果条件表达式值为-1, 则表示逻辑为假。 ( × ) 

10. 在C++中用new分配的内存空间,在不需要时一般用free将该空间释放。 ( × ) 

四、 填空题(每空2分,共20分) 
1.以下函数完成求表达式 的值,请填空使之完成此功能。 

float sum( float x ) 

{ float s=0.0; 

int sign=1; 

float t=1.0; 

for(int i=1; i<=100; i++) 



t=t*x; 

s=s+-sign*i/(t+sign*i); 

sign=-sign; 



return s; 



2.以下程序中实现类CSort, 完成对其成员p所指向的整数数组进行从小到大排序,该数组的元素个数由num表示,请填空完善该程序。 

#i nclude 

class CSort 



int *p; 

int num; 

public: 

void Order(); 

CSort(int *, int); 

void Disp(); 

}; 

CSort::CSort(int *arry, int n) 

(arry), num(n) 

{ } 

void CSort::Order() //函数Order原型 

{ int m, tmp; 

for(int i=0; i 
{ m=i; 

for(int j=i+1; j 
{ if(p[j] 
m=j; 



if(m!=i) 

{ tmp=p[i]; 

p[i]=p[m]; 

p[m]=tmp; 







void CSort::pisp() 

{ for(int i=0; i 
cout< 
cout< 


void main( ) 

{ static int a[]={10, -15, -3, 5, -4, 7,2}; 

CSort obj(a,2); 

obj.Disp(); //应输出一行:10,-15,-3,5,-4,7,2 

obj.Order(); //对数组排序 

obj.Disp(); //应输出一行:-15,-4,-3,2,5,7,10 



3.以下函数完成求两个数n1和n2的最大公约数。 

#i nclude 

int fac(int n1, int n2) 

{ int tmp; 

if( n1 < n2 ) 

{ tmp=n1; 

n1=n2 ; 

n2=tmp ; 



while(n1%n2!=0) 

{ tmp=n1%n2; n1=n2; n2=tmp; 



return n2; 



五、 阅读程序题(每个小题5分,共20分) 
1.阅读以下程序,概括地写出程序的功能。 

#i nclude 

double Exp(double x) 

{ double sum=1.0; 

double term=x; 

double i=1 ; 

while (term>=1.0E-8) 

{ sum+=term ; 

i++; 

term=term*x/i ; 



return sum ; 



void main() 

{ double s; 

s=Exp(1.0)+Exp(2.0); 

cout.precision(8); 

cout<<"s="< 


zz 

2. 阅读程序,写出程序执行时输出结果。 

#i nclude 

const int SIZE=10; 

class stack 

{ char stck[SIZE]; 

int top; 

public: 

void init(); 

void push(char ch); 

char pop(); 

}; 

void stack::init() 

{ top=0; } 

void stack::push(char ch) 

{ if(top==SIZE) 

 { cout<<"Stack is full./n"; 

return ; 

 } 

stck[top++]=ch; 



char stack::pop() 

{ if(top==0) 

   { cout<<"Stack is empty./n"; 

   return 0; 



return stck[--top]; 



void main() 

{ stack s1, s2; 

 s1.init(); 

 s2.init(); 

 s1.push('a'); 

 s1.push('b'); 

 s1.push('c'); 

 s2.push('x'); 

 s2.push('y'); 

 s2.push('z'); 

 for(int i=0; i<3; i++) 

cout<<"Pop s1:"< 
 for(i=0; i<3; i++) 

cout<<"Pop s2:"< 


程序结果: 

Pop s1: c 

Pop s1: b 

Pop s1: a 

Pop s2: z 

Pop s2: y 

Pop s2: z 

3.阅读程序,写出程序运行时输出结果。 

#i nclude 

class Tdate 

{ public: 

Tdate(); 

Tdate(int d); 

Tdate(int m, int d); 

Tdate(int m, int d, int y); 

protected: 

int month; 

int day; 

int year; 

}; 

Tdate::Tdate() 

{ month=4; 

   day=15; 

   year=1995; 

cout< 


Tdate::Tdate(int d) 

{ month=4; 

   day=d; 

   year=1996; 

cout< 


Tdate::Tdate(int m, int d) 

{ month=m; 

   day=d; 

   year=1997; 

cout< 


Tdate::Tdate(int m, int d, int y) 

{ month=m; 

   day=d; 

   year=y; 

cout< 


void main() 

{ Tdate aday; 

Tdate bday(10); 

Tdate cday(2,12); 

Tdate dday(1,2,1998); 



运行结果: 

4/15/1995 

4/10/1996 

2/12/1997 

1/2/1998 

4.阅读程序,写出程序运行时输出结果。 

#i nclude 

#i nclude 

class shape 

{ public: 

shape(double x, double y):xCoord(x), yCoord(y){} 

virtual double Area()const {return 0.0; } 

protected: 

double xCoord, yCoord; 

}; 

class AA :public shape 

{ public: 

AA(double x, double y, double r): shape(x,y), rad(r){} 

virtual double Area()const { return 3.0 * rad * rad; } 

protected: 

double rad; 

}; 

class BB :public shape 

{ public: 

BB(double x1, double y1, double x2, double y2) 

:shape(x1, y1), x2Coord(x2), y2Coord(y2){ } 

virtual double Area()const; 

protected: 

double x2Coord, y2Coord; 

}; 

double BB:Area()const 

{ return fabs((xCoord-x2Coord)* (yCoord - y2Coord)); 

//库函数fabs(double t)求得t的绝对值 



void fun(const shape& sp) 

{ cout< 


void main() 

{ AA aa(2.0, 5.0, 4.0); 

fun(aa); 

BB bb(2.0, 8.0, 12.0, 17.0); 

fun(bb); 



运行结果: 

48 

30 

六、 编写程序题(每小题10分,共20分) 
1.编写一个函数int Judge(int *pArray, int n),判断一个n×n二维整数数组pArray 是否为“魔方阵”,若是返回1,否则返回0。所谓 魔方阵就是将1到n2的各个数字组成的方阵,它的每一行、每一列以及两个对角线上数字之和均相等。例如,3×3的 中,A是魔方阵,而B不是魔方阵。然后在主程序中调用Judge函数判断数组A是否为魔方阵。 

参考程序 

#i nclude 

int Judge(int *pArray, int n) 

{ int s1, s2, s3,s4,sum=0; 

int *p=pArray; 

for(int i=1; i<= n*n; i++) 

{ int Found=0; //为0,不在方阵中; 

for(int j=0; j 
if(p[j]==i) 

{ Found=1; //为1,在方阵中 

break; 



if(Found==0) return 0; // 值为 i 的元素不在数组中,显然不是魔方阵 



for( i=1; i<=n*n; i++) 

sum=sum+i; 

sum=sum / n; // 各行、各列、对角线元素应当得到的和 

s3=0; 

s4=0; 

for( i=0; i 
{ s1=0, s2=0; 

p=pArray; 

for(int j=0; j 
{ s1=s1+p[i*n+j]; //第i行的元素和 

 s2=s2+p[j*n+i]; //第i列的元素和 



if ( s1!=sum) 

return 0; 

if ( s2!=sum) 

return 0; 

s3=s3+pArray[i*n+i];     // 对角线一元素和 

s4=s4+pArray[i*n+(n-1-i)]; // 对角线二元素和 



if(s3!=sum) 

return 0; 

if(s4 != sum) 

return 0; 

return 1; 



void main() 

{ int Array[3][3]={{ 8, 1, 6},{ 3, 5, 7},{ 4, 9, 2}}; 

if(Judge((int*)Array, 3)) 

cout<<"Yes, it's a magic array"< 
else 

cout<<"No, it isn't a magic array"< 

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值