C++ Programming Exam 3

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Which statement about operator overloading is false? New operators can never be created. Certain overloaded operators can change the number of arguments they take. The precedence of an operator cannot be changed by overloading. Overloading cannot change how an operator works on built-in types.

Certain overloaded operators can change the number of arguments they take.

Which of the following is not true about separating a class's interface and implementation? Inline member function definitions are included in the header file. Changes in the class's interface will affect the client. Private data members are included in the header file. Changes in the class's implementation will affect the client.

Changes in the class's implementation will affect the client.

If the line: friend class A; appears in class B, and the line: friend class B; appears in class C, then: Class A is a friend of class C. Class A can access private variables of class B. Class C can call class A's private member functions. Class B can access class A's private variables.

Class A can access private variables of class B.

The assignment operator (=) can be used to: Test for equality. Copy data from one object to another. Compare two objects. Copy a class.

Copy data from one object to another.

Every object of the same class: Gets a copy of every member function and member variable. Gets a copy of every member variable. Gets a copy of every member function. Shares pointers to all member variables and member functions.

Gets a copy of every member variable.

Proxy classes are best described as an example of: Object-oriented programming (as used in the text). Structured programming. Information hiding. Utility functions.

Information hiding.

The is-a relationship represents. Composition. Inheritance. Information Hiding. A friend.

Inheritance.

Classes cannot: Be derived from other classes. Initialize data members in the class definition. Be used to model attributes and behaviors of objects. Include objects from other classes as members.

Initialize data members in the class definition.

If Americans are objects of the same class, which of the following attributes would most likely be represented by a static variable of that class? Age. The President. Place of birth. Favorite food.

The President.

To use operators on class objects, they must be overloaded, with the exception of operators _____ , _____, and _____. assignment (=), address (&), comma (,). logical AND (&&), logical OR (||), logical negation (!). equality (==), greater than (<), less than (>). extraction (>>), insertion (<<), equality (==).

assignment (=), address (&), comma (,).

Which of the following is most likely a base class of the other three? automobile. convertible. miniVan. sedan.

automobile.

A class's functions can throw exceptions, such as __________to indicate invalid data. invalid_data bad_data invalid_argument bad_argument

invalid_argument

Keyword _____ introduces an overloaded-operator function definition. overloaded operator function method

operator

The correct function name for overloading the addition (+) operator is: operator+ operator(+) operator:+ operator_+

operator+

Assume that t is an object of class Test, which has member functions a(), b(), c() and d(). If the functions a(), b() and c() all return references to an object of class Test (using the dereferenced this pointer) and function d() returns void, which of the following statements will not produce a syntax error: t.a().b().d(); a().b().t; t.d().c(); t.a().t.d();

t.a().b().d();

y and z are user-defined objects and the += operator is an overloaded member function. The operator is overloaded such that y += z adds z and y, then stores the result in y. Which of the following expressions is always equivalent to y += z? y = y operator+= z y.operator+=( z ) y = y + z y operator+=( y + z )

y.operator+=( z )

Which of the following preprocessor directives does not constitute part of the preprocessor wrapper? #define #endif #ifndef #include

#include

Inside a function definition for a member function of an object with data element x, which of the following is not equivalent to this->x: *this.x (*this).x x (* (& (*this) ) ).x

*this.x

A class-scope variable hidden by a block-scope variable can be accessed by preceding the variable name with the class name followed by: :: : . ->

::

Which of the following operators can be overloaded as a non-member function? () [] += ==

==

Which of the following statements about friend functions and friend classes is false? A class can either grant friendship to or take friendship from another class using the friend keyword. A friend declaration can appear anywhere in a class definition. A friend of a class can access all of its private data member and member functions. The friendship relationship is neither symmetric nor transitive.

A class can either grant friendship to or take friendship from another class using the friend keyword.

Which of the following is false? A string can be defined to store any data type. Class string provides bounds checking in its member function at. Class string's overloaded [] operator returns a vector element as an rvalue or an lvalue, depending on the context. An exception is thrown if the argument to string's at member function is an invalid subscript.

A string can be defined to store any data type.

Which of the following is not one of the disadvantages of using the "copy-and-paste" approach to duplicating code from one class into another class? Errors are prone to be spread around. It is time consuming. It forces the system to store many physical copies of the code, creating a code-maintenance nightmare. All of the above are disadvantages of the "copy-and-paste" approach.

All of the above are disadvantages of the "copy-and-paste" approach.

Assuming the definition, class BasePlusCommissionEmployee : public CommissionEmployee which of the following is false? The colon ( : ) in the header of the class definition indicates inheritance. The keyword public indicates the type of inheritance. All the public and protected members of class BasePlusCommissionEmployee are inherited as public and protected members, respectively, into class CommissionEmployee. CommissionEmployee is the base class and BasePlusCommissionEmployee is the derived class.

All the public and protected members of class BasePlusCommissionEmployee are inherited as public and protected members, respectively, into class CommissionEmployee.

Returning references to non-const, private data: Allows private functions to be modified. Is only dangerous if the binary scope resolution operator (::) is used in the function prototype. Allows private member variables to be modified, thus "breaking encapsulation." Results in a compiler error.

Allows private member variables to be modified, thus "breaking encapsulation."

The type of function a client would use to check the balance of a bank account would be: A utility function. A predicate function. An access function. A constructor.

An access function.

An error occurs if: A non-reference, non-const, primitive data member is initialized in the member initialization list. An object data member is not initialized in the member initialization list. An object data member does not have a default constructor. An object data member is not initialized in the member initialization list and does not have a default constructor.

An object data member is not initialized in the member initialization list and does not have a default constructor.

Base class constructors and assignment operators: Are not inherited by derived classes. Should not be called by derived class constructors and assignment operators. Can be inherited by derived classes, but generally are not. Can call derived-class constructors and assignment operators.

Are not inherited by derived classes.

Utility functions: Are private member functions that support operations of the class's other member functions. Are part of a class's interface. Are intended to be used by clients of a class. Are a type of constructor.

Are private member functions that support operations of the class's other member functions.

Which of the following statements will not produce a syntax error? Defining a const member function that modifies a data member of the object. Invoking a non-const member function on a const object. Declaring an object to be const. Declaring a constructor to be const.

Declaring an object to be const.

protected base class members cannot be accessed by: Functions that are neither friends of the base class, derived-class member functions nor friends of a derived class. friends of the base class. Functions that are not derived-class member functions. friends of derived classes.

Functions that are neither friends of the base class, derived-class member functions nor friends of a derived class.

static data members of a certain class: Can be accessed only if an object of that class exists. Cannot be changed, even by objects of the same that class. Have class scope. Can only be changed by static member functions.

Have class scope.

Member access specifiers (public and private) can appear: In any order and multiple times. In any order (public first or private first) but not multiple times. In any order and multiple times, if they have brackets separating each type. Outside a class definition.

In any order and multiple times.

A copy constructor: Is a constructor with only default arguments. Is a constructor that initializes a newly declared object to the value of an existing object of the same class. Is a constructor that takes no arguments. None of the above.

Is a constructor that initializes a newly declared object to the value of an existing object of the same class.

Which of the following is false about the new operator and the object for which it allocates memory? It calls the object's constructor. It returns a pointer. It does not require the size of the object to be explicitly specified in the new expression. It automatically destroys the object after main is exited.

It automatically destroys the object after main is exited.

An overloaded + operator takes a class object and a double as operands. For it to be commutative (i.e., a + b and b + a both work): operator+ must be a member function of the class from which the objects are instantiated. operator+ must be a non-member function. It must be overloaded twice; the operator+ function that takes the object as the left operand must be a member function, and the other operator+ function must be a global function. The + operator cannot be overloaded to be commutative.

It must be overloaded twice; the operator+ function that takes the object as the left operand must be a member function, and the other operator+ function must be a global function.

Which of the following is not true of a destructor? It performs termination housekeeping. It is called before the system reclaims the object's memory. If the programmer does not explicitly provide a destructor, the compiler creates an "empty" destructor. It releases the object's memory

It releases the object's memory

When composition (one object having another object as a member) is used: The host object is constructed first and then the member objects are placed into it. Member objects are constructed first, in the order they appear in the host constructor's initializer list. Member objects are constructed first, in the order they are declared in the host's class. Member objects are destructed last, in the order they are declared in the host's class.

Member objects are constructed first, in the order they are declared in the host's class.

To use an operator on user-defined class objects, operator overloading: Must always be used. Must always be used, with three exceptions. Must never be used, with three exceptions. Must never be used.

Must always be used, with three exceptions.

Which forms of inheritance are is-a relationships? All forms of inheritance are is-a relationships. Only public and private. Only public and protected. Only public.

Only public.

A copy constructor must receive its argument by reference because: Otherwise the constructor will only make a copy of a pointer to an object. Otherwise infinite recursion occurs. The copy of the argument passed by value has function scope. The pointer needs to know the address of the original data, not a temporary copy of it.

Otherwise infinite recursion occurs.

A client changing the values of private data members is: Only possible by calling private member functions. Possible using public functions and references. Never possible. Only possible if the private variables are not declared inside the class.

Possible using public functions and references.

Which of the following is not a good example of a hierarchy likely to be modeled by inheritance? Airplanes. Geometric shapes. Animals. Prime numbers.

Prime numbers.

Member function definitions: Always require the binary scope operator (::). Require the binary scope operator only when being defined outside of the definition of their class. Can use the binary scope operator anywhere, but become public functions. Must use the binary scope operator in their function prototype.

Require the binary scope operator only when being defined outside of the definition of their class.

When independent software vendors provide class libraries to clients, they typically give the __________ for the class's interface and the __________ for the class's implementation. Source code file, source code file. Source code file, object file. Object file, source code file. Object file, object file.

Source code file, object file.

Theoretically, clients do not need to see the _________ of classes from which they derive other classes. Header files. Source code. Object code. Interface.

Source code.

The prototypes of overloaded cast operator functions do not: Specify the type they convert to. Specify the type that is being converted. Specify a return type. Need to be defined inside the class whose objects are being converted.

Specify a return type.

The array subscript operator [], when overloaded, cannot: Be used with linked list classes. Take a float as an operand. Take multiple values inside (e.g., [4,8]). Take user-defined objects as operands.

Take multiple values inside (e.g., [4,8]).

For a non-constant member function of class Test, the this pointer has type: const Test * Test * const Test const * const Test * const

Test * const

To implicitly overload the += operator: Only the + operator needs to be overloaded. Only the = operator needs to be overloaded. Both the + and = operators need to be overloaded. The += operator cannot be overloaded implicitly.

The += operator cannot be overloaded implicitly.

Which of the following operators cannot be overloaded? The . operator. The -> operator. The & operator. The [ ] operator.

The . operator.

Which situation would require the operator to be overloaded as a non-member function? The overloaded operator is =. The left most operand must be a class object (or a reference to a class object). The left operand is an int. The operator returns a reference.

The left operand is an int.

In addition to hiding the implementation details that the ordinary method of "separating implementation from interface" would hide, using a proxy class also hides: The definition of inline functions. The definition of access functions. The definition of constructors and the destructor. The names of private data members.

The names of private data members.

Because the postfix increment operator returns objects by value and the prefix increment operator returns objects by reference: Prefix increment has slightly more overhead than postfix increment. The postfix increment operator returns the actual incremented object with its new value. Objects returned by postfix increment cannot be used in larger expressions. The postfix increment operator typically returns a temporary object that contains the original value of the object before the increment occurred.

The postfix increment operator typically returns a temporary object that contains the original value of the object before the increment occurred.

Which of the following is not true of a constructor and destructor of the same class? They both have the same name aside from the tilde (~) character. They are both usually called once per object created. They both are able to have default arguments. Both are called automatically, even if they are not explicitly defined in the class.

They both are able to have default arguments.

When should base class members be declared protected? When all clients should be able to access these members. When these members are used only by member functions of this base class. When these members should be available only to derived classes (and friends), but not to other clients. The protected access specified should never be used.

When these members should be available only to derived classes (and friends), but not to other clients.

Which statement about operator overloading is false? Operator overloading is the process of enabling C++'s operators to work with class objects. C++ overloads the addition operator (+) and the subtraction operator (-) to perform differently, depending on their context in integer, floating-point and pointer arithmetic with data of fundamental types. You can overload all C++ operators to be used with class objects. When you overload operators to be used with class objects, the compiler generates the appropriate code based on the types of the operands.

You can overload all C++ operators to be used with class objects.

To declare class subClass a privately derived class of superClass one would write: class subclass : private superClass class subclass :: private superClass class subclass < private superClass > class subclass inherits private superClass

class subclass : private superClass

Suppose you have a programmer-defined data type Data and want to overload the << operator to output your data type to the screen in the form cout << dataToPrint; and allow cascaded function calls. The first line of the function definition would be: ostream &operator<<( ostream &output, const Data &dataToPrint ) ostream operator<<( ostream &output, const Data &dataToPrint ) ostream &operator<<( const Data &dataToPrint, ostream &output ) ostream operator<<( const Data &dataToPrint, ostream &output )

ostream &operator<<( ostream &output, const Data &dataToPrint )

From most restrictive to least restrictive, the access modifiers are: protected, private, public private, protected, public private, public, protected protected, public, private

private, protected, public

When deriving a class from a protected base class, the public members of the base class become _________ and the protected members of the base class become __________? protected, private public, private protected, protected public, protected

protected, protected

Suppose the unary ! operator is an overloaded member function of class String. For a String object s, which function call is generated by the compiler when it finds the expression !s? s.operator!() s.operator!( default_value1, default_value2,...) operator!( s ) A compiler error results because no arguments are given.

s.operator!()

Which of the following is not a kind of inheritance in C++? public. private. static. protected.

static.

Which of the following is true about using inheritance in software engineering? Common attributes and behaviors should be factored out of closely related classes and placed into a base class from which the original classes can now inherit. It is best to create a huge class library to make it easy for a client to find the most appropriate class for his or her needs. A class produced through inheritance should be as large as possible to fully encompass all of the functionality it should offer. The standard C++ libraries that are shipped with C++ compilers are usually enough to accomplish anything an application might need to do.

Common attributes and behaviors should be factored out of closely related classes and placed into a base class from which the original classes can now inherit.

Suppose class A inherits from base class B. What is the order in which their constructors and destructors will be called when an object of class A is instantiated and then destroyed? B constructor, A constructor, A destructor, B destructor. B constructor, A constructor, B destructor, A destructor. A constructor, B constructor, A destructor, B destructor. A constructor, B constructor, B destructor, A destructor.

B constructor, A constructor, A destructor, B destructor.

When an object of a derived class is instantiated, the __________ constructor initializes the _________ members. Base class, base class. Derived class, base class. Base class, derived class. Derived class, public.

Base class, base class.

Select the false statement regarding inheritance. A derived class can contain more attributes and behaviors than its base class. A derived class can be the base class for other derived classes. Some derived classes can have multiple base classes. Base classes are usually more specific than derived classes.

Base classes are usually more specific than derived classes.

For operators overloaded as non-static member functions: Binary operators can have two arguments and unary operators can have one. Both binary and unary operators take one argument. Binary operators can have one argument, and unary operators cannot have any. Neither binary nor unary operators can have arguments.

Binary operators can have one argument, and unary operators cannot have any.

Variables defined inside a member function of a class have: File scope. Class scope. Block scope. Class or block scope, depending on whether the binary scope resolution operator (::) is used.

Block scope.

A default constructor: Is a constructor that must receive no arguments. Is the constructor generated by the compiler when no constructor is provided by the programmer. Does not perform any initialization. Both (a) and (b).

Both (a) and (b).

To prevent class objects from being copied: Make the overloaded assignment operator private. Make the copy constructor private. Both (a) and (b). None of the above.

Both (a) and (b).

Which of the following is false? An entire non-char array cannot be input or output at once. Two arrays cannot be meaningfully compared with equality or relational operators. Arrays cannot be assigned to one another (i.e., array1 = array2;). C++ ensures that you cannot "walk off" either end of an array.

C++ ensures that you cannot "walk off" either end of an array.

If a member function of a class already provides all or part of the functionality required by a constructor or another member function then: Call that member function from this constructor or member function. Copy and paste that member function's code into this constructor or member function. That member function is unnecessary. This constructor or member function is unnecessary.

Call that member function from this constructor or member function.

static member functions: Can use the this pointer. Can access only other static member functions and static data members. Cannot be called until an object of their class is instantiated. Can be declared const as well.

Can access only other static member functions and static data members.

Conversion constructors: Can have multiple arguments. Can convert between user-defined types. Are implicitly defined by the compiler if not explicitly written by the programmer. Cannot convert built-in types to user defined types.

Can convert between user-defined types.

The delete operator: Can terminate the program. Must be told which destructor to call when destroying an object. Can delete an entire array of objects declared using new. Is called implicitly at the end of a program.

Can delete an entire array of objects declared using new.

The conventional way to distinguish between the overloaded preincrement and postincrement operators (++) is: To assign a dummy value to preincrement. To make the argument list of postincrement include an int. To have the postincrement operator call the preincrement operator. Implicitly done by the compiler.

To make the argument list of postincrement include an int.

Parameterized stream manipulator setfill specifies the fill character that is displayed when an output is displayed in a field wider than the number of characters or digits in the output. The effect of setfill applies: Only to the current value being displayed. Only to outputs displayed in the current statement. Until explicitly set to a different setting. Until the output buffer is flushed.

Until explicitly set to a different setting.


संबंधित स्टडी सेट्स

BoatUSFoundation Chapter 1-6 questions

View Set

Means and Motives for Overseas Expansion

View Set

NUR101 - Chp. 25 - Growth & Development of the Preschool-Aged Child

View Set

Quiz 14 Late Medieval Italy Art History

View Set

Ch. 9 Security Awareness and Training

View Set