Introduction to C++ Programming, Y. Daniel Liang
Chapter 13 Operator Overloading
Section 13.2 The Rational Class
1
Which of the following statements creates a Rational object?
A.
Rational r1(1, 2);
B.
Rational r1();
C.
Rational r1;
D.
Rational r1(1, 2, 3);
2
What is the printout of the following code?
  
   #include <iostream>
   #include "Rational.h"
   using namespace std;
   int main()
   {
     cout << Rational().toString();
     return 0;
   }
A.
0/0
B.
0/1
C.
0
D.
It is a syntax error.
3
What is the printout of the following code?
  
   #include <iostream>
   #include "Rational.h"
   using namespace std;
   int main()
   {
     Rational r1(1, 2);
     Rational r2(2, 4);
     cout << r1.equals(r2);
    
     return 0;
   }
A.
true
B.
false
C.
1
D.
0
4
What is the printout of the following code?
  
   #include <iostream>
   #include "Rational.h"
   using namespace std;
   int main()
   {
     Rational r1(1, 2);
     Rational r2(1, 3);
     cout << r1.add(r2).toString() << endl;
    
     return 0;
   }
A.
5/6
B.
1/2
C.
1/3
D.
1/6
5
What is the printout of the following code?
  
   #include <iostream>
   #include "Rational.h"
   using namespace std;
   int main()
   {
     Rational r1(1, 2);
     cout << r1.doubleValue();
    
     return 0;
   }
A.
0
B.
1
C.
0.5
D.
0.1
6
Consider the Rational class defined in this section. Which of the following statements are true?
A.
The Rational class is immutable.
B.
The string class is immutable.
C.
The vector class is immutable.
D.
Once a Rational object is created, you cannot change its contents.
Section 13.3 Operator Functions
7
The signature for the < operator function for comparing two Rational objects is __________.
A.
bool operator<(Rational &secondRational)
B.
bool <operator(Rational &secondRational)
C.
bool operator<(Rational secondRational)
D.
bool operator(<)(Rational &secondRational)
8
You can overload the following operators.
A.
+
B.
+=
C.
>
D.
&&
E.
?:
9
To add Rational objects r1 to r2, use _______________.
A.
r2 = r2.add(r1);
B.
r2 += r1;
C.
r2 = r2.operator+=(r1);
D.
r2 = r1.operator+=(r2);
Section 13.4 Overloading the Shorthand Operators
10
What is wrong if the += operator is implemented as follows:
Rational Rational::operator+=(Rational &secondRational)
{
   this->add(secondRational);
   return this;
}
A.
You should replace return this by retrun (*this).
B.
You should replace this->add(secondRational) by *this = this->add(secondRational).
Section 13.5 Overloading the [] Operators
11
Suppose r is a Rational object. You can access _______.
A.
r[0]
B.
r[1]
C.
r[2]
D.
r[3]
12
What is the correct signature for the overloaded subscript operator []?
A.
long Rational::operator[](const int &index)
B.
long& operator[](const int &index);
C.
&long operator[](const int &index);
D.
long operator&[](const int &index);
Section 13.6 Overloading the Unary Operators
13
What is the correct signature for the overloaded unary operator +?
A.
Rational Rational::operator+(const Rational &r)
B.
Rational Rational::operator+()
C.
Rational Rational::operator<+>(const Rational &r)
D.
Rational Rational::operator(+)(const Rational &r)
Section 13.7 Overloading the ++ and -? Operators
14
What is the correct signature for the overloaded postfix ++ operator?
A.
Rational operator++(Rational &r)
B.
Rational operator++()
C.
Rational operator++(int dummy)
D.
Rational operator++(int &dummy)
Section 13.8 Overloading the << and >> Operators
15
If the << operator does not access the private data fields in Rational, do you still have to declare it friend?
A.
Yes
B.
No
16
What is the correct signature for the overloaded >> operator?
A.
friend istream &operator>>(istream &stream, const Rational &rational);
B.
friend istream &operator>>(istream &stream, Rational &rational);
C.
friend istream operator>>(istream &stream, Rational &rational);
D.
friend istream operator>>(istream &stream, const Rational &rational);
Section 13.9 Object Conversion
17
What is the correct signature for a function that converts a Rational to double?
A.
double operator()
B.
double operator double()
C.
Rational operator double()
D.
operator double()
Section 13.11 Overloading the = Operators
18
What is the correct signature for the = operator function?
A.
operator==(const Rational &secondRational);
B.
operator=(const Rational &secondRational);
C.
Rational operator=(const Rational &secondRational);
D.
Rational operator==(const Rational &secondRational);