CS 2, Chapter 11

¡Supera tus tareas y exámenes ahora con Quizwiz!

Which of the following operators cannot be overloaded? a. the stream b. the object c. it doesn't matter d. none of the above

.

How many parameters are there in a unary operator implemented as a friend? a. 0 b. 1 c. 2 d. as many as you need

1

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

2

How many members (data and functions) does the following class have? class Rational { public: Rational (); Rational (int number, 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

7

Which of the following statements are true? a. A dynamic array can have a base type which is a class or a struct. b. A class or a struct may have a member which is a dynamic array. c. A and B d. Neither

A and B A dynamic array can have a base type which is a class or a struct. A class or a struct may have a member which is a dynamic array.

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

A and C If the friend function changes the values of a the data member(s) It is more efficient to pass the object by reference.

What is wrong with the following member function definition given the class below? class Rational { public: Rational (); Rational (int number, int denom); Rational(int whole); int getNumerator(); int getDenominator(); 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

A and C You cannot set the numerator to zero. The function may not modify any of the private data members.

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

A and D friend Rational operator+(const Rational& left, int right); friend ostream& operator <<(ostream& out, const Rational& object);

When you have a class which has a dynamic array as one of it's members, you should also include _____________ in the class. a. a copy constructor b. a default constructor c. a destructor d. the assignment operator e. all of the above f. none of the above

All of the above a copy constructor a default constructor a destructor the assignment operator

The copy constructor for a class is called a. when an object of the class is passed by value to a function. b. when an object of the class is initialized by another object of the class c. when a function returns an object of the class d. all of the above

All of the above when an object of the class is passed by value to a function. when an object of the class is initialized by another object of the class when a function returns an object of the class

Which of the following statements are true? a. Array elements may be user-defined types (structs or classes) b. If a function is expecting a variable of the base type of the array, then you can pass an element of the array to the function. c. All of the above. d. None of the above.

All of the above. Array elements may be user-defined types (structs or classes) If a function is expecting a variable of the base type of the array, then you can pass an element of the array to the function.

What happens when you define a class that used dynamic memory allocation and define a destructor but no copy constructor? a. If an object of the class is plugged in for a call-by-value parameter, when the function ends, the parameter's dynamic memory is returned to the freestore at the end of the function execution. b. When an object that was used as an argument for a call-by-value parameter goes out of scope, it will cause a run-time error. c. It is possible to modify the values in the argument in the function. d. All of the above e. None of the above

All of the above. -If an object of the class is plugged in for a call-by-value parameter, when the function ends, the parameter's dynamic memory is returned to the freestore at the end of the function execution. -When an object that was used as an argument for a call-by-value parameter goes out of scope, it will cause a run-time error. -It is possible to modify the values in the argument in the function.

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

All of the above. One of the arguments must be an object of the class. The operator can be a friend or a member of the class. The operator does not have to be a friend or a member of the class.

Who can access private data in a class? a. members of the class b. friends of the class c. everyone d. B and C

B and C Members of the class Friends of the class

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 a ___________

a constant reference.

An overloaded extraction or insertion operator should return ____________

a reference to the stream

An operator that expects two parameters is called a __________ operator.

binary

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

friend

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

friend

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

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

If given a task being performed by a function involves one object, than that function should normally be a _________ function.

member

To overload functions with symbolic names (like + - / <<), you must use the keyword ______ before the symbolic name a. const b. operator c. reference d. void

operator

In order to do automatic type conversion for your class, you would write ______________

overloaded functions or overloaded constructors

In the following code fragment, which is the calling object for the lass-than operator? string s1, s2; if ( s1 < s2) a. s1 b. s2 c. < d. none

s1

Putting the keyboard const in front of a pass by reference parameter guarantees _______

that the function will not modify that parameter

When a dynamic array with a class for a base type is declared, which constructor is called? a. the copy constructor b. the destructor c. the default constructor d. an explicit constructor

the default constructor

An operator that expects only one parameter is called a _______ operator

unary

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)

void display(const myClass& object)

Given the following class, which is the correct function header for the display function? class Rational { public: Rational (); Rational (int number, int denom); Rational(int whole); 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)

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

Which of the following are valid declarations for an assignment operator for a class named myClass? a. void friend operator = (myClass& left, const myClass& source); b. void operator = (myClass& left, const myClass& source); c. void friend operator = (const myClass& source); d. void operator = (const myClass& source);

void operator = (const myClass& source);

Given the following class declaration, class Rational { public: Rational (); Rational (int number, int denom); Rational(int whole); int getNumerator(); int getDenominator(); 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) 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

B or D We need another < operator that expects an integer as the first parameter. We need a constructor that expects an integer.

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.

Because the first parameter must be the stream object.

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

D, all of the above Friends of a class Members of a class Non-friends, non members of a class

All operators can be overloaded. T/F?

FALSE

Friend functions are members of the class. T/F?

FALSE

Functions that are constant member functions may call the class mutator functions. . T/F?

FALSE

If you have mutators and accessors, you should not have friend functions also. T/F?

FALSE

Operators must be friends of the class. T/F?

FALSE

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

More efficient access to the private data members

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

Object should not be a const parameter

What member functions do you need to allow the compiler to perform automatic type conversions from a type difference than the class to the class? a. Overloaded constructors b. Converters c. This can not be done d. This already happens automatically

Overloaded constructors

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

Rational operator +(const Rational &left, const Rational &Right);

Friend functions may directly modify or access the private data members? . T/F?

TRUE

Functions that are constant member functions may call constant class accessor functions. T/F?

TRUE

The following is a properly declared overloaded insertion operator for myClass. ostream& operator << (ostream & out, const myClass &obj); . T/F?

TRUE

You cannot create new operators (such as the quote). T/F?

TRUE

You may not change the precedence of operators by overloading them. T/F?

TRUE

Putting the keyword const after the function declaration guarantees ________

That the function will not change the calling object

Which of the following are not correct? a. The destructor of a class is not named the same as the name of the class, but preceded with a tilde b. The destructor of a class is not called when an object of the class goes out of scope c. The destructor of a class is not a member of the class d. The destructor of a class is a void function e. all of the above

The destructor of a class is a void function

Given the following class, what is syntactically wrong with the implementation of the display function? class Rational { public: Rational (); Rational (int number, 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(); } 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

The get functions are not const functions.

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')); } 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

The integer equivalent of the digit stored in c

If obj1 and obj2 are both objects of a class that uses dynamic memory allocation, but the class does not have an assignment operator, what happens if you execute the following code? obj1=obj2; a. A syntax error occurs, you can not assign one object to another object without the = operator b. A run-time error occurs, because the C++ system does not know how to do the assignment. c. The pointer(s) to the dynamically declared memory in obj2 are copied to the corresponding pointers in obj1. d. There is a complete and independent copy of all the dynamic memory from obj2 to obj1

The pointer(s) to the dynamically declared memory in obj2 are copied to the corresponding pointers in obj1

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

The stream

The destructor for a class is called a. explicitly from the main program b. when the class is instantiated c. when the object of the class goes out of scope d. Only at the end of main

When the object of the class goes out of scope

Since accessor 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

const

Given the following class and array declaration, how would you print out the age of the 10th person in the array? class personClass { public: void setAge(int newAge); void setGender( char newGender); void setSalary(float newSalary); int getAge(); char getGender(); float getSalary(); private: int age; char gender; float salary; }; personClass people[100];

cout << people[9].getAge();


Conjuntos de estudio relacionados

Real Estate Practice, Edition 9, Chapter 3 Quiz

View Set

Final Exam for Fall semester of Surgical technology

View Set

chapter 13 ITN 261 Web servers and Applications

View Set

Sports Medicine Midterm Study Guide -- Chapter 1

View Set

SPMT 217 EXAM 3 REVIEW + Ch 12 & 13

View Set

Ch 21: Lymphatic & Immune System

View Set

latin names and latin names only, if your not latin get the hell out of my quizlet bro i swear to god i will literally call my mom on you dont test me!

View Set

Project Constraints (Domain 2.0)

View Set