一份不错的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.

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值