2013年海康威视校园招聘笔试题

1、10、10、4、4四个数,怎么算出24点?
(10*10-4)/4=24
2、下列表达式在32位机器编译环境下的值()
  1. class
  2. }; 
  3.  
  4. class
  5. public
  6.     B(); 
  7.     virtual ~B(); 
  8. }; 
  9.  
  10. class
  11. private
  12. #pragma pack(4) 
  13.     int i; 
  14.     short j; 
  15.     float k; 
  16.     char l[64]; 
  17.     long m; 
  18.     char *p; 
  19. #pragma pack() 
  20. }; 
  21.  
  22. class
  23. private
  24. #pragma pack(1) 
  25.     int i; 
  26.     short j; 
  27.     float k; 
  28.     char l[64]; 
  29.     long m; 
  30.     char *p; 
  31. #pragma pack() 
  32. }; 
  33.  
  34. int main(void
  35.     printf("%d\n",sizeof(A)); 
  36.     printf("%d\n",sizeof(B)); 
  37.     printf("%d\n",sizeof(C)); 
  38.     printf("%d\n",sizeof(D)); 
  39.     return 0; 
class A
{
};

class B
{
public:
	B();
	virtual ~B();
};

class C
{
private:
#pragma pack(4)
	int i;
	short j;
	float k;
	char l[64];
	long m;
	char *p;
#pragma pack()
};

class D
{
private:
#pragma pack(1)
	int i;
	short j;
	float k;
	char l[64];
	long m;
	char *p;
#pragma pack()
};

int main(void)
{
	printf("%d\n",sizeof(A));
	printf("%d\n",sizeof(B));
	printf("%d\n",sizeof(C));
	printf("%d\n",sizeof(D));
	return 0;
}
A、1、4、84、82      B、4、4、82、84      C、4、4、84、82      D、1、4、82、82
3、以下程序在32位机器下运行的结果是()
  1. #pragma pack(4) 
  2. struct info_t 
  3.     unsigned char version; 
  4.     unsigned char padding; 
  5.     unsigned char extension; 
  6.     unsigned char count; 
  7.     unsigned char marker; 
  8.     unsigned char payload; 
  9.     unsigned short sequence; 
  10.     unsigned int timestamp; 
  11.     unsigned int ssrc; 
  12. }; 
  13.  
  14. union info_u 
  15.     unsigned char version; 
  16.     unsigned char padding; 
  17.     unsigned char extension; 
  18.     unsigned char count; 
  19.     unsigned char marker; 
  20.     unsigned char payload; 
  21.     unsigned short sequence; 
  22.     unsigned int timestamp; 
  23.     unsigned int ssrc; 
  24. }; 
  25. #pragma pack() 
  26.  
  27. int main(void
  28.     printf("%d\n",sizeof(info_t)); 
  29.     printf("%d\n",sizeof(info_u)); 
  30.     return 0; 
#pragma pack(4)
struct info_t
{
	unsigned char version;
	unsigned char padding;
	unsigned char extension;
	unsigned char count;
	unsigned char marker;
	unsigned char payload;
	unsigned short sequence;
	unsigned int timestamp;
	unsigned int ssrc;
};

union info_u
{
	unsigned char version;
	unsigned char padding;
	unsigned char extension;
	unsigned char count;
	unsigned char marker;
	unsigned char payload;
	unsigned short sequence;
	unsigned int timestamp;
	unsigned int ssrc;
};
#pragma pack()

int main(void)
{
	printf("%d\n",sizeof(info_t));
	printf("%d\n",sizeof(info_u));
	return 0;
}
A、12  12      B、12  4       C、16  4   D、16  12     E、16  1
4、以下表达式result的值是()
  1. #define VAL1(a,b) a*b 
  2. #define VAL2(a,b) a/b-- 
  3. #define VAL3(a,b) ++a%b 
  4.  
  5. int a = 1; 
  6. int b = 2; 
  7. int c = 3; 
  8. int d = 3; 
  9. int e = 5; 
  10.  
  11. int result = VAL2(a,b)/VAL1(e,b)+VAL3(c,d); 
#define VAL1(a,b) a*b
#define VAL2(a,b) a/b--
#define VAL3(a,b) ++a%b

int a = 1;
int b = 2;
int c = 3;
int d = 3;
int e = 5;

int result = VAL2(a,b)/VAL1(e,b)+VAL3(c,d);
A、-2    B、1     C、0     D、2
5、请写出以下程序的输出(5分)
  1. void swap_1(int a , int b) 
  2.     int c; 
  3.     c = a; 
  4.     a = b; 
  5.     b = c; 
  6.     return
  7. void swap_2(int &a , int &b) 
  8.     int c; 
  9.     c = a; 
  10.     a = b; 
  11.     b = c; 
  12.     return
  13. void swap_3(int *a , int *b) 
  14.     int c; 
  15.     c = *a; 
  16.     *a = *b; 
  17.     *b = c; 
  18.     return
  19.  
  20. int main(void
  21.     int a = 100; 
  22.     int b = 200; 
  23.     swap_1(a , b); 
  24.     printf("a = %d , b = %d\n",a , b); 
  25.     swap_2(a , b); 
  26.     printf("a = %d , b = %d\n",a , b); 
  27.     swap_3(&a , &b); 
  28.     printf("a = %d , b = %d\n",a , b); 
  29.     return 0; 
void swap_1(int a , int b)
{
	int c;
	c = a;
	a = b;
	b = c;
	return ;
}
void swap_2(int &a , int &b)
{
	int c;
	c = a;
	a = b;
	b = c;
	return ;
}
void swap_3(int *a , int *b)
{
	int c;
	c = *a;
	*a = *b;
	*b = c;
	return ;
}

int main(void)
{
	int a = 100;
	int b = 200;
	swap_1(a , b);
	printf("a = %d , b = %d\n",a , b);
	swap_2(a , b);
	printf("a = %d , b = %d\n",a , b);
	swap_3(&a , &b);
	printf("a = %d , b = %d\n",a , b);
	return 0;
}
输出结果:
a = 100 , b = 200
a = 200 , b = 100
a = 100 , b = 200
6、下面的程序是否有问题,如有问题,请重构代码(5分)
  1. void test_type(bool b , const char *p , float f) 
  2.     if(!b) 
  3.     { 
  4.         return
  5.     } 
  6.     else if(!p) 
  7.     { 
  8.         return
  9.     } 
  10.     else if(!f) 
  11.     { 
  12.         return
  13.     } 
void test_type(bool b , const char *p , float f)
{
	if(!b)
	{
		return ;
	}
	else if(!p)
	{
		return ;
	}
	else if(!f)
	{
		return ;
	}
}
修改如下:
  1. void test_type(bool b , const char *p , float f) 
  2.     if(!b) 
  3.     { 
  4.         return
  5.     } 
  6.     else if(!p) 
  7.     { 
  8.         return
  9.     } 
  10.     else if(f > -1e-10 && f < 1e-10) 
  11.     { 
  12.         return
  13.     } 
void test_type(bool b , const char *p , float f)
{
	if(!b)
	{
		return ;
	}
	else if(!p)
	{
		return ;
	}
	else if(f > -1e-10 && f < 1e-10)
	{
		return ;
	}
}
7、请指出以下程序有什么问题(5分)
  1. void test_mem() 
  2.     char *p = new char[64]; 
  3.     delete p; 
  4.     p = NULL; 
  5.     return
void test_mem()
{
	char *p = new char[64];
	delete p;
	p = NULL;
	return ;
}
应该修改为 delete[]p;  p指向的是一个字符型的数组空间,原来的代码只是简单的释放了指向申请空间的指针,并没有释放申请的空间,容易造成内存崩溃。
回收用 new 分配的单个对象的内存空间的时候用 delete,回收用 new[] 分配的一组对象的内存空间的时候用 delete[]。
8、以下程序有什么问题,请指出。
  1. char* GetMem() 
  2.     char p[] = "hello"
  3.     return p; 
  4.  
  5. void test_get_mem() 
  6.     char *p = GetMem(); 
  7.     printf(p); 
  8.     return
char* GetMem()
{
	char p[] = "hello";
	return p;
}

void test_get_mem()
{
	char *p = GetMem();
	printf(p);
	return ;
}
GetMem函数中的p是一个在栈上的局部变量,当函数运行结束的时候,栈上的内容会自动释放的,此处返回的值有可能会成为一个野指针,会出现一个意想不到的结果。
9、请写出strcpy 和 memcpy 的区别(5分)
答:strcpy和memcpy都是标准C库函数,它们有下面的特点。
strcpy提供了字符串的复制。即strcpy只用于字符串复制,并且它不仅复制字符串内容之外,还会复制字符串的结束符。
strcpy函数的原型是:char* strcpy(char* dest, const char* src);
memcpy提供了一般内存的复制。即memcpy对于需要复制的内容没有限制,因此用途更广。
memcpy函数的原型是:void *memcpy( void *dest, const void *src, size_t count );
strcpy和memcpy主要有以下3方面的区别。
1、复制的内容不同。strcpy只能复制字符串,而memcpy可以复制任意内容,例如字符数组、整型、结构体、类等。
2、复制的方法不同。strcpy不需要指定长度,它遇到被复制字符的串结束符"\0"才结束,所以容易溢出。memcpy则是根据其第3个参数决定复制的长度。
3、用途不同。通常在复制字符串时用strcpy,而需要复制其他类型数据时则一般用memcpy。

10、请写出以下程序的输出结果

  1. class Base 
  2. public
  3.     Base() 
  4.     { 
  5.         printf("I am Base()\n"); 
  6.     } 
  7.     virtual ~Base() 
  8.     { 
  9.         printf("I am ~Base()\n"); 
  10.     } 
  11. public
  12.     virtual void SayHello() 
  13.     { 
  14.         printf("Hello Base\n"); 
  15.     } 
  16.     void SayWorld() 
  17.     { 
  18.         printf("World Base\n"); 
  19.     } 
  20. }; 
  21. class Derived : public Base 
  22. public
  23.     Derived() 
  24.     { 
  25.         printf("I am Derived()\n"); 
  26.     } 
  27.     virtual ~Derived() 
  28.     { 
  29.         printf("I am ~Derived()\n"); 
  30.     } 
  31. public
  32.     void SayHello(); 
  33.     void SayWorld(); 
  34. }; 
  35.  
  36. void Derived::SayHello() 
  37.     printf("Hello Derived\n"); 
  38. void Derived::SayWorld() 
  39.     printf("World Derived\n"); 
  40.  
  41. int main(void
  42.     Base *b1 = new Base; 
  43.     Base *b2 = new Derived; 
  44.     Derived *d = new Derived; 
  45.  
  46.     b1->SayHello(); 
  47.     b1->SayWorld(); 
  48.  
  49.     b2->SayHello(); 
  50.     b2->SayWorld(); 
  51.  
  52.     d->SayHello(); 
  53.     d->SayWorld(); 
  54.  
  55.     delete d; 
  56.     delete b2; 
  57.     delete b1; 
  58.  
  59.     d= NULL; 
  60.     b2 = NULL; 
  61.     b1 = NULL; 
  62.  
  63.     return 0; 
class Base
{
public:
	Base()
	{
		printf("I am Base()\n");
	}
	virtual ~Base()
	{
		printf("I am ~Base()\n");
	}
public:
	virtual void SayHello()
	{
		printf("Hello Base\n");
	}
	void SayWorld()
	{
		printf("World Base\n");
	}
};
class Derived : public Base
{
public:
	Derived()
	{
		printf("I am Derived()\n");
	}
	virtual ~Derived()
	{
		printf("I am ~Derived()\n");
	}
public:
	void SayHello();
	void SayWorld();
};

void Derived::SayHello()
{
	printf("Hello Derived\n");
}
void Derived::SayWorld()
{
	printf("World Derived\n");
}

int main(void)
{
	Base *b1 = new Base;
	Base *b2 = new Derived;
	Derived *d = new Derived;

	b1->SayHello();
	b1->SayWorld();

	b2->SayHello();
	b2->SayWorld();

	d->SayHello();
	d->SayWorld();

	delete d;
	delete b2;
	delete b1;

	d= NULL;
	b2 = NULL;
	b1 = NULL;

	return 0;
}
输出结果:
I am Base()
I am Base()
I am Derived()
I am Base()
I am Derived()
Hello Base
World Base
Hello Derived
World Base
Hello Derived
World Derived
I am ~Derived()
I am ~Base()
I am ~Derived()
I am ~Base()
I am ~Base()


11、阅读以下程序并给出执行结果
  1. class Bclass 
  2. public
  3.     Bclass(int i , int j) 
  4.     { 
  5.         x = i; 
  6.         y = j; 
  7.     } 
  8.     virtual int fun() 
  9.     { 
  10.         return 0; 
  11.     } 
  12. protected
  13.     int x , y; 
  14. }; 
  15.  
  16. class lclass : public Bclass 
  17. public
  18.     lclass(int i , int j , int k) : Bclass(i , j) 
  19.     { 
  20.         z = k; 
  21.     } 
  22.     int fun() 
  23.     { 
  24.         return (x+y+z)/3; 
  25.     } 
  26. private
  27.     int z; 
  28. }; 
  29. int main(void
  30.     lclass obj(2,4,10); 
  31.     Bclass p1 = obj; 
  32.     cout<<p1.fun()<<endl; 
  33.  
  34.     Bclass &p2 = obj; 
  35.     cout<<p2.fun()<<endl; 
  36.     cout<<p2.Bclass::fun()<<endl; 
  37.  
  38.     Bclass *p3 = &obj; 
  39.     cout<<p3->fun()<<endl; 
  40.  
  41.     return 0; 
class Bclass
{
public:
	Bclass(int i , int j)
	{
		x = i;
		y = j;
	}
	virtual int fun()
	{
		return 0;
	}
protected:
	int x , y;
};

class lclass : public Bclass
{
public:
	lclass(int i , int j , int k) : Bclass(i , j)
	{
		z = k;
	}
	int fun()
	{
		return (x+y+z)/3;
	}
private:
	int z;
};
int main(void)
{
	lclass obj(2,4,10);
	Bclass p1 = obj;
	cout<<p1.fun()<<endl;

	Bclass &p2 = obj;
	cout<<p2.fun()<<endl;
	cout<<p2.Bclass::fun()<<endl;

	Bclass *p3 = &obj;
	cout<<p3->fun()<<endl;

	return 0;
}
输出结果:
0
5
0
5
12、如何减少频繁分配内存(malloc或者new)造成的内存碎片?(10分)


13、请写出strchr的实现(10分)
函数功能:找出在字符串str中第一次出现字符ch的位置,找到就返回该字符位置的指针(也就是返回该字符在字符串中的地址的位置),找不到就返回空指针(就是NULL)
const char* strchr(const char* str , char ch)
  1. const char* strchr(const char* str , char ch) 
  2.     char *p = NULL; 
  3.     const char* s = str; 
  4.     for( ; *s != '\0' ; ++s) 
  5.     { 
  6.         if(*s == ch) 
  7.         { 
  8.             p = (char *)s; 
  9.             break
  10.         } 
  11.     } 
  12.     return p; 
const char* strchr(const char* str , char ch)
{
	char *p = NULL;
	const char* s = str;
	for( ; *s != '\0' ; ++s)
	{
		if(*s == ch)
		{
			p = (char *)s;
			break;
		}
	}
	return p;
}
14、请写出冒泡排序法算法(20分)
void BubbleSort(int r[] , int n);
  1. void BubbleSort(int r[] , int n) 
  2.     int i , j , temp; 
  3.     for(i = 0 ; i < n - 1 ; ++i) 
  4.     { 
  5.         for(j = 0 ; j < n-i-1 ; ++j) 
  6.         { 
  7.             if(r[j] > r[j + 1]) 
  8.             { 
  9.                 temp = r[j]; 
  10.                 r[j] = r[j + 1]; 
  11.                 r[j + 1] = temp; 
  12.             } 
  13.         } 
  14.     } 
void BubbleSort(int r[] , int n)
{
	int i , j , temp;
	for(i = 0 ; i < n - 1 ; ++i)
	{
		for(j = 0 ; j < n-i-1 ; ++j)
		{
			if(r[j] > r[j + 1])
			{
				temp = r[j];
				r[j] = r[j + 1];
				r[j + 1] = temp;
			}
		}
	}
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值