首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

几道C++笔试题,不定项选择,小弟我也不知道答案,高手解答,大家一起讨论吧

2012-02-20 
几道C++笔试题,不定项选择,我也不知道答案,高手解答,大家一起讨论吧1. Given the following program snipp

几道C++笔试题,不定项选择,我也不知道答案,高手解答,大家一起讨论吧
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. libraryclass 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 codeclass 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


[解决办法]
1. 选A
2. 只有D是标准的写法,E中的第三个参数是环境变量字符串的指针数组,虽然许多编译器支持,但不是标准的写法
3. D
4. AD
5. B
6. A

下面的先MARK
[解决办法]
楼主去的公司出的题目很厉害哦!
我没看是多选,全是单选的。请高手批评!

A
D
C
A
B
A
B
B
B
D
B
C
B
A
B
C
[解决办法]

探讨
等了1天,总算有人响应了T_T

第一题楼上2位选A都确定吗?
第二题为什么题目强调standard or standard conforming extensions?谁有标准和标准扩展翻翻。
第三题有C的情况吗?真没见过哦
第四题D是肯定可以编译和运行通过的。只是不推荐这么用。答案应该确定是AD没问题了。

[解决办法]
第7题 A. A preprocessor macro expansion cannot work when user-defined types are passed to it as arguments. 

I think this is wrong. Macros don't check types. So u can pass any type to them.

"user-defined types are passed to it as arguments" is confusing. Does it mean an object of the type or the type itself?

Can template function accept a type as an argument? I am not sure. Can anyone give me an example? thanks.
[解决办法]
mark,刚开始学C++,还没有看到类那一章。

第6题:
TC++PL 第三版特别版里面有,int i = i,一个变量从声明的那一点开始就是可见的了,而且我觉得你帖的题目可能有笔误,像下面这样才更迷惑人:
C/C++ code
int i = 1;void main(){    int i = i;}
[解决办法]
探讨
I think 'user-defined type' means non-built-in type of C++ such as class or struct. So template function can accept an object of a certain class as its parameter, whereas Macros can't. That's my understanding. What do you say?

热点排行