一些常见的面试笔试题

电话问题1:构造和析构函数中的虚函数调用;
答案:虚函数可以在构造函数和析构函数中调用,但虚函数此时是静态绑定;而非动态绑定。


电话问题2:C++中的异常可不可以是引用;
答案:异常可以是引用,并且效率高。

电话问题3:TCP状态中的close_wait是什么状态;

答案:close_wait状态是被动关闭方的一个状态,此时是半关闭状态,被关闭方收到了Fin包,并且发送了fin包的ack,等待上层应用结束连接。


电话问题4:排序算法的时间复杂度;
答案:最好是nLogn,其他的上网搜索。

面试问题1.atoi函数编写;
答案:
自己写的atoi函数----(注意:自己定义的atoi函数和库的atoi函数一样的时候,抛出异常时会引起异常退出,个人认为是异常没有不知道被那个函数抛出,所以coredump)
[cpp]  view plain copy
  1. #include <stdio.h>  
  2. #include <stdlib.h>  
  3. #include <string.h>  
  4. #include <unistd.h>  
  5. #include <assert.h>  
  6. #include <iostream>  
  7. #include <string>  
  8. #include <exception>  
  9. using namespace std;  
  10.                                                                                   
  11. const unsigned int SIGN_BIT = 0x1 << 31;  
  12.    
  13. bool isDigit(const char ch)  
  14. {  
  15.         if (ch <= '9' && ch >= '0')  
  16.         {  
  17.                 return true;  
  18.         }  
  19.    
  20.         return false;  
  21. }  
  22.    
  23. int atoi_i(const char *str)  
  24. {  
  25.         assert(str != NULL);  
  26.    
  27.         while (' ' == *str){ str++; }  
  28.    
  29.         int result = 0;  
  30.         bool signFlag = false;  
  31.         if ('+' == *str)  
  32.         {  
  33.                 if (false == isDigit(*++str)) throw "input format error!";  
  34.         }  
  35.         else if ('-' == *str)  
  36.         {  
  37.                 if (false == isDigit(*++str)) throw "input format error!";  
  38.                 signFlag = true;  
  39.         }  
  40.         else if (*str > '9' || *str < '0')  
  41.         {  
  42.                 throw "input format error!";  
  43.         }  
  44.    
  45.         do  
  46.         {  
  47.                 result = result * 10 + *str++ - '0';  
  48.                 if ((result & SIGN_BIT) != 0)  
  49.                 {  
  50.                         throw "overflow error!";  
  51.                 }  
  52.         }  
  53.         while (isDigit(*str));  
  54.    
  55.         if (true == signFlag)  
  56.         {  
  57.                 result = -result;  
  58.         }  
  59.    
  60.         return result;  
  61. }  
  62.    
  63. int main(int argc, char *argv[])  
  64. {  
  65.         char input[1024];  
  66.         while (1)  
  67.         {  
  68.                 try  
  69.                 {  
  70.                         cout << "Input Array:";  
  71.                         cin >> input;  
  72.                         printf("exchange:%d/n", atoi_i(input));  
  73.                 }  
  74.                 catch (const char *p)  
  75.                 {  
  76.                         cout <<"Error Info:" << p << endl;  
  77.                 }  
  78.                 catch ( ... )  
  79.                 {  
  80.                         cout << "test" << endl;  
  81.                 }  
  82.         }  
  83.         return 0;  
  84. }  


本文来自CSDN博客,转载请标明出处: http://blog.csdn.net/zhangxinrun/archive/2010/12/01/6048695.aspx

面试问题2.sizeof和空类;
答案:

class CBase
{
    int a;
    char *p;
};
那么运行cout<<"sizeof(CBase)="<<sizeof(CBase)<<endl;之后输出什么?

这个应该很简单,两个成员变量所占的大小——8。

第一步:空类

class CBase
{
};
运行cout<<"sizeof(CBase)="<<sizeof(CBase)<<endl;

sizeof(CBase)=1;

深度探索c++对象模型中是这样说的:     那是被编译器插进去的一个char ,使得这个class的不同实体(object)在内存中配置独一无二的地址。     也就是说这个char是用来标识类的不同对象的。                                                                                                                                                             

第二步:

还是最初的那个类,运行结果:sizeof(CBase)=8

第三步:添个虚函数

class CBase
{
public:
    CBase(void);
    virtual ~CBase(void);
private:
    int   a;
    char *p;
};
再运行:sizeof(CBase)=12

C++ 类中有虚函数的时候有一个指向虚函数的指针(vptr),在32位系统分配指针大小为4字节”。那么继承类呢?

第四步:

基类就是上面的了不写了

class CChild :
    public CBase
{
public:
    CChild(void);
    ~CChild(void);
private:
    int b;
};
运行:cout<<"sizeof(CChild)="<<sizeof(CChild)<<endl;

输出:sizeof(CChild)=16;

可见子类的大小是本身成员变量的大小加上子类的大小。

 
面试问题3.(1)对象只允许在堆上创建,(2)对象只允许在栈上创建;
答案:

[cpp]  view plain copy
  1. class   HeapOnly   
  2. {   
  3. public:   
  4.  HeapOnly()  
  5.  {     
  6.   cout<<"constructor. "<<endl;     
  7.  }   
  8.  void destroy()  
  9.  {     
  10.   delete this;     
  11.  }   
  12. private:   
  13.  ~HeapOnly(){}     
  14. };  
  15.    
  16. int main()   
  17. {   
  18.  HeapOnly   *p = new HeapOnly;   
  19.  p->destroy();   
  20.  HeapOnly h;   
  21.  h.Output();   
  22.    
  23.  return 0;   
  24. }  
  25.    
  26.   
  27. #include   <iostream>   
  28. using   namespace   std;   
  29.    
  30. class StackOnly   
  31. {   
  32. public:   
  33.  StackOnly()     
  34.  {     
  35.   cout<<"constructor." <<endl;     
  36.  }   
  37.  ~StackOnly()     
  38.  {     
  39.   cout<<"destructor." <<endl;     
  40.  }   
  41. private:   
  42.  void *operator new (size_t);   
  43. };  
  44.    
  45. int main()   
  46. {   
  47.  StackOnly s;                        //okay   
  48.  StackOnly *p = new StackOnly;       //wrong   
  49.    
  50.  return   0;   
  51. }  
  52.    


本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/zhangxinrun/archive/2010/12/03/6052551.aspx

 

面试问题4.在一个不知道升序还是降序的数据组中查找一个给定的数,
  个人想法:1.根据数组的首尾比较,判断数组的序列形式;2.折半查找算法。

答案:

[cpp]  view plain copy
  1. #include <stdio.h>  
  2. #include <assert.h>  
  3. using namespace std;  
  4.                                                                                   
  5. static bool flag = true;  
  6. bool intCompare(int value1, int value2)  
  7. {  
  8.  return (value1 > value2) == flag;  
  9. }  
  10.                                                                                   
  11. int binary_search_i(int a[], int value, int start, int end)  
  12. {  
  13.  if (start > end) return -1;  
  14.                                                                                 
  15.  int pos = (start + end)/ 2;  
  16.                                                                                   
  17.  if (value == a[pos])  
  18.  {  
  19.   return pos;  
  20.  }  
  21.  else if (intCompare(value, a[pos]))  
  22.  {  
  23.   return binary_search_i(a, value, pos + 1, end);  
  24.  }  
  25.  else  
  26.  {  
  27.   return binary_search_i(a, value, start, pos - 1);  
  28.  }  
  29. }  
  30.    
  31. int binary_search(int a[], int value, int n)  
  32. {  
  33.  assert((a != NULL) && (n > 0));  
  34.     
  35.  if ((n == 1) || (a[0] == a[n - 1]))  
  36.  {  
  37.   if (a[0] == value)  
  38.   {   
  39.     return 0;  
  40.   }  
  41.   else  
  42.   {  
  43.    return -1;  
  44.   }  
  45.  }  
  46.     
  47.  if (a[0] < a[n - 1])  
  48.  {  
  49.         flag = true;  
  50.  }  
  51.  else  
  52.  {  
  53.         flag = false;  
  54.  }  
  55.    
  56.  int temp = binary_search_i(a, value, 0, n - 1);  
  57.     
  58.  while ((temp > 0) && (a[temp] == a[temp - 1]))  
  59.  {  
  60.         --temp;  
  61.  }  
  62.     
  63.  return temp;  
  64.    
  65. }  
  66.    
  67.    
  68. int main()  
  69. {  
  70.         //int a[] = {1, 3, 5, 7, 7, 7, 7, 7, 7, 7, 9, 10, 11};  
  71.         int a[] = {11, 10, 9, 7, 7, 7, 7, 7, 5, 3, 1};  
  72.         int arrayNum = sizeof(a) / sizeof(int);  
  73.         for(int i = 0; i < arrayNum; ++i)  
  74.         {  
  75.                 printf("a[%d]=%d/t", i, a[i]);  
  76.         }  
  77.         printf("/n");  
  78.    
  79.         int value = 0;  
  80.         while(1)  
  81.         {  
  82.                 printf("Input search value:");  
  83.                 scanf("%d", &value);  
  84.                 printf("Pos in array:%d/n", binary_search(a, value, arrayNum));  
  85.         }  
  86.    
  87.         return 0;  
  88. }  



面试问题5.那些算法是稳定排序,那些算法是不稳定排序。

答案:上网上搜索一下。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值