C++面试题

来源:互联网 发布:htc锁屏软件 编辑:程序博客网 时间:2024/05/02 03:06

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);
}

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

本题主要考察的是隐式转换问题。Show函数的参数输入是一个Number,而在调用时传入的是short。请注意由整数转为Number的两个构造函数,Number(short)要求显式调用,而Number(int)则不需要。因此这里的转换为: short->int->Number,答案为C。如果把Show()的调用改为Show(Number(s)),那么这里就该选B了。
                             
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;
cout << (int)q - (int)p << endl;

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

q–p是指针的操作,表示它们相差多少个指向的数据对象。这里显然是1。(int)q-(int)p 是p和q指向内容的首地址的操作,这里刚好相差一个double,即8。答案为A。

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;
    cout << c << endl;

    return 0;
}

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

这里主要考察的是操作符优先级的问题。c = a+++++b;中,++操作符的优先级高于+,因此该句相当于c = ((a++)++)+b。显然,a++之后返回的是一个临时变量,而临时变量不能作为左值来进行++操作,因此这里会出现编译错误。答案为B。

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;

    return 0;
}

a) 8
b) 9
c) 10
d) 11

这里主要考察宏的副作用。事实上,把参数替换之后为:(((a--) >= (++b)) ? (a--) : (++b))。先看?的左边,a--先返回10,然后再自减,因此左边为真。所以最终返回的是a--。但a前面已经执行过一个自减操作,此时值为9。因此最终的返回值是9。

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.

一个定义为volatile的变量是说这变量可能会被意想不到地改变,这样,编译器就不会去假设这个变量的值了。精确地说就是,优化器在用到这个变量时必须每次都小心地重新读取这个变量的值,而不是使用保存在寄存器里的备份。下面是volatile变量的几个例子:
1) 并行设备的硬件寄存器(如:状态寄存器)
2) 一个中断服务子程序中会访问到的非自动变量(Non-automatic variables)
3) 多线程应用中被几个任务共享的变量
因此这里的答案为A。

7. What is the output of the following program?

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

    *pNumber = 10 ;

    cout << ++*pNumber++ << endl;
    cout << *pNumber << endl;

    return 0 ;
}

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

这里既考察了优先级,也考察了指针的应用。这里首先是*与pNumber结合,进行++*pNumber操作,因此这里第一行打印的是11。注意,操作之后指针pNumber还要进行一个自增操作,操作之后pNumber指向的位置就不再是合法的了,因此*pNumber将会是一个随机值。因此比较接近的答案是D。

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

本题考察的是数据封装方面的知识。由于前面有了这个宏定义,后面所有的public都变为private了。因此MakeNoise是私有成员函数,在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

注意模版实例只有在需要的时候才创建。这里T的类型就只有int、char和double三个,因此最终生成的类型也只有三个类型。答案为A。

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

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。

原创粉丝点击