阿里雲面試總結

來源:互聯網
上載者:User
電話問題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.那些演算法是穩定排序,那些演算法是不穩定排序。

答案:上網上搜尋一下。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.