网上收集的C++笔试题目(8)

来源:互联网 发布:淘宝打不开手机相册了 编辑:程序博客网 时间:2024/05/21 22:37

说明:下划线是我认为的答案,仅供参考。

【Q1】 In the given C++ code snippet, which of the following statements correctly identify how Mon of enum DOW can be assigned to a variable named var?

void main()

{

const int Mon = 1, Tue = 2;

enum DOW{ Mon = 11, Tue = 12 };

...

 

A.    The variable var will have to be of type enum DOW, and then defined as:

var = Mon;

B.    Mon of enum DOW cannot be assigned because the compiler will give a redefinition error.

C.    Since enum DOW has been declared and defined later, it will be pushed onto the stack later, but will be accessed first. Hence, the scope resolution operator is not required and var will be assigned as:

var = Mon;

D.    Since Mon is of type enum DOW, var will be assigned as:

var = DOW::Mon;

E.    The variable var must be of type const int, and then it can be assigned as:

var = static_cast<const int>(Mon);

 

【Q2】 For the code snippet below, which of the following statements provide the correct order of constructor calls when object obj is created in main()?

class Base

{

public:

Base(){cout << "In Base Ctor/n";

 

class Nest

{

public:

Nest(){cout << "In Nest Ctor/n"; }

};

};

 

class Derive : public Base

{

public:

Derive(){cout << "In Derive Ctor/n"; }

};

 

void main()

{

    Derive obj;

}

 

A.    Base constructor

Derive constructor

B.    Base constructor

Derive constructor

Nest constructor

C.    Base constructor

Nest constructor

Derive constructor

D.    Nest constructor

Base constructor

Derive constructor

E.    Derive constructor

Base constructor

Nest constructor

 

【Q3】 The C++ code below generates a compiler error. Which of the following solutions can be used to correctly access the member named nested?

class SomeClass

{

public:

int data;

protected:

class Nest

{

public:

int nested;

};

public:

static Nest* createNest(){return new Nest;}

};

 

void use_someclass()

{

SomeClass::Nest* nst = SomeClass::createNest();

nst->nested = 5;

}

A.    Make function void use_someclass() a friend of class SomeClass.

B.    Make the function createNest() a non-static function of SomeClass.

C.    Declare the class Nest in public scope of class SomeClass.

D.    Make the object nst a reference object, and make the function createNest() return a Nest&.

E.    Derive a class from SomeClass. Make the object nst a derived class pointer so that it can access SomeClass's protected declarations.

 

【Q4】 In C++, which of the following statements regarding the code below are valid?

#include <iostream>

 

class Outer

{

int m_o;

public:

class Inner

{

int m_i;

public:

Inner(){}

Inner(Outer m_outer, int x)

{

m_outer.m_o = x;

}

};

 

Outer(int y)

{

m_inner.m_i = y;

}

 

void Display()

{

using namespace std;

cout << m_o << endl

<< m_inner.m_i << endl;

}

Inner m_inner;

};

 

void main()

{

Outer    objOut(10);

Outer::Inner objIn(objOut, 5);

objOut.Display();

}

 

A.    The output will be:

5

10

B.    Outer class cannot access its nested class's private members.

C.    The relationship between class Inner and class Outer is not valid C++ syntax.

D.    Inner class cannot access its containing class's private members.

E.    The output will be:

10

5

 

【Q5】 What is the output of the program below in C++?

#include <iostream>

using namespace std;

 

class Object

{

public:

Object() {}

 

void Print() const

{

cout << "const" << endl;

}

 

void Print()

{

cout << "mutable" << endl;

}

};

 

void print_obj(const Object& obj)

{

obj.Print();

}

 

int main()

{

Object       obj1;

const Object obj2;

Object*const pobj1 = &obj1;

 

print_obj(obj1);

print_obj(obj2);

 

obj1.Print();

obj2.Print();

 

pobj1->Print();

 

return 0;

}

 

A.    const

const

mutable

const

const

B.    const

const

mutable

const

mutable

C.    mutable

const

mutable

const

mutable

D.    Undefined behavior

E.    Fails to compile

 

【Q6】 Which of the following are possible outputs of the C++ code below?

#include <iostream>

 

class TestPrint

{

public:

 

void Print()

{

std::cout << "TestPrint" << std::endl;

}

 

void Print() const

{

std::cout << "const TestPrint" << std::endl;

}

 

void Print() volatile

{

std::cout << "volatile TestPrint" << std::endl;

}

 

void Print() const volatile

{

std::cout << "const volatile TestPrint" << std::endl;

}

};

 

int main(int argc, char* argv[])

{

TestPrint normal_test;

normal_test.Print();

 

const TestPrint const_test;

const_test.Print();

 

volatile TestPrint volatile_test;

volatile_test.Print();

 

const volatile TestPrint const_volatile_test;

const_volatile_test.Print();

}

 

A.    TestPrint

const TestPrint

const volatile TestPrint

const volatile TestPrint

B.    TestPrint

const volatile TestPrint

const volatile TestPrint

const volatile TestPrint

C.    TestPrint

const TestPrint

volatile TestPrint

const volatile TestPrint

D.    TestPrint

TestPrint

TestPrint

TestPrint

E.    TestPrint

const volatile TestPrint

volatile TestPrint

const volatile TestPrint

 

【Q7】 Which exception handler will catch the exception throw by SomeFunction in C++?

#include <iostream>

 

class ExBase

{

 

};

class ExDer:public ExBase{};

void SomeFunction()

{

ExBase base;

throw base;

}

 

void main()

{

try

{

SomeFunction();

}

catch(ExDer& ex)

{

std::cout << "ExDer& handler/n";

}

catch(ExDer* ex)

{

std::cout << "ExDer* handler/n";

}

catch(ExBase& ex)

{

std::cout << "ExBase& handler/n";

}

catch(ExBase* ex)

{

std::cout << "ExBase* handler/n";

}

}

 

A.    ExBase& handler

B.    ExDer& handler

C.    ExBase* handler

D.    ExDer* handler

E.    The compiler will throw an ambiguity error as it will not able to determine which catch handler must be invoked.