Jul 13, 2011

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

说明:下划线是我认为的答案,仅供参考。
【Q1】 When using C++ iostreams as shown in the code snippet below, which of the following statements will print ”3.14” to the display?
#include <iostream>
#include <iomanip>

int main(int argc, char* argv[])
{
using namespace std;
const double pi = 3.14159265358979323;

//Statement (s) goes here
}

A.    cout << format(“%.2f”, pi) << endl;
B.    cout << setw(3) << pi << endl;
C.    cout << setprecision(3) << pi << endl;
D.    cout.precision(3);
cout << pi << endl;
E.    cout << “%.2f” << pi << endl;

【Q2】 Which of the following C++ keywords are used to create an alias of a type?
A.    const
B.    typedef
C.    ref
D.    enum
E.    #define

【Q3】 Which of the following are appropriate uses of the const type-modifier when declaring or defining variables in C++?
A.    When instancing objects that should never be modified.
e.g. const Point2D origin = Point2D(0.0., 0.0);
B.    When defining mathematical constants wht never change.
e.g. const double sqrt2 = std::sqrt(2.0);
C.    When defining a reference parameter that is not modified by the called function.
e.g. complex add(const complex& a, const complex& b);
D.    When defining flags that should rarely be changed.
e.g. const bool exit_application = false;
E.    When defining a reference to ensure it will always reference the same object.
e.g. const Object& assign_once = other_object;
assign_once.MutatingMethod();

【Q4】 Which of the following statements accurately present the access control in C++?
A.    Protected members of a class are accessible only by member functions and friends of that class.
B.    Private members of a class are accessible only by member functions and friends of that class.
C.    Protected members of a class are accessible only by member functions of that class.
D.    Private members of a class are accessible only by member functions of that class.
E.    Public members of a class are accessible only by everyone except friend functions.

【Q5】 For std::vector, which C++ member function should be invoked to pre-allocate storage without constructing new elements?
A.    std::vector::malloc(int)
B.    std::vector::reserve(size_t)
C.    std::vector::new(size_t)
E.    std::vector::allocate(size_t)
E.    std::vector::resize(size_t)

【Q6】 Given the C++ code below, which of the following are correct declarations for a copy constructor for a class named Vector?
class Vector
{
public:
Vector();
~Vector();
//declaration goes here
}
A.    Vector(const Vector&) const;
B.    Vector(const Vector& copy_me);
C.    Vector(const Vector& v);
D.    Vector(const Vector&);
E.    Vector(const Vector* copy_me) const;

【Q7】 In C++, which of the following declarations will correctly declare an overloaded insertion operator for the count object that displays an object of a user-defined class MyString?
int main
{
//…
cout << objMyString << endl;
}
A.    void MyString::operator <<(ostream&);
B.    void MyString::operator <<( MyString&);
C.    ostream operator <<( MyString&);
D.    std::ostream operator <<(std::ostream&, MyString&);
E.    void std::ostream operator <<(MyString &);

【Q8】 Which of the following operations of the list<T> container take a variable amount of time to complete in C++?
A.    Access item in the middle of the list
B.    Insert item in the middle of the list
C.    Insert item at the beginning of the list
D.    Insert item at the end of the list
E.    Access item at the end of the list

【Q9】 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.

【Q10】 Which of the following statements correctly describe the C++ language and its libraries?
A.    It is possible to declare a function pointer to the constructor of a C++ class.
B.    Though one can override the << operator of the ostream class, it is not possible to do so for the ofstream class because it does not use a buffered output stream.
C.    The C++ language does not have built in features for persisting or serializing objects to disk and back.
D.    It is not possible to declare a pointer to a private data member of a C++ class.
E.    If the << operator of the ostream class has been templatized, it cannot be specialized.

No comments:

Post a Comment