C/C++試題(from 周星星’s Blog)

來源:互聯網
上載者:User
呵呵,不知道對一些筆/面試是否有協助 :)
1. 以下三條輸出語句分別輸出什嗎?[C易]
char str1[]       = "abc";
char str2[]       = "abc";
const char str3[] = "abc";
const char str4[] = "abc";
const char* str5  = "abc";
const char* str6  = "abc";
cout << boolalpha << ( str1==str2 ) << endl; // 輸出什嗎?
cout << boolalpha << ( str3==str4 ) << endl; // 輸出什嗎?
cout << boolalpha << ( str5==str6 ) << endl; // 輸出什嗎?

2. 非C++內建型別 A 和 B,在哪幾種情況下B能隱式轉化為A?[C++中等]
答:
a. class B : public A { ……} // B公有繼承自A,可以是間接繼承的
b. class B { operator A( ); } // B實現了隱式轉化為A的轉化
c. class A { A( const B& ); } // A實現了non-explicit的參數為B(可以有其他帶預設值的參數)建構函式
d. A& operator= ( const A& ); // 賦值操作,雖不是正宗的隱式類型轉換,但也可以勉強算一個

3. 以下代碼中的兩個sizeof用法有問題嗎?[C易]
void UpperCase( char str[] ) // 將 str 中的小寫字母轉換成大寫字母
{
    for( size_t i=0; i&ltsizeof(str)/sizeof(str[0]); ++i )
        if( 'a'<=str[i] && str[i]<='z' )
            str[i] -= ('a'-'A' );
}
char str[] = "aBcDe";
cout << "str字元長度為: " << sizeof(str)/sizeof(str[0]) << endl;
UpperCase( str );
cout << str << endl;

4. 以下代碼有什麼問題?[C難]
void char2Hex( char c ) // 將字元以16進位表示
{
    char ch = c/0x10 + '0'; if( ch > '9' ) ch += ('A'-'9'-1);
    char cl = c%0x10 + '0'; if( cl > '9' ) cl += ('A'-'9'-1);
    cout << ch << cl << ' ';
}
char str[] = "I love 中國";
for( size_t i=0; i&ltstrlen(str); ++i )
    char2Hex( str[i] );
cout << endl;

5. 以下代碼有什麼問題?[C++易]
struct Test
{
    Test( int ) {}
    Test() {}
    void fun() {}
};
void main( void )
{
    Test a(1);
    a.fun();
    Test b();
    b.fun();
}

6. 以下代碼有什麼問題?[C++易]
cout << (true?1:"1") << endl;

7. 以下代碼能夠編譯通過嗎,為什嗎?[C++易]
unsigned int const size1 = 2;
char str1[ size1 ];
unsigned int temp = 0;
cin >> temp;
unsigned int const size2 = temp;
char str2[ size2 ];

8. 以下代碼中的輸出語句輸出0嗎,為什嗎?[C++易]
struct CLS
{
    int m_i;
    CLS( int i ) : m_i(i) {}
    CLS()
    {
        CLS(0);
    }
};
CLS obj;
cout << obj.m_i << endl;

9. C++中的空類,預設產生哪些類成員函數?[C++易]
答:
class Empty
{
public:
    Empty();                          // 預設建構函式
    Empty( const Empty& );            // 拷貝建構函式
    ~Empty();                         // 解構函式
    Empty& operator=( const Empty& ); // 賦值運算子
    Empty* operator&();               // 取址運算子
    const Empty* operator&() const;   // 取址運算子 const
};

10. 以下兩條輸出語句分別輸出什嗎?[C++難]
float a = 1.0f;
cout << (int)a << endl;
cout << (int&)a << endl;
cout << boolalpha << ( (int)a == (int&)a ) << endl; // 輸出什嗎?
float b = 0.0f;
cout << (int)b << endl;
cout << (int&)b << endl;
cout << boolalpha << ( (int)b == (int&)b ) << endl; // 輸出什嗎?

11. 以下反向遍曆array數組的方法有什麼錯誤?[STL易]
vector array;
array.push_back( 1 );
array.push_back( 2 );
array.push_back( 3 );
for( vector::size_type i=array.size()-1; i>=0; --i ) // 反向遍曆array數組
{
    cout << array[i] << endl;
}

12. 以下代碼有什麼問題?[STL易]
typedef vector IntArray;
IntArray array;
array.push_back( 1 );
array.push_back( 2 );
array.push_back( 2 );
array.push_back( 3 );
// 刪除array數組中所有的2
for( IntArray::iterator itor=array.begin(); itor!=array.end(); ++itor )
{
    if( 2 == *itor ) array.erase( itor );
}

13. 寫一個函數,完成記憶體之間的拷貝。[考慮問題是否全面]
答:
void* mymemcpy( void *dest, const void *src, size_t count )
{
    char* pdest = static_cast&ltchar*>( dest );
    const char* psrc = static_cast&ltconst char*>( src );
    if( pdest>psrc && pdest&ltpsrc+cout ) 能考慮到這種情況就行了
    {
        for( size_t i=count-1; i!=-1; --i )
                pdest[i] = psrc[i];
    }
    else
    {
        for( size_t i=0; i&ltcount; ++i )
            pdest[i] = psrc[i];
    }
    return dest;
}
int main( void )
{
    char str[] = "0123456789";
    mymemcpy( str+1, str+0, 9 );
    cout << str << endl;

    system( "Pause" );
    return 0;
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.