1. 程式人生 > >一份不錯的C++筆試題

一份不錯的C++筆試題

1. 
What is displayed when f() is called given the code:

class Number {
public:
  string type;
  Number(): type(“void”) { }
  explicit Number(short) : type(“short”) { } //這裡顯式構造,防止隱式轉換.
  Number(int) : type(“int”) { }
};

void Show(const Number& n) { cout << n.type; }

void f()
{
 short s = 42;
 Show(s); //這裡首先需要對s的資料型別自動轉換到int型,所以使用Number(int)這個建構函式
                              //結果答案為C.
}

a) void
b) short
c) int
d) None of the above


2. Which is the correct output for the following code

double  dArray[2] = {4, 8}, *p, *q;
p =  &dArray[0];
q = p + 1;
cout << q – p << endl; //這裡輸出1
cout << (int)q - (int)p << endl; //這裡輸出8
//所以選擇A.原因很簡單,不再細敘.

a) 1 and 8
b) 8 and 4
c) 4 and 8
d) 8 and 1

3. Which set of statements is not legal C++?
a) int x = 4; const int& y = x;
b) float f = 2.4; short s = f;
c) char c = 'A'; const char * const p = &c;
d) struct s { int m, n; } q = { 0 };
e) All are valid.
解析:
很明顯,答案為E
4. What is the output of the below program?

int main(void)
{
int a = 10, b = 20, c;

     c = a+++++b; //這裡產生編譯時錯誤,原因在於non-lvalue cann't increment.可以修改正為
//c = a+++(++b)這樣即可,輸出答案為31.所以本題修正前的答案為B
     cout << c << endl;

     return 0;
}

a) 31
b) Compilation error
c) 30
d) 32

5. What is the output of the following program?

#define MAX(A,B) (((A) >= (B)) ? (A) : (B))

int main(void)
{
    int a = 10, b = 9;

    cout << MAX(a--, ++b) << endl;  //這裡輸出9,原因是cout<<(a--)<<endl;
    cout<<a<<endl; //這裡輸出為8

    return 0;
}
解析:
這道題目也比較簡單,巨集在做這樣的運算都會發生兩次自增或自減.
正如前面所說的,後置--發生cout之後,所以輸出9.答案為B
a) 8
b) 9
c) 10
d) 11

6. The keyword volatile in the following code…

Volatile unsigned x = getValue();

a) …ensures thread safety.
b) …is obsolete and irrelevant.
c) …sets x to the return value of GetValue().
d) …none of the above.

解析:
The volatile keyword is an implementation-dependent modifier, used when declaring variables, which
prevents the compiler from optimizing those variables. Volatile should be used with variables whose value can change in unexpected ways (i.e. through an interrupt), which could conflict with optimizations that the compiler might perform.
這道題目我有些疑問,偶的答案是A.


 7. What is the output of the following program?

int main(void)
{
    int * pNumber ;
    pNumber = new int;

    *pNumber = 10 ;

    cout << ++*pNumber++ << endl; //這裡輸出11
    cout << *pNumber << endl; //這裡指標指向不明確了,估計值也是隨機的.

    return 0 ;
}

a) 10 and 11
b) 11 and 12
c) 10 and 0
d) 11 and 0

解析:
我在編譯器上試了一下,答案為D.
不過偶認為cout << *pNumber << endl;將會輸出隨機值.原因在於前面的輸出語句後,pNumber++,指向不明
確了.

8. Find out the error in the code below, if any

#define public private         // (1)

class Animal {
public:                   //  (2)
      void MakeNoise();
};

int main(void)
{
    Animal animal;
    animal.MakeNoise();         // (3)
    return 0;
}

a) Error at Line 1
b) Error at Line 2
c) Error at Line 3
d) Error at all locations 1, 2, and 3

解析:
這道題目就比較簡單了,答案是C.物件不能直接訪問私有成員,而是要通過介面即公共成員函式來訪問.


9.  template<class T, int size = 10>
class Array {


};
 
void foo( )
 {
  Array <int> arr1;
  Array <char> arr4, arr5;
  Array <int> arr2, arr3;
  Array <double> arr6;
  … …
  … …
 }

How many instances of the template class Array will get instantiated inside the function foo()

a) 3
b) 6
c) 4
d) 1

解析:
汗,偶對模板瞭解不多,猜一個.
答案為A,想法是Array<int>arr2,arr3將使用Array<int>arr1的instance.所以不再重複產生,即總共3個.


10. 
class Test
{
        public:
                Test() {}
                Test(char *Name, int len = 0) { }  //f1
                Test(char *Name) { } //f2
};

int main()
{
        Test obj("Hello");
        return 0;
}

Which of the following statement is CORRECT for the above stated code sample
 
a) Will generate run time error
b) Will generate compile time error.
c) Will execute successfully
d) None of the above.

解析:
這道題目也比較簡單,答案為B.
用f1,f2分別代替以上兩個函式,編譯時會產生模糊語意.
C++有一種思想:它認為潛在的二義性不是一種錯誤.
編譯器無法覺得呼叫那個函式,當然只有generate compile time error.