您的位置:首页 > 职场人生

阿里云面试总结

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

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

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

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

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

面试问题1.atoi函数编写;
答案:
自己写的atoi函数----(注意:自己定义的atoi函数和库的atoi函数一样的时候,抛出异常时会引起异常退出,个人认为是异常没有不知道被那个函数抛出,所以coredump)
#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <assert.h>

#include <iostream>

#include <string>

#include <exception>

using namespace std;

                                                                               

const unsigned int SIGN_BIT = 0x1 << 31;

 

bool isDigit(const char ch)

{

        if (ch <= '9' && ch >= '0')

        {

                return true;

        }

 

        return false;

}

 

int atoi_i(const char *str)

{

        assert(str != NULL);

 

        while (' ' == *str){ str++; }

 

        int result = 0;

        bool signFlag = false;

        if ('+' == *str)

        {

                if (false == isDigit(*++str)) throw "input format error!";

        }

        else if ('-' == *str)

        {

                if (false == isDigit(*++str)) throw "input format error!";

                signFlag = true;

        }

        else if (*str > '9' || *str < '0')

        {

                throw "input format error!";

        }

 

        do

        {

                result = result * 10 + *str++ - '0';

                if ((result & SIGN_BIT) != 0)

                {

                        throw "overflow error!";

                }

        }

        while (isDigit(*str));

 

        if (true == signFlag)

        {

                result = -result;

        }

 

        return result;

}

 

int main(int argc, char *argv[])

{

        char input[1024];

        while (1)

        {

                try

                {

                        cout << "Input Array:";

                        cin >> input;

                        printf("exchange:%d/n", atoi_i(input));

                }

                catch (const char *p)

                {

                        cout <<"Error Info:" << p << endl;

                }

                catch ( ... )

                {

                        cout << "test" << endl;

                }

        }

        return 0;

}

本文来自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)对象只允许在栈上创建;

答案:

class   HeapOnly

{

public:

 HeapOnly()

 {  

  cout<<"constructor. "<<endl;  

 }

 void destroy()

 {  

  delete this;  

 }

private:

 ~HeapOnly(){}  

};

 

int main()

{

 HeapOnly   *p = new HeapOnly;

 p->destroy();

 HeapOnly h;

 h.Output();

 

 return 0;

}

 

#include   <iostream>

using   namespace   std;

 

class StackOnly

{

public:

 StackOnly()  

 {  

  cout<<"constructor." <<endl;  

 }

 ~StackOnly()  

 {  

  cout<<"destructor." <<endl;  

 }

private:

 void *operator new (size_t);

};

 

int main()

{

 StackOnly s;                        //okay

 StackOnly *p = new StackOnly;       //wrong

 

 return   0;

}

 

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

 

面试问题4.在一个不知道升序还是降序的数据组中查找一个给定的数,

  个人想法:1.根据数组的首尾比较,判断数组的序列形式;2.折半查找算法。

答案:

#include <stdio.h>

#include <assert.h>

using namespace std;

                                                                               

static bool flag = true;

bool intCompare(int value1, int value2)

{

 return (value1 > value2) == flag;

}

                                                                               

int binary_search_i(int a[], int value, int start, int end)

{

 if (start > end) return -1;

                                                                               

 int pos = (start + end)/ 2;

                                                                               

 if (value == a[pos])

 {

  return pos;

 }

 else if (intCompare(value, a[pos]))

 {

  return binary_search_i(a, value, pos + 1, end);

 }

 else

 {

  return binary_search_i(a, value, start, pos - 1);

 }

}

 

int binary_search(int a[], int value, int n)

{

 assert((a != NULL) && (n > 0));

 

 if ((n == 1) || (a[0] == a[n - 1]))

 {

  if (a[0] == value)

  { 

    return 0;

  }

  else

  {

   return -1;

  }

 }

 

 if (a[0] < a[n - 1])

 {

        flag = true;

 }

 else

 {

        flag = false;

 }

 

 int temp = binary_search_i(a, value, 0, n - 1);

 

 while ((temp > 0) && (a[temp] == a[temp - 1]))

 {

        --temp;

 }

 

 return temp;

 

 

}

 

 

int main()

{

        //int a[] = {1, 3, 5, 7, 7, 7, 7, 7, 7, 7, 9, 10, 11};

        int a[] = {11, 10, 9, 7, 7, 7, 7, 7, 5, 3, 1};

        int arrayNum = sizeof(a) / sizeof(int);

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

        {

                printf("a[%d]=%d/t", i, a[i]);

        }

        printf("/n");

 

        int value = 0;

        while(1)

        {

                printf("Input search value:");

                scanf("%d", &value);

                printf("Pos in array:%d/n", binary_search(a, value, arrayNum));

        }

 

        return 0;

}

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

答案:http://blog.csdn.net/petib_wangwei/article/details/38638391
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息