Jul 13, 2011

[ZT]网上收集的C++笔试题目(7)

说明:下划线是我认为的答案,仅供参考。
【Q1】 Which of the following must be ensured in order to implement a polymorphic function in C++?
A.    There has to be a pointer of the derived class that has implemented the polymorphic function that holds the address of the derived class object.
B.    The function must be declared as virtual in both the base class and in the derived class that overrides the function.
C.    The function must be declared as pure virtual.
D.    There has to be a base class pointer holding the address of a base or derived class object that has implemented the polymorphic function.
E.    The function must be declared as virtual in the base class.

【Q2】 In C++, which of the following are valid uses of the std::auto_ptr template considering the class definition below?
class Object
{
public:
virtual ~Object() {}
//...
};
A.    std::auto_ptr <Object> pObj(new Object);
B.    std::vector <std::auto_ptr <Object*> > object_vector;
C.    std::auto_ptr <Object*> pObj(new Object);
D.    std::vector <std::auto_ptr <Object> > object_vector;
E.    std::auto_ptr <Object> source()
{
return new Object;
}

【Q3】 Which of the following statements accurately describe the C++ code excerpt below?
int* arr = new int[10];
delete arr;
A.    delete will fail.
B.    An exception may be thrown at time.
C.    delte[] arr; must be used to properly deallocate arr.
D.    The compiler will emit a diagnostic error.
E.    The free-store may be corrupted.

【Q4】 Referring to the sample code below, why are the default and copy constructors declared as protected?
class A
{
public:
void f();
protected:
A()
{
cout << "A:A()" << endl;
}
A(const A&){}
};
A.    To ensure that instances of A cannot be copied
B.    To ensure that instances of A cannot be created via new by a more derived class
C.    To ensure that instances of A can only be created by subclasses of A
D.    To ensure that A cannot be instantiated on the stack
E.    To ensure that A cannot be used as a base class

【Q5】 In which of the following scenarios should a destructor be declared virtual in a base class?
A.    When the destructor of the base class will be doing the clean-up of the derived class data;
B.    When the developer wants to ensure that a derived class destructor is invoked when an instance is destroyed through a base class pointer;
C.    When an implementation for the derived class destructor is not desired.
D.    When a derived class allocates system resources which are released in its destructor;
E.    When the default constructor of the base class is declared as virtual;

【Q6】 A C++ developer wants to handle a static_cast<char*>() operation for the class String shown below. Which of the following options are valid declarations that will accomplish this task?
class String
{
public:
//...
//declaration goes here
};

A.    char* operator();
B.    char* operator char*();
C.    String operator char*();
D.    operator char*();
E.    char* operator String();

【Q7】 Which of the following expression(s) will not cause an error when used to replace the ***** in the C++ code excerpt below?
#include <iostream>

class Outer
{
public:
static int m_Out;
class Inner
{
public:
static int m_In;
void Display();
};
};

int Outer::m_Out = 10;
int Outer::Inner::m_In = 25;

void Outer::Inner::Display() {  std::cout << m_Out << std::endl;  }

void main()
{
Outer    objOut;
Outer::Inner objIn;
*****
}
A. objOut.m_In;
B. objIn.m_Out;
C. objIn.Display();
D. objIn.Outer::m_Out;
E. objOut.Inner::m_In;

【Q8】 Which of the following statements correctly describe the results of executing the code below in C++?
#include <iostream>
using namespace std;

class ExBase
{
private:
static int stat;
public:
static int GetStat(){ return stat; }
};

int ExBase::stat = 25;

class ExDer1 : public ExBase
{
public:
friend int Der1Fn(){ return ExBase::stat; }
};

class ExDer2 : public ExBase{};

class ExDer : public ExDer1, public ExDer2{};

A.    int main()
{
ExDer d;
cout <<d.Der1Fn() << endl;
}
will result in an ambiguity error from the compiler.
B.    int main()
{
ExDer d;
cout <<d.GetStat() << endl;
}
will display an output as "25".
C.    int main()
{
cout << ExDer1::GetStat() << endl;
}
will result in an ambiguity error from the compiler.
D.    class ExDer1 : public ExBase
{
public:
friend int Der1Fn(){ return ExBase::stat; }
};
will result in an access error from the compiler.
E.    int main()
{
cout << ExDer1::ExBase::GetStat() << endl;
}
will display an output as "25".

【Q9】 Given the following program snippet, what can we conclude about the use of dynamic_cast in C++?
#include <iostream>
#include <memory>

//Someone else's code,E.g. library
class IGlyph
{
public:
virtual ~IGlyph(){}
virtual std::string Text()=0;
virtual IIcon*      Icon()=0;
//...
};

class IWidgetSelector
{
public:
virtual ~IWidgetSelector(){}

virtual void    AddItem(IGlyph*)=0;
virtual IGlyph* Selection()=0;
};

//Your code
class MyItem : public IGlyph
{
public:
virtual std::string Text()
{
return this->text;
}

virtual IIcon* Icon()
{
return this->icon.get();
}

void Activate()
{
std::cout << "My Item Activated" << std::endl;
}

std::string          text;
std::auto_ptr<IIcon> icon;
};

void SpiffyForm::OnDoubleClick(IWidgetSelector* ws)
{
IGlyph* gylph = ws->Selection();
MyItem* item  = dynamic_cast<MyItem*>(gylph);
if(item)
item->Activate();
}

A.    The dynamic_cast ought to be a reinterpret_cast since the concrete type is unknown.
B.    The dynamic_cast is unnecessary since we know that the concrete type returned by IWidgetSelector::Selection() must be a MyItem object.
C.    The dynamic_cast is redundant, the programmer can invoke Activate directly,E.g. ws->Selection()->Activate();
D.    The dynamic_cast is necessary since we cannot know for certain what concrete type is returned by IWidgetSelector::Selection().
E.    A polymorphic_cast should be used in place of the dynamic_cast.

【Q10】 Which of the following identify const-correctness failures in the C++ program below?
template<typename T>
class MyArray
{
public:
MyArray();
MyArray(MyArray& copy);
MyArray& operator=(MyArray& copy);
//...
};

class MyData
{
public:
MyData(MyArray<int>& x, MyArray<int>& y);
//...
const MyArray<int>& x();
const MyArray<int>& y();
};

MyArray<int> read_data(int*, char**);
void make_changes(MyData* edit);

int main(int argc, char* argv[])
{
const MyArray<int> read_x = read_data(&argc, argv);
const MyArray<int> read_y = read_data(&argc, argv);

MyData user_data(read_x, read_y);
MyData edit_buffer(user_data);
make_changes(&edit_buffer);
}

A.    MyData(MyArray<int>& x, MyArray<int>& y); should be
MyData(const MyArray<int>& x, const MyArray<int>& y);
B.    MyArray& operator=(MyArray& copy); should be
const MyArray& operator=(const MyArray& copy);
C.    void make_changes(MyData* edit); should be
void make_changes(const MyData* edit);
D.    MyArray(MyArray& copy); should be
MyArray(const MyArray& copy);
E.    const MyArray& operator=(const MyArray& copy); should be
const MyArray& operator=(const MyArray& copy) const;

No comments:

Post a Comment