C++ How To Program Early Objects Version 9th Edition Deitel Test Bank Download
C++ How To Program Early Objects Version 9th Edition Deitel Test Bank Download
Section 10.2 Using the Overloaded Operators of Standard Library Class string
10.3 Q1: The correct function name for overloading the addition ( +) operator is:
a. operator+
© Copyright 1992-2012 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.
C++ How to Program, 8/e Multiple Choice Test Bank 2 of 7
b. operator(+)
c. operator:+
d. operator_+
ANS a. operator+
10.4 Q1: Which of the following operators can be overloaded as a non-member function?
a. ()
b. []
c. +=
d. ==
ANS: d. ==
10.4 Q2: Which situation would require the operator to be overloaded as a non-member function?
a. The overloaded operator is =.
b. The left most operand must be a class object (or a reference to a class object).
c. The left operand is an int.
d. The operator returns a reference.
ANS c. The left operand is an int.
10.4 Q3: An overloaded + operator takes a class object and a double as operands. For it to be commutative (i.e., a
+ b and b + a both work):
a. operator+ must be a member function of the class from which the objects are instantiated.
b. operator+ must be a non-member function.
c. It must be overloaded twice; the operator+ function that takes the object as the left operand must be a
member function, and the other operator+ function must be a global function.
d. The + operator cannot be overloaded to be commutative.
ANS c. It must be overloaded twice; the operator+ function that takes the object as the left operand must be
a member function, and the other operator+ function must be a global function.
10.4 Q4: y and z are user-defined objects and the += operator is an overloaded member function. The operator is
overloaded such that y += z adds z and y, then stores the result in y. Which of the following expressions is always
equivalent to y += z?
a. y = y operator+= z
b. y.operator+=( z )
c. y = y + z
© Copyright 1992-2012 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.
C++ How to Program, 8/e Multiple Choice Test Bank 3 of 7
d. y operator+=( y + z )
ANS: b. y.operator+=( z )
Section 10.5 Overloading the Binary Stream Insertion and Stream Extraction Operators
10.5 Q1: Suppose you have a programmer-defined data type Data and want to overload the << operator to output
your data type to the screen in the form cout << dataToPrint; and allow cascaded function calls. The first line
of the function definition would be:
a. ostream &operator<<( ostream &output, const Data &dataToPrint )
b. ostream operator<<( ostream &output, const Data &dataToPrint )
c. ostream &operator<<( const Data &dataToPrint, ostream &output )
d. ostream operator<<( const Data &dataToPrint, ostream &output )
ANS: a. ostream &operator<<( ostream &output, const Data &dataToPrint )
10.6 Q1: Suppose the unary ! operator is an overloaded member function of class String. For a String object s,
which function call is generated by the compiler when it finds the expression !s?
a. s.operator!()
b. s.operator!( default_value1, default_value2,…)
c. operator!( s )
d. A compiler error results because no arguments are given.
ANS: a. s.operator!()
Section 10.7 Overloading the Unary Prefix and Postfix ++ and -- Operators
10.7 Q1: The conventional way to distinguish between the overloaded preincrement and postincrement operators
(++) is:
a. To assign a dummy value to preincrement.
b. To make the argument list of postincrement include an int.
c. To have the postincrement operator call the preincrement operator.
d. Implicitly done by the compiler.
ANS: b. To make the argument list of postincrement include an int.
10.7 Q2: Because the postfix increment operator returns objects by value and the prefix increment operator returns
objects by reference:
a. Prefix increment has slightly more overhead than postfix increment.
b. The postfix increment operator returns the actual incremented object with its new value.
c. Objects returned by postfix increment cannot be used in larger expressions.
d. The postfix increment operator typically returns a temporary object that contains the original value of the
object before the increment occurred.
ANS: d. The postfix increment operator typically returns a temporary object that contains the original value
of the object before the increment occurred.
10.8 Q1: There exists a data type Date with member function Increment that increments the current Date object
by one. The ++ operator is being overloaded to postincrement an object of type Date. Select the correct
implementation:
© Copyright 1992-2012 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.
C++ How to Program, 8/e Multiple Choice Test Bank 4 of 7
10.9 Q1: Which of the following is false about the new operator and the object for which it allocates memory?
a. It calls the object’s constructor.
b. It returns a pointer.
c. It does not require the size of the object to be explicitly specified in the new expression.
d. It automatically destroys the object after main is exited.
ANS: d. It automatically destroys the object after main is exited.
10.9 Q3[C++11]: Which of the following statements about a unique_ptr object is true?
a. A unique_ptr is a “smart pointer” for managing dynamically allocated memory.
b. When a unique_ptr goes out of scope, its destructor automatically returns the managed memory to the
free store.
c. You must explicitly delete the memory that’s managed by a unique_ptr before the object goes out of
scope.
d. All of the above.
ANS: c. You must explicitly delete the memory that’s managed by a unique_ptr before the object goes out
of scope.
© Copyright 1992-2012 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.
C++ How to Program, 8/e Multiple Choice Test Bank 5 of 7
ANS: d. C++ ensures that you cannot “walk off” either end of an array .
10.10 Q2: The array subscript operator [], when overloaded, cannot:
a. Be used with linked list classes.
b. Take a float as an operand.
c. Take multiple values inside (e.g., [4,8]).
d. Take user-defined objects as operands.
ANS: c. Take multiple values inside (e.g., [4,8]).
10.10 Q4: A copy constructor must receive its argument by reference because:
a. Otherwise the constructor will only make a copy of a pointer to an object.
b. Otherwise infinite recursion occurs.
c. The copy of the argument passed by value has function scope.
d. The pointer needs to know the address of the original data, not a temporary copy of it.
ANS: b. Otherwise infinite recursion occurs.
10.10 Q6: Which of the following is not a disadvantage of default memberwise copy with objects containing
pointers?
a. Having the possibility of leaving a dangling pointer.
b. Allowing both objects to point to the same dynamically allocated storage.
c. Allowing the destructor of one object to be called while leaving the second pointer, to the same memory
location, intact.
d. Requiring the explicit overloading of the assignment operator.
ANS: d. Requiring the explicit overloading of the assignment operator.
10.10 Q7[C++11]: To prevent class objects from being copied or assigned, you can:
a. Declare as private the class’s copy constructor and overloaded assignment operator.
b. Declare the class’s copy constructor and overloaded assignment operator with with = delete after the
parameter list.
c. Simply do not declare a copy constructor or assignment operator in the class.
d. (a) or (b).
ANS: d. (a) or (b).
© Copyright 1992-2012 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.
C++ How to Program, 8/e Multiple Choice Test Bank 6 of 7
10.12 Q3: Which of the following lines would be the prototype for an overloaded cast operator function that
converts an object of user-defined type Time into a double?
a. Time::operator double() const;
b. Time::static_cast double() const;
c. Time::operator_cast(double) const;
d. Time::double() const;
ANS: a. Time::operator double() const;
10.14 Q3: Assume that the function call operator() is overloaded for data type String in the usual sense of
selecting a substring from a larger string. For a String object string1 with the character string "ABCDEFGHI",
what string does string1( 4 , 2 ) return?
a. "EF"
b. "EFGHI"
c. "CDEF"
d. "CD"
ANS: a. "EF"
© Copyright 1992-2012 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.
C++ How to Program, 8/e Multiple Choice Test Bank 7 of 7
© Copyright 1992-2012 by Deitel & Associates, Inc. and Pearson Education, Inc. All Rights Reserved.