嵌入式系統是軟件和硬件的綜合體,還可以涵蓋機械等附屬裝置。那麼嵌入式考試筆試的試題你做了嗎?下面跟yjbys小編一起來看看吧!
1. 以下三條輸出語句分別輸出什麼?
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; // 輸出什麼?
答:分別輸出false,false,true。str1和str2都是字符數組,每個都有其自己的存儲區,它們的值則是各存儲區首地址,不等;str3和str4同上,只是按const語義,它們所指向的數據區不能修改。str5和str6並非數組而是字符指針,並不分配存儲區,其後的“abc”以常量形式存於靜態數據區,而它們自己僅是指向該區首地址的指針,相等。
3. 非C++內建型別 A 和 B,在哪幾種情況下B能隱式轉化爲A?
答:
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& ); // 賦值操作,雖不是正宗的隱式類型轉換,但也可以勉強算一個
4. 以下代碼有什麼問題?
struct Test
{
Test( int ) {}
Test() {}
void fun() {}
};
void main( void )
{
Test a(1);
();
Test b();
();
}
答:變量b定義出錯。按默認構造函數定義對象,不需要加括號。
5. 以下代碼有什麼問題?
cout << (true?1:"1") << endl;
答:三元表達式“?:”問號後面的兩個操作數必須爲同一類型。
6. 以下代碼能夠編譯通過嗎,爲什麼?
unsigned int const size1 = 2;
char str1[ size1 ];
unsigned int temp = 0;
cin >> temp;
unsigned int const size2 = temp;
char str2[ size2 ];
答:str2定義出錯,size2非編譯器期間常量,而數組定義要求長度必須爲編譯期常量。
During my test in linux environment. The above code could be compiled successfully. But if we initialize the defined arrary like this “char str2[ size2 ] = {0};”, there would be a compile error informing that “variable-sized object `str2' may not be initialized”.
7. 以下反向遍歷array數組的方法有什麼錯誤?
vector array;
_back( 1 );
_back( 2 );
_back( 3 );
for( vector::size_type i=()-1; i>=0; --i ) // 反向遍歷array數組
{
cout << array[i] << endl;
}
答:首先數組定義有誤,應加上類型參數:vector array。其次vector::size_type被定義爲unsigned int,即無符號數,這樣做爲循環變量的i爲0時再減1就會變成最大的整數,導致循環失去控制。
8. 以下代碼中的輸出語句輸出0嗎,爲什麼?
struct CLS
{
int m_i;
CLS( int i ) : m_i(i) {}
CLS()
{
CLS(0);
}
};
CLS obj;
cout << obj.m_i << endl;
答:不能。在默認構造函數內部再調用帶參的構造函數屬用戶行爲而非編譯器行爲,亦即僅執行函數調用,而不會執行其後的初始化表達式。只有在生成對象時,初始化表達式纔會隨相應的構造函數一起調用。
9. C++中的空類,默認產生哪些類成員函數?
答:
class Empty
{
public:
Empty(); // 缺省構造函數
Empty( const Empty& ); // 拷貝構造函數
~Empty(); // 析構函數
Empty& operator=( const Empty& ); // 賦值運算符
Empty* operator&(); // 取址運算符
const Empty* operator&() const; // 取址運算符 const
};
10. 以下兩條輸出語句分別輸出什麼?
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; // 輸出什麼?
答:分別輸出false和true。注意轉換的應用。(int)a實際上是以浮點數a爲參數構造了一個整型數,該整數的值是1,(int&)a則是告訴編譯器將a當作整數看(並沒有做任何實質上的轉換)。因爲1以整數形式存放和以浮點形式存放其內存數據是不一樣的,因此兩者不等。對b的兩種轉換意義同上,但是0的整數形式和浮點形式其內存數據是一樣的,因此在這種特殊情形下,兩者相等(僅僅在數值意義上)。