[面试]几道C++面试题.有关于dynamic_cast in C++

http://topic.csdn.net/u/20081102/06/D838DF74-5164-41FE-B99F-E5201172FBCE.html

 

1. Given the following program snippet, what can we conclude about the use of dynamic_cast in C++?

C/C++ code
    
    
    
    
#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 is necessary since we cannot know for certain what concrete type is returned by IWidgetSelector::Selection().

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 ought to be a reinterpret_cast since the concrete type is unknown.

D. The dynamic_cast is redundant, the programmer can invoke Activate directly, e.g. ws->Selection()->Activate();

E. A polymorphic_cast should be used in place of the dynamic_cast.


2. Which of the following declarations of function main are standard or standard conforming extensions?
(Please note that some compilers accept ill-formed main declarations, these should be considered incorrect).


A. void main(char* argv[], int argc)
B. int main()
C. void main()
D. int main(int argc, char* argv[])
E. int main(int argc, char* argv[], char* arge[])

3. Which of the following statements accurately describe the condition that can be used for conditional compilation in C++?
A. The condition can depend on the value of program variables.
B. The condition can depend on the values of any const variables.
C. The condition can use the sizeof operator to make decisions about compiler-dependent operations, based on the size of standard data types."
D. The condition can depend on the value of environmental variables.
E. The condition must evaluate to either a "0" or a "1" during pre-processing.

4. 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;
}

5. Which of the following statements correctly describe C preprocessor directives in C++?
A. The #pragma directive is machine-independent.
B. Preprocessor directives are processed before macros are expanded.
C. The #import directive is used to copy code from a library into the program's source code.
D. Any number of #else directives can be used between an #if and an #endif.
E. The #include directive is used to identify binary files that will be linked to the program.

6. Which of the following statements describe the results of executing the code snippet below in C++?

C/C++ code
    
    
    
    
int var = 1 ; void main() { int i = i; }


A. The i within main will have an undefined value.
B. The i within main will have a value of 1.
C. The compiler will not allow this statement.
D. The compiler will allow this statement, but the linker will not be able to resolve the declaration of i.
E. The result is compiler-dependent.

7. Which of the following statements regarding the benefits of using template functions over preprocessor #define macros are correct?
A. A preprocessor macro expansion cannot work when user-defined types are passed to it as arguments.
B. While expanding #define macros, the preprocessor does no type checking on the arguments to the macro.
C. Since the preprocessor does the macro expansion and not the compiler, the build process takes a longer period of time.
D. A preprocessor macro expansion incurs a performance overhead at runtime.
E. It is simple to step into a template function code during the debugging process.

8. In a hierarchy of exception classes in C++, which of the following represent possible orders of catch blocks when a C++ developer wishes to catch exceptions of more than one class from a hierarchy of exception classes?
A. Classes belonging to the same hierarchy cannot be part of a common set of catch blocks.
B. The most derived classes must appear first in the catch order, and the parent classes must follow the child classes.
C. The most derived classes must appear last in the catch order, and the parent classes must precede the child classes.
D. The order is unimportant as exception handling is built into the language.
E. Multiple classes can be caught in a single catch clause as multiple arguments.

9. Which of the following statements provide a valid reason NOT to use RTTI for distributed (i.e. networked between different platforms) applications in C++?
A. RTTI is too slow.
B. RTTI does not have standardized run-time behavior.
C. RTTI uses too much memory.
D. RTTI's performance is unpredictable/non-deterministic.
E. RTTI frequently fails to function correctly at run-time.

10. Which of the following options describe the expected overhead for a class that has 5 virtual functions?
A. Every object of the class holds the address of the first virtual function, and each function in turn holds the address of the next virtual function.
B. Every object of the class holds the address of a link list object that holds the addresses of the virtual functions.
C. Every object of the class holds the addresses of the 5 virtual functions.
D. Every object of the class holds the address of a structure holding the addresses of the 5 virtual functions.
E. Every object of the class holds the address of the class declaration in memory, through which the virtual function calls are resolved.

11. 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 char*();
B. operator char*();
C. char* operator();
D. String operator char*();
E. char* operator String();

12. Which of the following options describe the functions of an overridden terminate() function?
A. It performs the desired cleanup and shutdown processing, and then throws a termination_exception.
B. It performs the desired cleanup and shutdown processing, and then returns an error status value to the calling function.
C. It performs the desired cleanup and shutdown processing, and then calls abort() or exit().
D. It performs the desired cleanup and shutdown processing, and if it has restored the system to a stable state, it returns a value of "-1" to indicate successful recovery.
E. It performs the desired cleanup and shutdown processing, and then calls the unexpected() handler.

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

14. Which of the following statements accurately describe unary operator overloading in C++?
A. A unary operator can be overloaded with no parameters when the operator function is a class member.
B. A unary operator can be overloaded with one parameter when the operator function is a class member.
C. A unary operator can be overloaded with one parameter when the operator function is free standing function (not a class member).
D. A unary operator can only be overloaded if the operator function is a class member.
E. A unary operator can be overloaded with no parameters when the operator function is a free standing function (not a class member).

15.  What is the correct syntax for portable fstream file paths?
(e.g. the string you would pass to std::fstream::open() to open a file.)

A. "::directory:file.bin"
B. "C:/Directory/File.bin"
C. "/directory/file.bin"
D. "C://Directory//File.bin"
E. std:fstream file paths are not portable.

16. When a Copy Constructor is not written for a class, the C++ compiler generates one. Which of the following statements correctly describe the actions of this compiler-generated Copy Constructor when invoked?
A. The compiler-generated Copy Constructor makes the object being constructed, a reference to the object passed to it as an argument.
B. The compiler-generated Copy Constructor does not do anything by default.
C. The compiler-generated Copy Constructor performs a member-wise copy of the object passed to it as an argument, into the object being constructed.
D. The compiler-generated Copy Constructor tags the object as having been Copy-Constructed by the compiler.
E. The compiler-generated Copy Constructor invokes the assignment operator of the class.

你可能感兴趣的:(C++,面试,object,function,Constructor,preprocessor)