Jul 13, 2011

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

说明:下划线是我认为的答案,仅供参考。
【Q1】 Which of the following statements accurately describe unary operator overloading in C++?
A.    A unary operator can be overloaded with one parameter when the operator function is a class member.
B.    A unary operator can be overloaded with one parameter when the operator function is free standing function (not a     class member).
C.    A unary operator can only be overloaded if the operator function is a class member.
D.    A unary operator can be overloaded with no parameters when 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).

【Q2】 Which of the following statements correctly describe the code below in C++?
#define language 437                   //Line 1
#if language < 400
#undef language                          //Line 2
#else                                          //Line 3
#define language 850                   //Line 4
#ifdef language                           //Line 5
printf("%d", language);        //Line 6
#endif
#endif
A.    An error or warning will occur on Line 6 because a macro cannot be used as part of a preprocessor directive.
B.    An error or warning will occur on Line 2 because #undef is not a valid preprocessor directive.
C.    An error or warning will occur on Line 4 because language has already been defined.
D.    If Line 1 is changed to #define language 300, Line 6 will print "850".
E.    An error or warning will occur on Line 3 because #else can only be used as the last conditional in the chain.

【Q3】 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.    Since the preprocessor does the macro expansion and not the compiler, the build process takes a longer period of time.
C.    While expanding #define macros, the preprocessor does no type checking on the arguments to the macro.
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.

【Q4】 Which of the following are container adapters in the STL (Standard Template Library) in C++?
A.    list
B.    map
C.    stack
D.    queue
E.    deque

【Q5】 Which of the following correctly identify benefits of the getline() member function for cin over the extraction operator in C++?
A.    The getline() function by default, accepts whitespace, and returns on seeing the /n character, whereas the extraction operator returns when it encounters any whitespace character.
B.    Delimiters indicating end of input can be specified to the getline() function, whereas the extraction operator has no such facility.
C.    The getline() function can be overloaded to accept different argument types, whereas the extraction operator cannot be overloaded.
D.    The number of bytes to read can be specified to the getline() function, whereas it cannot be done with the extraction operator.
E.    The getline() function can be used like a manipulator with cin, whereas the extraction operator cannot be used as a manipulator.

【Q6】 In which of the following scenarios is a Copy Constructor called or invoked?
A.    When no conversion function exists for converting the class object to another class object
B.    When an existing object is assigned an object of its own class
C.    When a function receives as an argument, an object of the class, by value
D.    When a function returns an object of the class by value
E.    When creating an object and initializing it with an object of its own class

【Q7】 Which of the following statements describe the result when standard new cannot allocate the requested storage in C++?  (Note: older compilers may not implement standard behavior).
A.    It throws a bad_alloc exception.
B.    It returns null.
C.    It returns silently with no effect on the expression.
D.    It throws an insufficient_memory exception.
E.    It logs an error message to the mem_err.log file.

【Q8】 In C++, which of the following is the best declaration for an overloaded operator[] to allow read-only access (and only read-only access) to the data?
template<typename T>
class MyArray
{
//declaration goes here
};

A.    T& operator[](size_t i);
B.    const T& operator[](size_t i);
C.    const T& operator[](size_t i) const;
D.    T& operator[](size_t i)const;
E.    T& operator[](const size_t i);

【Q9】 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.    int main()
B.    void main(char* argv[], int argc)
C.    int main(int argc, char* argv[])
D.    void main()
E.    int main(int argc, char* argv[], char* arge[])

【Q10】 What is the correct declaration for a file stream insertion operator for a class my_stuff::my_class as indicated in the C++ code snippet below?
#include <fstream>

namespace my_stuff
{
class my_class
{
public:
int i;
};

//Declaration goes here

}//ns my_stuff

A.    std::ofstream& operator<<(std::ofstream& ofs, const my_class&);
B.    const my_class& operator<<(const my_class&)
C.    std::fstream& operator<<(std::fstream& fs, const my_class&)
D.    std::ifstream& operator<<(std::ifstream& ifs, const my_class&)
E.    void operator<<(const my_class&)

1 comment:

  1. 这80道C++笔试题似乎是IKM的网络试题...过几天就要考了,临时抱佛脚ing......怨念...

    ReplyDelete