Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

Chapter 19 – C How to Program 6e Multiple Choice Test Bank


 

Download  file with the answers

Not a member!
Create a FREE account here to get access and download this file with answers


Chapter 19: Operator Overloading; String and Array Objects

Section 19.1 Introduction
19.1 Q1: Which statement about operator overloading is false?
a. Operator overloading is the process of enabling C++’s operators to work with class objects.
b. C++ overloads the addition operator (+) and the subtraction operator (-) to perform differently, depending on their context in integer, floating-point and pointer arithmetic with data of fundamental types.
c. You can overload all C++ operators to be used with class objects.
d. When you overload operators to be used with class objects, the compiler generates the appropriate code based on the types of the operands.

Section 19.2 Using the Overloaded Operators of Standard Library Class string

19.2 Q1: Which of the following is false?
a. A string can be defined to store any data type.
b. Class string provides bounds checking in its member function at.
c. Class string’s overloaded [] operator returns a vector element as an rvalue or an lvalue, depending on the context.
d. An exception is thrown if the argument to string’s at member function is an invalid subscript.

Section 19.3 Fundamentals of Operator Overloading

19.3 Q1: To use an operator on user-defined class objects, operator overloading:
a. Must always be used.
b. Must always be used, with three exceptions.
c. Must never be used, with three exceptions.
d. Must never be used.

19.3 Q2: The correct function name for overloading the addition (+) operator is:
a. operator+
b. operator(+)
c. operator:+
d. operator_+

19.3 Q3: Which of the following operators cannot be overloaded?
a. The . operator.
b. The -> operator.
c. The & operator.
d. The [ ] operator.

19.3 Q4: Which statement about operator overloading is false?
a. New operators can never be created.
b. Certain overloaded operators can change the number of arguments they take.
c. The precedence of an operator cannot be changed by overloading.
d. Overloading cannot change how an operator works on built-in types.

19.3 Q5: To implicitly overload the += operator:
a. Only the + operator needs to be overloaded.
b. Only the = operator needs to be overloaded.
c. Both the + and = operators need to be overloaded.
d. The += operator cannot be overloaded implicitly.

Section 19.4 Overloading Binary Operators

19.4 Q1: Which of the following operators can be overloaded as a non-member function?
a. ()
b. []
c. +=
d. ==

19.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.

19.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.

19.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
d. y operator+=( y + z )

19.4 Q5: For operators overloaded as non-static member functions:
a. Binary operators can have two arguments and unary operators can have one.
b. Both binary and unary operators take one argument.
c. Binary operators can have one argument, and unary operators cannot have any.
d. Neither binary nor unary operators can have arguments.

Section 19.5 Overloading the Binary Stream Insertion and Stream Extraction Operators

19.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 )

Section 19.6 Overloading Unary Operators

19.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.

Section 19.7 Overloading the Unary Prefix and Postfix ++ and — Operators

19.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.

19.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.

Section 19.8 Case Study: A Date Class

19.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:
a. Date Date::operator++( int )
{
Date temp = *this;
Increment();
return *temp;
}
b. Date Date::operator++( int )
{
Increment();
Date temp = *this;
return temp;
}
c. Date Date::operator++( int )
{
Date temp = *this;
return this;
temp.Increment();
}
d. Date Date::operator++( int )
{
Date temp = *this;
Increment();
return temp;
}

Section 19.9 Dynamic Memory Management

19.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.

19.9 Q2: The delete operator:
a. Can terminate the program.
b. Must be told which destructor to call when destroying an object.
c. Can delete an entire array of objects declared using new.
d. Is called implicitly at the end of a program.

Section 19.10 Case Study: Array Class

19.10 Q1: Which of the following is false?
a. An entire non-char array cannot be input or output at once.
b. Two arrays cannot be meaningfully compared with equality or relational operators.
c. Arrays cannot be assigned to one another (i.e., array1 = array2;).
d. C++ ensures that you cannot “walk off” either end of an array.

19.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.

19.10 Q3: A copy constructor:
a. Is a constructor with only default arguments.
b. Is a constructor that initializes a newly declared object to the value of an existing object of the same class.
c. Is a constructor that takes no arguments.
d. None of the above.

19.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.

19.10 Q5: To prevent class objects from being copied:
a. Make the overloaded assignment operator private.
b. Make the copy constructor private.
c. Both (a) and (b).
d. None of the above.

Section 19.11 Operators as Member Function vs. Non-Member Functions

19.11 Q1: Which statement is false?
a. Based on whether an operator is implemented as a member function or as a non-member function, the operator is used differently in expressions.
b. When an operator function is implemented as a member function, the leftmost (or only) operand must be an object (or a reference to an object) of the operator’s class.
c. Operator member functions of a specific class are called (implicitly by the compiler) only when the left operand of a binary operator is specifically an object of that class, or when the single operand of a unary operator is an object of that class.
d. Another reason why you might choose a non-member function to overload an operator is to enable the operator to be commutative.

Section 19.12 Converting between Types

19.12 Q1: Conversion constructors:
a. Can have multiple arguments.
b. Can convert between user-defined types.
c. Are implicitly defined by the compiler if not explicitly written by the programmer.
d. Cannot convert built-in types to user defined types.

19.12 Q2: The prototypes of overloaded cast operator functions do not:
a. Specify the type they convert to.
b. Specify the type that is being converted.
c. Specify a return type.
d. Need to be defined inside the class whose objects are being converted.

19.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;

Section 19.13 explicit Constructors

19.13 Q1: An explicit constructor:
a. Cannot be called outside of the class it is declared in.
b. Can be implicitly called by the compiler to perform a data type conversion.
c. Does not initialize its class’s data members.
d. Must take exactly one argument.

Section 19.14 Case Study: Building a String Class

19.14 Q1: Conversion constructors cannot:
a. Be applied implicitly.
b. Be used to convert the arguments for overloaded operators to the types needed by those overloaded operators.
c. Take exactly one argument.
d. Be used implicitly in series to match the needs of an overloaded operator.

19.14 Q2: 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.

19.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”

About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!