Test Bank for Problem Solving with C++: The Object of Programming, 6/e

Chapter 11 Friends and Overloaded Operators

TRUE/FALSE

  1. Friend functions are members of the class.

ANSWER: FALSE

  1. All operators can be overloaded.

ANSWER: FALSE

  1. If you have mutators and accessors, you should not have friend functions also

ANSWER: FALSE

  1. Friend functions may directly modify or access the private data members.

ANSWER: TRUE

  1. The following is a properly declared overloaded insertion operator for myClass.

ostream& operator <(ostream &out, const myClass &obj);

ANSWER: TRUE

  1. Functions that are constant member functions may call the class mutator functions.

ANSWER: FALSE

  1. Functions that are constant member functions may call constant class accessor functions.

ANSWER: TRUE

  1. You cannot create new operators (such as the quote).

ANSWER: TRUE

  1. Operators must be friends of the class.

ANSWER: FALSE

  1. You may not change the precedence of operators by overloading them

ANSWER: TRUE

Short Answer

  1. If a given task being performed by a function involves more than one object, then that function should normally be a ______function.

ANSWER: friend

  1. If a given task being performed by a function involves one object, then that function should normally be a ______function.

ANSWER: member

  1. A ______function is not a member of the class, but has access to the private members of the class.

ANSWER: friend

  1. An overloaded extraction or insertion operator should return ______

ANSWER: a reference to the stream

  1. A friend function needs to be passed an object of the class. If the friend only needs to access the object, but not change its data members, then the object should be passed as ______

ANSWER: a constant reference

  1. An operator that expects only one parameter is called a ______operator

ANSWER: unary

  1. An operator that expects two parameters is called a ______operator.

ANSWER: binary

  1. In order to do automatic type conversion for your class, you would write ______

ANSWER: overloaded functions or overloaded constructors

  1. Putting the keyword const after the function declaration guarantees ______

ANSWER: That the function will not change the calling object.

  1. Putting the keyword const in front of a pass by reference parameter guarantees ______

ANSWER: that the function will not modify that parameter.

Multiple Choice

  1. How many members (data and functions) does the following class have?

class Rational

{

public:

Rational();

Rational(int numer, int denom);

Rational(int whole);

int getNumerator();

int getDenominator();

friend void display(ostream& out, const Rational& value);

private:

int numerator;

int denominator;

};

  1. 2
  2. 6
  3. 5
  4. 7
  5. 8

ANSWER: D

  1. Who can access private data in a class?
  2. members of the class
  3. friends of the class
  4. everyone
  5. B and C
  6. no one

ANSWER: D

  1. Given the following class, which is the correct function header for the display function?

class Rational

{

public:

Rational();

Rational(int numer, int denom);

Rational(int whole);

int getNumerator();

int getDenominator();

friend void display(ostream& out, const Rational& value);

private:

int numerator;

int denominator;

};

  1. friend void display(ostream& out, const Rational& value)
  2. void display(ostream& out, const Rational& value)
  3. void Rational::display(ostream& out, const Rational& value)
  4. friend void Rational::display(ostream& out, const Rational& value)

ANSWER: B

  1. Operators can be overloaded as
  2. friends of a class
  3. members of a class
  4. non-friends, non-members of a class
  5. All of the above

ANSWER: D

  1. If we have a full selection of accessor and mutator functions, why would we have friend functions?
  2. You should not have them
  3. More efficient access to the private data members.
  4. The friend function must call the accessor or mutator functions anyway.
  5. none of the above

ANSWER: B

  1. Since accessors functions in a class do not modify or mutate the data members of the object, the function should have the ______modifier.
  2. reference
  3. friend
  4. const
  5. private

ANSWER: C

  1. Why should you generally pass an object of the class to a friend function as a reference parameter?
  2. If the friend function changes the values of the data member(s).
  3. If the friend function will not change the values of the data member(s).
  4. It is more efficient to pass the object by reference.
  5. A and B
  6. A and C

ANSWER: E

  1. If c is a character variable that contains a digit, what does the following function return?

int digit_to_int(char c)

{

return ( int(c) – int('0'));

}

  1. The ASCII value of c
  2. The character value of c
  3. The integer equivalent of the digit stored in c
  4. none of the above

ANSWER: C

  1. What is wrong with the following member function definition given the class below?

class Rational

{

public:

Rational();

Rational(int numer, int denom);

Rational(int whole);

int getNumerator() const;

int getDenominator() const;

friend void display(ostream& out, const Rational& value);

private:

int numerator;

int denominator;

};

int Rational::getNumerator() const

{

numerator = 0;

return numerator;

}

  1. You can not set the numerator to zero
  2. The function may not modify numerator, but it can modify denominator
  3. The function may not modify any of the private data members
  4. nothing
  5. A and B
  6. A and C

ANSWER: F

  1. Given the following class, what is syntactically wrong with the implementation of the display function?

class Rational

{

public:

Rational();

Rational(int numer, int denom);

Rational(int whole);

int getNumerator();

int getDenominator();

friend void display(ostream& out, const Rational& value);

private:

int numerator;

int denominator;

};

void display(ostream& out, const Rational& value)

{

out < value.getNumerator() < '/"<value.getDenominator();

}

  1. nothing
  2. value must be not be pass by reference
  3. The get functions are not const functions
  4. out should be pass by value

ANSWER: C

  1. To overload functions with symbolic names (like + - / <), you must use the keyword ______before the symbolic name.
  2. const
  3. operator
  4. reference
  5. void

ANSWER: B

  1. In the following code fragment, which is the calling object for the less-than operator?

string s1, s2;

if( s1 < s2 )

  1. s1
  2. s2
  3. none

ANSWER: A

  1. Given the following class declaration,

class Rational

{

public:

Rational();

Rational(int numer, int denom);

int getNumerator() const;

int getDenominator() const;

friend void display(ostream& out, const Rational& value);

friend bool operator(const Rational& left, const Rational& right);

private:

int numerator;

int denominator;

};

what must we add to the class in order for the following code to compile?

Rational myRational(2,3);

if ( 3 < myRational)

  1. We need another < operator that expects an integer as the second parameter.
  2. We need another < operator that expects an integer as the first parameter.
  3. We need a constructor that expects a ration number
  4. We need a constructor that expects an integer
  5. A or D
  6. B or D

ANSWER: F

  1. When overloading an operator, which of the following is true?
  2. One of the arguments must be an object of the class
  3. The operator can be a friend or a member of the class.
  4. The operator does not have to be a friend or a member of the class
  5. All of the above
  6. None of the above

ANSWER: D

  1. What is wrong with the following overloaded extraction operator declaration?

istream& operator >(istream& in, const myClass &object);

  1. Object should not be a pass by reference parameter
  2. Object should not be a const parameter
  3. You can not put the & on the return type
  4. nothing

ANSWER: B

  1. Which of the following would be an appropriate function declaration to add two rational numbers?
  2. void friend operator+( const Rational &left, const Rational &right);
  3. void operatator+( const Rational &left, const Rational &right);
  4. friend Rational operator+( const Rational &left, const Rational &right);
  5. Rational operator+( const Rational &left, const Rational &right);

ANSWER: D

  1. How many parameters are there in a binary operator implemented as a friend?
  2. 0
  3. 1
  4. 2
  5. as many as you need

ANSWER: C

  1. How many parameters are there in a unary operator implemented as a friend?
  2. 0
  3. 1
  4. 2
  5. as many as you need

ANSWER: B

  1. Given the following function declaration,

friend void display(const myClass& object);

which is the correct header for the definition of the function?

  1. void myClass::display(const myClass& object)
  2. void display(const myClass& object)
  3. friend void display(const myClass& object);
  4. friend void display(const myClass& object)

ANSWER: B

  1. Which of the following function declarations would be correct to overload the multiply operator for the Rational numbers class?
  2. friend Rational operator times(const Rational &left, const Rational &right);
  3. Rational operator times(const Rational &left, const Rational &right);
  4. friend Rational operator *(const Rational &left, const Rational &right);
  5. Rational operator *(const Rational &left, const Rational &right);

ANSWER: C

  1. Why are the extraction and insertion operators always implemented as friends of the class rather than as members of the class?
  2. Because the first parameter must be the stream object.
  3. They don't, they could be members
  4. Because they return a reference
  5. Because the stream is passed by reference.

ANSWER: A

  1. If you want to be able to compile the following code,

Rational r1;

int x;

cout < r1 + x < endl;

which overloaded operator(s) do you need?

  1. friend Rational operator+( const Rational& left, int right);
  2. friend void operator+ (const Rational& left, int right);
  3. friend ostream operator < (ostream& out, const Rational& object);
  4. friend ostream& operator < (ostream& out, const Rational& object);
  5. A and C
  6. A and D

ANSWER: F

  1. What member functions do you need to allow the compiler to perform automatic type conversions from a type different than the class to the class?
  2. Overloaded constructors
  3. Converters
  4. This can not be done
  5. This already happens automatically

ANSWER: A

  1. In an overloaded insertion or extraction operator, which object should be the first parameter, the stream or the object of the class?
  2. the stream
  3. the object
  4. it doesn't matter
  5. none of the above

ANSWER: A

  1. Which of the following operators can not be overloaded?
  2. =
  3. ==
  4. .
  5. []

ANSWER: C