Jul 13, 2011

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

说明:下划线是我认为的答案,仅供参考。
【Q1】 Which of the following define valid string constants in C++?
A.    #define MESSAGE "Whoever said that you could run this program" &
"must not have known what you'd do."
B.    #define REETING = "Hello!"
C.    #define MESSAGE = "This is a long message, but I know you have"
#define MESSAGE = MESSAGE + " plenty of time to read it."
D.    #define ERROR_MSG "You did something very very wrong and now the /
program will terminate."
E.    #define MESSAGE "Hello "
#concat MESSAGE & #UserName

【Q2】  What member function of std::fstream could a C++ developer invoke in order to change the target output filebuf of the fstream?
A.    setbuffer
B.    setfilebuf
C.    rdbuf
D.    filebuf
E.    streambuf

【Q3】 Which of the following options are returned by the typeid operator in C++?
A.    A reference to a const std::type_info object
B.    A const std::type_info object
C.    A const reference to a const std::type_info object
D.    A reference to a std::type_info object
E.    A const reference to a std::type_info object

【Q4】 Which of the following statements describe correct methods of handling C++ exceptions?
A.    Once an exception is thrown, the compiler unwinds the heap, freeing any memory dynamically allocated within the block from which the exception was thrown.
B.    In a hierarchy of exception classes, the order of handling exceptions can be from the most specific class to the most general class.
C.    If an exception is caught by its address or pointer, it is the responsibility of the thrower to release the memory occupied by the exception.
D.    Catching an exception by reference is preferable to catching it by value.
E.    To write an exception handler, it is essential to know the concrete class of exception to catch.

【Q5】 Which of the following statements correctly describe functions of the endl manipulator for the ostream object cout in C++?
A.    It only flushes the standard output stream.
B.    It puts a newline character into the standard output stream and flushes the standard output stream.
C.    It puts an end-of-output character into the standard output stream.
D.    It only puts a newline character into the standard output stream.
E.    It indicates end-of-output and closes the standard output stream.

【Q6】 In C++, which of the following statements accurately describe a base class destructor calling a virtual function override of a derived class?
A.    The base class destructor calls the virtual function override of the derived class through the vtable.
B.    The base class destructor cannot call the virtual function override of the derived class because the derived class portion of the data may be in an undefined state.
C.    The base class destructor calls the virtual function of the base class and not of the derived class.
D.    The C++ compiler maintains the overridden virtual function pointers in a separate structure when it sees the call in a destructor. The call is then resolved through this structure.
E.    The language does not permit calling a virtual function override in either a constructor or the destructor of the base class.

【Q7】 In which of the following situations is the unexpected() handler called in C++?
A. When a function throws an exception of an undefined type
B. When an appropriate catch block is not present in the calling function to catch an exception thrown by the called function
C. When the stack gets corrupted during unwinding as a result of a thrown exception
D. When a function that has an "exception specification" throws an exception not listed in that specification.
E. When a constructor throws an exception

【Q8】 Which of the following classes must be instantiated so that the object can be used both for reading and writing to the same file in C++?
A. stream
B. ofstream
C. fstream
D. ifstream
E. iostream

【Q9】 A class der inherits from class base and the functions are defined in the code below:
void SomeFunc(base& b){ ... }
void SomeFunc(base b){ ... }
void SomeFunc(der& b){ ... }
void SomeFunc(der b){ ... }

void main()
{
der d;
SomeFunc(d);
}
In the call to SomeFunc(d), which of the following overloads of the SomeFunc function will be executed in C++?
A. void SomeFunc(base& b){ ... }
B. The compiler will generate an ambiguous call error.
C. void SomeFunc(der& b){ ... }
D. void SomeFunc(base b){ ... }
E. void SomeFunc(der b){ ... }

【Q10】 The code below generates the compiler error "'Derived::data' is ambiguous". In C++, which of the following steps can be taken to rectify it so that obj has only one instance of data?
class Base
{
public:
int data;
};
class DerivedOne : public Base {};
class DerivedTwo : public Base {};
class Derived : public DerivedOne, public DerivedTwo {};

void main()
{
Derived obj;
obj.data = 5;
}
A. Inherit class Derived from DerivedOne and DerivedTwo as virtual public DerivedOne and virtual public DerivedTwo.
B. Inherit classes DerivedOne and DerivedTwo from Base as virtual public Base.
C. Inherit classes DerivedOne and DerivedTwo from Base as public virtual Base.
D. Inherit class Derived from DerivedOne and DerivedTwo as public virtual DerivedOne and public virtual DerivedTwo.
E. Inherit either class DerivedOne or class DerivedTwo from Base as virtual public Base

No comments:

Post a Comment