Chapter11 - 6e

更新时间:2024-04-09 10:31:02 阅读量: 综合文库 文档下载

说明:文章内容仅供预览,部分内容可能不全。下载后的文档,内容与下面显示的完全一致。下载之前请确认下面内容是否您想要的,是否完整无缺。

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

2. All operators can be overloaded. ANSWER: FALSE

3. If you have mutators and accessors, you should not have friend functions also ANSWER: FALSE

4. Friend functions may directly modify or access the private data members. ANSWER: TRUE

5. The following is a properly declared overloaded insertion operator for myClass. ostream& operator <<(ostream &out, const myClass &obj); ANSWER: TRUE

6. Functions that are constant member functions may call the class mutator functions. ANSWER: FALSE

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

ANSWER: TRUE

8. You cannot create new operators (such as the quote). ANSWER: TRUE

9. Operators must be friends of the class. ANSWER: FALSE

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

2. If a given task being performed by a function involves one object, then that function should normally be a __________ function. ANSWER: member

3. A _________ function is not a member of the class, but has access to the private members of the class. ANSWER: friend

4. An overloaded extraction or insertion operator should return ___________ ANSWER: a reference to the stream

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

6. An operator that expects only one parameter is called a ________ operator ANSWER: unary

7. An operator that expects two parameters is called a ________ operator. ANSWER: binary

8. In order to do automatic type conversion for your class, you would write _________

ANSWER: overloaded functions or overloaded constructors

Test Bank for Problem Solving with C++: The Object of Programming, 6/e Chapter 11 Friends and Overloaded Operators

9. Putting the keyword const after the function declaration guarantees __________________________

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

10. 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; };

a. 2 b. 6 c. 5 d. 7 e. 8 ANSWER: D

2. Who can access private data in a class?

a. members of the class b. friends of the class c. everyone d. B and C e. no one ANSWER: D

3. 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);

Test Bank for Problem Solving with C++: The Object of Programming, 6/e Chapter 11 Friends and Overloaded Operators

int getNumerator(); int getDenominator();

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

int numerator; int denominator; };

a. friend void display(ostream& out, const Rational& value) b. void display(ostream& out, const Rational& value)

c. void Rational::display(ostream& out, const Rational& value)

d. friend void Rational::display(ostream& out, const Rational& value) ANSWER: B

4. Operators can be overloaded as

a. friends of a class b. members of a class

c. non-friends, non-members of a class d. All of the above ANSWER: D

5. If we have a full selection of accessor and mutator functions, why would we have friend functions?

a. You should not have them

b. More efficient access to the private data members.

c. The friend function must call the accessor or mutator functions anyway. d. none of the above ANSWER: B

6. Since accessors functions in a class do not modify or mutate the data members of the object, the function should have the __________ modifier.

a. reference b. friend c. const d. private ANSWER: C

7. Why should you generally pass an object of the class to a friend function as a reference parameter?

a. If the friend function changes the values of the data member(s).

b. If the friend function will not change the values of the data member(s). c. It is more efficient to pass the object by reference. d. A and B e. A and C ANSWER: E

8. 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'));

Test Bank for Problem Solving with C++: The Object of Programming, 6/e Chapter 11 Friends and Overloaded Operators }

a. The ASCII value of c b. The character value of c

c. The integer equivalent of the digit stored in c d. none of the above ANSWER: C

9. 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; }

a. You can not set the numerator to zero

b. The function may not modify numerator, but it can modify denominator c. The function may not modify any of the private data members d. nothing e. A and B f. A and C ANSWER: F

10. 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();

Test Bank for Problem Solving with C++: The Object of Programming, 6/e Chapter 11 Friends and Overloaded Operators

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() << '/\}

a. nothing

b. value must be not be pass by reference c. The get functions are not const functions d. out should be pass by value ANSWER: C

11. To overload functions with symbolic names (like + - / <<), you must use the keyword _______ before the symbolic name.

a. const b. operator c. reference d. void ANSWER: B

12. In the following code fragment, which is the calling object for the less-than operator? string s1, s2; if( s1 < s2 )

a. s1 b. s2 c. < d. none ANSWER: A

13. 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);

Test Bank for Problem Solving with C++: The Object of Programming, 6/e Chapter 11 Friends and Overloaded Operators

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)

a. We need another < operator that expects an integer as the second parameter.

b. We need another < operator that expects an integer as the first parameter. c. We need a constructor that expects a ration number d. We need a constructor that expects an integer e. A or D f. B or D ANSWER: F

14. When overloading an operator, which of the following is true?

a. One of the arguments must be an object of the class b. The operator can be a friend or a member of the class.

c. The operator does not have to be a friend or a member of the class d. All of the above e. None of the above ANSWER: D

15. What is wrong with the following overloaded extraction operator declaration? istream& operator >>(istream& in, const myClass &object);

a. Object should not be a pass by reference parameter b. Object should not be a const parameter c. You can not put the & on the return type d. nothing ANSWER: B

16. Which of the following would be an appropriate function declaration to add two rational numbers?

a. void friend operator+( const Rational &left, const Rational &right); b. void operatator+( const Rational &left, const Rational &right);

c. friend Rational operator+( const Rational &left, const Rational &right); d. Rational operator+( const Rational &left, const Rational &right); ANSWER: D

17. How many parameters are there in a binary operator implemented as a friend?

a. 0 b. 1 c. 2

d. as many as you need ANSWER: C

18. How many parameters are there in a unary operator implemented as a friend?

a. 0 b. 1

Test Bank for Problem Solving with C++: The Object of Programming, 6/e Chapter 11 Friends and Overloaded Operators

c. 2

d. as many as you need ANSWER: B

19. Given the following function declaration, friend void display(const myClass& object);

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

a. void myClass::display(const myClass& object) b. void display(const myClass& object)

c. friend void display(const myClass& object); d. friend void display(const myClass& object) ANSWER: B

20. Which of the following function declarations would be correct to overload the multiply operator for the Rational numbers class?

a. friend Rational operator times(const Rational &left, const Rational &right); b. Rational operator times(const Rational &left, const Rational &right); c. friend Rational operator *(const Rational &left, const Rational &right); d. Rational operator *(const Rational &left, const Rational &right); ANSWER: C

21. Why are the extraction and insertion operators always implemented as friends of the class rather than as members of the class?

a. Because the first parameter must be the stream object. b. They don't, they could be members c. Because they return a reference

d. Because the stream is passed by reference. ANSWER: A

22. 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?

a. friend Rational operator+( const Rational& left, int right); b. friend void operator+ (const Rational& left, int right);

c. friend ostream operator << (ostream& out, const Rational& object); d. friend ostream& operator << (ostream& out, const Rational& object); e. A and C f. A and D ANSWER: F

23. 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?

a. Overloaded constructors b. Converters

c. This can not be done

d. This already happens automatically ANSWER: A

Test Bank for Problem Solving with C++: The Object of Programming, 6/e Chapter 11 Friends and Overloaded Operators

24. In an overloaded insertion or extraction operator, which object should be the first parameter, the stream or the object of the class?

a. the stream b. the object

c. it doesn't matter d. none of the above ANSWER: A

25. Which of the following operators can not be overloaded?

a. = b. == c. . d. [] ANSWER: C

本文来源:https://www.bwwdw.com/article/8d7r.html

Top