アリ雲面接総括
7088 ワード
電話問題1:構造関数と解析関数の虚関数呼び出し;
答え:虚関数は構造関数と解析関数で呼び出すことができますが、虚関数は静的バインドです.動的バインドではありません.
電話問題2:C++の異常は引用であってもよいか.
答え:異常は参照であり、効率が高い.
電話問題3:TCP状態のclose_waitはどんな状態ですか.
答え:close_wait状態はパッシブクローズ側の1つの状態であり,このときは半クローズ状態であり,クローズ側にFinパケットが受信され,finパケットのackが送信され,上位アプリケーションが接続を終了するのを待つ.
電話問題4:ソートアルゴリズムの時間的複雑さ;
答え:nLognが一番いいです.他のインターネット検索がいいです.
面接問題1.atoi関数の作成;
答え:
自分で書いたatoi関数---(注意:自分で定義したatoi関数がライブラリのatoi関数と同じである場合、異常を投げ出すと異常脱退を起こすので、個人的には異常だと思ってその関数に投げ出されることを知らないのでcoredump)
本文はCSDNブログから来て、転載して出典を明記してください:
http://blog.csdn.net/zhangxinrun/archive/2010/12/01/6048695.aspx
面接問題2.sizeofと空のクラス;
答え:
class CBase { int a; char *p; }; するとcout<これは簡単なはずです.2つのメンバー変数が占めるサイズは8です.
ステップ1:空のクラス
class CBase { }; 実行cout<sizeof(CBase)=1;
深さ探索c++オブジェクトモデルでは、コンパイラに挿入されたcharであり、このclassの異なるエンティティ(object)がメモリにユニークなアドレスを構成する.すなわち,このcharはクラスの異なるオブジェクトを識別するために用いられる.
ステップ2:
やはり最初のクラスで、実行結果:sizeof(CBase)=8
ステップ3:虚関数を追加
class CBase { public: CBase(void); virtual ~CBase(void); private: int a; char *p; }; 再実行:sizeof(CBase)=12
C++クラスに虚関数がある場合は虚関数を指すポインタ(vptr)があり、32ビットシステムではポインタサイズが4バイトに割り当てられます.」では、継承クラスは?
ステップ4:
ベースクラスは上に書いてありません
class CChild : public CBase { public: CChild(void); ~CChild(void); private: int b; }; 運転:cout<出力:sizeof(CChild)=16;
可視サブクラスのサイズは、自身のメンバー変数のサイズとサブクラスのサイズです.
面接問題3.(1)オブジェクトはスタック上の作成のみを許可し,(2)オブジェクトはスタック上の作成のみを許可する.答え:
本文はCSDNブログから来て、転載して出典を明記してください:http://blog.csdn.net/zhangxinrun/archive/2010/12/03/6052551.aspx
面接問題4.昇順か降順か分からないデータ・グループで、与えられた数を検索します.個人的な考え:1.配列の首尾比較に基づいて、配列のシーケンス形式を判断する.2.半角検索アルゴリズム.
答え:
面接問題5.それらのアルゴリズムは安定ソートであり,それらのアルゴリズムは不安定ソートである.
インターネットで検索してみます.
答え:虚関数は構造関数と解析関数で呼び出すことができますが、虚関数は静的バインドです.動的バインドではありません.
電話問題2:C++の異常は引用であってもよいか.
答え:異常は参照であり、効率が高い.
電話問題3:TCP状態のclose_waitはどんな状態ですか.
答え:close_wait状態はパッシブクローズ側の1つの状態であり,このときは半クローズ状態であり,クローズ側に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<
ステップ1:空のクラス
class CBase { }; 実行cout<
深さ探索c++オブジェクトモデルでは、コンパイラに挿入されたcharであり、このclassの異なるエンティティ(object)がメモリにユニークなアドレスを構成する.すなわち,このcharはクラスの異なるオブジェクトを識別するために用いられる.
ステップ2:
やはり最初のクラスで、実行結果:sizeof(CBase)=8
ステップ3:虚関数を追加
class CBase { public: CBase(void); virtual ~CBase(void); private: int a; char *p; }; 再実行:sizeof(CBase)=12
C++クラスに虚関数がある場合は虚関数を指すポインタ(vptr)があり、32ビットシステムではポインタサイズが4バイトに割り当てられます.」では、継承クラスは?
ステップ4:
ベースクラスは上に書いてありません
class CChild : public CBase { public: CChild(void); ~CChild(void); private: int b; }; 運転:cout<
可視サブクラスのサイズは、自身のメンバー変数のサイズとサブクラスのサイズです.
面接問題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.それらのアルゴリズムは安定ソートであり,それらのアルゴリズムは不安定ソートである.
インターネットで検索してみます.