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

转自:http://blog.csdn.net/hackbuteer1/article/details/8476206,Hackbuteer1的专栏

1、10、10、4、4四个数,怎么算出24点?
(10*10-4)/4=24
2、下列表达式在32位机器编译环境下的值()

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位机器下运行的结果是()

#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的值是()

#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分)

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. {  
  3.     if(!b)  
  4.     {  
  5.         return ;  
  6.     }  
  7.     else if(!p)  
  8.     {  
  9.         return ;  
  10.     }  
  11.     else if(!f)  
  12.     {  
  13.         return ;  
  14.     }  
  15. }  
修改如下:
  1. void test_type(bool b , const char *p , float f)  
  2. {  
  3.     if(!b)  
  4.     {  
  5.         return ;  
  6.     }  
  7.     else if(!p)  
  8.     {  
  9.         return ;  
  10.     }  
  11.     else if(f > -1e-10 && f < 1e-10)  
  12.     {  
  13.         return ;  
  14.     }  
  15. }  
7、请指出以下程序有什么问题(5分)
  1. void test_mem()  
  2. {  
  3.     char *p = new char[64];  
  4.     delete p;  
  5.     p = NULL;  
  6.     return ;  
  7. }  
应该修改为 delete[]p;  p指向的是一个字符型的数组空间,原来的代码只是简单的释放了指向申请空间的指针,并没有释放申请的空间,容易造成内存崩溃。
回收用 new 分配的单个对象的内存空间的时候用 delete,回收用 new[] 分配的一组对象的内存空间的时候用 delete[]。
8、以下程序有什么问题,请指出。
  1. char* GetMem()  
  2. {  
  3.     char p[] = "hello";  
  4.     return p;  
  5. }  
  6.   
  7. void test_get_mem()  
  8. {  
  9.     char *p = GetMem();  
  10.     printf(p);  
  11.     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. {  
  3. public:  
  4.     Base()  
  5.     {  
  6.         printf("I am Base()\n");  
  7.     }  
  8.     virtual ~Base()  
  9.     {  
  10.         printf("I am ~Base()\n");  
  11.     }  
  12. public:  
  13.     virtual void SayHello()  
  14.     {  
  15.         printf("Hello Base\n");  
  16.     }  
  17.     void SayWorld()  
  18.     {  
  19.         printf("World Base\n");  
  20.     }  
  21. };  
  22. class Derived : public Base  
  23. {  
  24. public:  
  25.     Derived()  
  26.     {  
  27.         printf("I am Derived()\n");  
  28.     }  
  29.     virtual ~Derived()  
  30.     {  
  31.         printf("I am ~Derived()\n");  
  32.     }  
  33. public:  
  34.     void SayHello();  
  35.     void SayWorld();  
  36. };  
  37.   
  38. void Derived::SayHello()  
  39. {  
  40.     printf("Hello Derived\n");  
  41. }  
  42. void Derived::SayWorld()  
  43. {  
  44.     printf("World Derived\n");  
  45. }  
  46.   
  47. int main(void)  
  48. {  
  49.     Base *b1 = new Base;  
  50.     Base *b2 = new Derived;  
  51.     Derived *d = new Derived;  
  52.   
  53.     b1->SayHello();  
  54.     b1->SayWorld();  
  55.   
  56.     b2->SayHello();  
  57.     b2->SayWorld();  
  58.   
  59.     d->SayHello();  
  60.     d->SayWorld();  
  61.   
  62.     delete d;  
  63.     delete b2;  
  64.     delete b1;  
  65.   
  66.     d= NULL;  
  67.     b2 = NULL;  
  68.     b1 = NULL;  
  69.   
  70.     return 0;  
  71. }  
输出结果:
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. {  
  3. public:  
  4.     Bclass(int i , int j)  
  5.     {  
  6.         x = i;  
  7.         y = j;  
  8.     }  
  9.     virtual int fun()  
  10.     {  
  11.         return 0;  
  12.     }  
  13. protected:  
  14.     int x , y;  
  15. };  
  16.   
  17. class lclass : public Bclass  
  18. {  
  19. public:  
  20.     lclass(int i , int j , int k) : Bclass(i , j)  
  21.     {  
  22.         z = k;  
  23.     }  
  24.     int fun()  
  25.     {  
  26.         return (x+y+z)/3;  
  27.     }  
  28. private:  
  29.     int z;  
  30. };  
  31. int main(void)  
  32. {  
  33.     lclass obj(2,4,10);  
  34.     Bclass p1 = obj;  
  35.     cout<<p1.fun()<<endl;  
  36.   
  37.     Bclass &p2 = obj;  
  38.     cout<<p2.fun()<<endl;  
  39.     cout<<p2.Bclass::fun()<<endl;  
  40.   
  41.     Bclass *p3 = &obj;  
  42.     cout<<p3->fun()<<endl;  
  43.   
  44.     return 0;  
  45. }  
输出结果:
0
5
0
5
12、如何减少频繁分配内存(malloc或者new)造成的内存碎片?(10分)
内存池(Memory Pool)是一种内存分配方式。 通常我们习惯直接使用new、malloc等API申请分配内存,这样做的缺点在于:由于所申请内存块的大小不定,当频繁使用时会造成大量的内存碎片并进而降低性能。内存池则是在真正使用内存之前,先申请分配一定数量的、大小相等(一般情况下)的内存块留作备用。当有新的内存需求时,就从内存池中分出一部分内存块,若内存块不够再继续申请新的内存。这样做的一个显著优点是尽量避免了内存碎片,使得内存分配效率得到提升。

(1)针对特殊情况,例如需要频繁分配释放固定大小的内存对象时,不需要复杂的分配算法和多线程保护。也不需要维护内存空闲表的额外开销,从而获得较高的性能。
(2)由于开辟一定数量的连续内存空间作为内存池块,因而一定程度上提高了程序局部性,提升了程序性能。
(3)比较容易控制页边界对齐和内存字节对齐,没有内存碎片的问题。
(4)当需要分配管理的内存在100M一下的时候,采用内存池会节省大量的时间,否则会耗费更多的时间。
(5)内存池可以防止更多的内存碎片的产生
(6)更方便于管理内存

13、请写出strchr的实现(10分)
函数功能:找出在字符串str中第一次出现字符ch的位置,找到就返回该字符位置的指针(也就是返回该字符在字符串中的地址的位置),找不到就返回空指针(就是NULL)
const char* strchr(const char* str , char ch)

  1. const char* strchr(const char* str , char ch)  
  2. {  
  3.     char *p = NULL;  
  4.     const char* s = str;  
  5.     for( ; *s != '\0' ; ++s)  
  6.     {  
  7.         if(*s == ch)  
  8.         {  
  9.             p = (char *)s;  
  10.             break;  
  11.         }  
  12.     }  
  13.     return p;  
  14. }  
14、请写出冒泡排序法算法(20分)
void BubbleSort(int r[] , int n);

  1. void BubbleSort(int r[] , int n)  
  2. {  
  3.     int i , j , temp;  
  4.     for(i = 0 ; i < n - 1 ; ++i)  
  5.     {  
  6.         for(j = 0 ; j < n-i-1 ; ++j)  
  7.         {  
  8.             if(r[j] > r[j + 1])  
  9.             {  
  10.                 temp = r[j];  
  11.                 r[j] = r[j + 1];  
  12.                 r[j + 1] = temp;  
  13.             }  
  14.         }  
  15.     }  

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值