Introduction to Programming with C++, Third Edition, Y. Daniel Liang

This quiz is for students to practice. A large number of additional quiz is available for instructors using Quiz Generator from the Instructor's Resource Website. Videos for Java, Python, and C++ can be found at https://yongdanielliang.github.io/revelvideos.html.

Many questions in this edition have been updated in the new edition. Please check with the publisher on the newest edition.

Chapter 14 Operator Overloading


Section 14.2 The Rational Class
14.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);

14.2  What will be displayed by 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 compile error.

14.3  What will be displayed by 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

14.4  What will be displayed by 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

14.5  What will be displayed by 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

14.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 14.3 Operator Functions
14.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)

14.8  You can overload the following operators.
A. +
B. +=
C. >
D. &&
E. ?:

14.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 14.4 Overloading the Shorthand Operators
14.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 return (*this).
B. You should replace this->add(secondRational) by *this = this->add(secondRational).

Section 14.5 Overloading the [] Operators
14.11  Suppose r is a Rational object. You can access _______.
A. r[0]
B. r[1]
C. r[2]
D. r[3]

14.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 14.6 Overloading the Unary Operators
14.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 14.7 Overloading the ++ and -? Operators
14.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 14.8 friend Functions and friend Classes
14.15  Which of the following statements are true?
A. Private members of a class cannot be accessed from outside of the class.
B. C++ enables you to use the friend keyword to declare friend functions and friend classes for a class so these functions and classes can access the class?s private members.

14.16  Analyze the following code:

  #include <iostream>
  using namespace std;
  
  class Date
  {
    friend void p();
  
  private:
    int year;
    int month;
    int day;
  };
  
  void p()
  {
    Date date;
    date.year = 2000;
    cout << date.year;
  }
  
  int main()
  {
    p();
    return 0;
  }
A. The program has a compile error because year is a private data field in Date.
B. The program compiles and runs fine and display 2000.
C. The program will have a compile error if the line friend void p() is deleted.
D. Since year is private, you cannot access it using date.year in function p().

Section 14.9 Overloading the << and >> Operators
14.17  If the << operator does not access the private data fields in Rational, do you still have to declare it friend?
A. Yes
B. No

14.18  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 14.10 Automatic Type Conversions
14.19  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 14.12 The Rational Class with Overloaded Function Operators
14.20  Suppose r is a Rational object, in order to perform 4 + r, the Rational class header file must contain:
A. conversion function int operator();
B. conversion constructor Rational(int numerator)
C. non-member function Rational operator+(const Rational& r1, const Rational& r2)
D. member function Rational operator+(const Rational& r1)

Section 14.13 Overloading the = Operators
14.21  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);