Deck 10: Operator Overloading; String and Array Objects

Full screen (f)
exit full mode
Question
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.
Use Space or
up arrow
down arrow
to flip the card.
Question
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;
}
Question
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.
Question
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.
Question
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.
Question
Which of the following operators can be overloaded as a non-member function?

A) ()
B) []
C) +=
D) ==
Question
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.
Question
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.
Question
The code ", have a great day!"s
Is an example of ________:

A) a compilation error
B) a string iterator
C) a string-object literal.
D) string interpolation.
Question
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)
Question
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.
Question
Which of the following operators cannot be overloaded?

A) The . operator.
B) The -> operator.
C) The & operator.
D) The [ ] operator.
Question
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.
Question
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)
Question
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.
Question
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.
Question
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.
Question
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.
Question
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.
Question
The correct function name for overloading the addition (+) operator is:

A) operator+
B) operator(+)
C) operator:+
D) operator_+
Question
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;
Question
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.
Question
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.
Question
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.
Question
Which of the following is false?

A) To receive a list initializer as an argument to a constructor, you can declare the constructor's parameter as type list_initialier where T represents the type of the values in the list initializer.
B) To receive a list initializer as an argument to a constructor, you can declare the constructor's parameter as type initializer_list where T represents the type of the values in the list initializer.
C) It's not possible to pass a list initializer to a constructor.
D) (a) and (c).
Question
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.
Question
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.
Question
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.
Question
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).
Question
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.
Question
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.
Question
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.
Question
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.
Question
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"
Unlock Deck
Sign up to unlock the cards in this deck!
Unlock Deck
Unlock Deck
1/34
auto play flashcards
Play
simple tutorial
Full screen (f)
exit full mode
Deck 10: Operator Overloading; String and Array Objects
1
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.
C
2
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;
}
D
3
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.
C
4
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.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
5
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.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
6
Which of the following operators can be overloaded as a non-member function?

A) ()
B) []
C) +=
D) ==
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
7
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.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
8
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.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
9
The code ", have a great day!"s
Is an example of ________:

A) a compilation error
B) a string iterator
C) a string-object literal.
D) string interpolation.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
10
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)
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
11
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.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
12
Which of the following operators cannot be overloaded?

A) The . operator.
B) The -> operator.
C) The & operator.
D) The [ ] operator.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
13
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.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
14
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)
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
15
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.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
16
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.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
17
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.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
18
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.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
19
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.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
20
The correct function name for overloading the addition (+) operator is:

A) operator+
B) operator(+)
C) operator:+
D) operator_+
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
21
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;
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
22
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.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
23
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.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
24
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.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
25
Which of the following is false?

A) To receive a list initializer as an argument to a constructor, you can declare the constructor's parameter as type list_initialier where T represents the type of the values in the list initializer.
B) To receive a list initializer as an argument to a constructor, you can declare the constructor's parameter as type initializer_list where T represents the type of the values in the list initializer.
C) It's not possible to pass a list initializer to a constructor.
D) (a) and (c).
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
26
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.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
27
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.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
28
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.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
29
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).
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
30
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.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
31
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.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
32
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.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
33
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.
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
34
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"
Unlock Deck
Unlock for access to all 34 flashcards in this deck.
Unlock Deck
k this deck
locked card icon
Unlock Deck
Unlock for access to all 34 flashcards in this deck.