Final exam COSC(11,12,13,14,15)

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

This is a special function that is called whenever a new object is created and initialized with another object's data.

copy constructor

The this pointer is a special built-in pointer that is automatically passed as a hidden argument to all non-static member functions.

false

When you overload the << operator, you must also overload the >> operator.

false

You can overload the conditional operator to make it function as an unconditional operator.

false

You may overload any C++ operator, and you may use the operator function to define non-standard operators, such as @ and ^.

false

Which statement opens a file and links it to a file stream object?

file.open("c:\\filename.txt");

A(n) ___________ informs the compiler that a class will be declared later in the program.

forward declarations

This data type can be used to create files, read data from them, and write data to them

fstream

This data type can be used to create files, read data from them, and write data to them.

fstream

Data stored here disappears once the program stops running or the computer is powered down.

in RAM

A base class can be derived from another base class

true

A specialized object has all of the characteristics of the general class, plus its own characteristics

true

A static member function does not need to be called by a specific object of the class.

true

A static member variable can be used when there are no objects of the class in existence. answer choices

true

By default, when an object is assigned to another, each member of one object is copied to its counterpart in the other object.

true

It is possible to declare an entire class as a friend of another class.

true

The this pointer is automatically passed to non-static member functions of a class.

true

A good reason for overloading an operator is to enable it to:

work in its usual way, but with programmer-defined data types

This member function can be used to store binary data to a file.

write

If a base class is declared as private, how will the derived class "receive" its members: private x -? protected y -? public z -?

x inaccessible y private z private

If a base class is declared as protected, how will the derived class "receive" its members: private x -? protected y -? public z -?

x inaccessible y protected z protected

can a derived class have more than one base class?

yes

In C++, if you overload the < operator, you must also overload the > operator

false

C++ requires that a copy constructor's parameter be a(n)

reference variables

True/False: When passing a file stream object to a function, you should always pass it by reference.

True

Does aggregation establish an "is a" relationship or a "has a" relationship with another class?

"has a"

Object composition is useful for creating this type of relationship between classes.

"has a" relationship

Does inheritance establish an "is a" relationship or "has a" relationship between classes?

"is a"

To access files from a C++ program, you must use this directive:

#include <fstream>

create a new instance of cube and assign it to a rectangle base class pointer (use default constructor)

Rectangle *rectangle = new Cube();

To overload the + operator, you would write a function named:

+ operator

19) When you dereference an object pointer, use the ________. A) -> operator B) <> operator C) dot operator D) & operator E) None of these

A

This operator may be used to assign one object to another.

= operator

17) Members of a class object are accessed with the ________. A) dot operator B) cin object C) extraction operator D) stream insertion operator E) None of these

A

31) Look at the following structure declaration. struct Employee { string name; int idNum; }; In this declaration, idNum is: A) a member B) an array C) a tag D) None of these

A

14) Before a structure can be used, it must be ________. A) declared B) dereferenced C) initialized D) All of these E) None of these

A

17) A function ________ return a structure. A) may B) may not C) will always D) cannot possibly E) None of these

A

18) A structure pointer contains ________. A) the address of a structure variable B) the dereferenced address of a structure tag C) the name and address of the structure tag D) the address of a structure tag E) None of these

A

20) If a is a structure variable and p, a pointer, is a member of the structure, what will the following statement do? cout << *a.p; A) Output the dereferenced value pointed to by p. B) Result in a compiler error. C) Output the address stored in p. D) Output the value stored in a. E) None of these

A

23) A class may have this many default constructor(s). A) only one B) more than one C) a maximum of two D) any number E) None of these

A

24) A good reason to pass a structure as a constant reference is ________. A) to prevent changes to the structure members B) to ensure changes to the structure members C) to slow down the function's execution, preventing errors D) to speed up the function's modification of the structure members E) None of these

A

24) Objects in an array are accessed with ________, just like any other data type in an array. A) subscripts B) parentheses C) #include statements D) output format manipulators E) None of these

A

25) The process of object-oriented analysis can be viewed as the following steps: A) Identify objects, then define objects' attributes, behaviors, and relationships B) Define data members and member functions, then assign a class name C) Declare private and public variables, prototype functions, then write code D) Write the main() function, then determine which classes are needed E) None of these

A

5) When a structure is passed ________ to a function, its members are not copied. A) by reference B) by value C) Neither of these

A

8) Members of a(n) ________ union have names, but the union itself has no name. A) anonymous B) dereferenced C) organized D) declared E) None of these

A

27) With an enumerated data type, the enumerators are stored in memory as ________. A) strings B) integers C) characters D) doubles

B

31) What is the output of the following program? #include <iostream> using namespace std; class TestClass { public: TestClass(int x) { cout << x << endl; } TestClass() { cout << "Hello!" << endl; } }; int main() { TestClass test(77); return 0; } A) The program runs, but with no output. B) 77 C) Hello! D) The program will not compile.

B

6) Class declarations are usually stored here. A) on separate disk volumes B) in their own header files C) in .cpp files, along with function definitions D) under pseudonyms E) None of these

B

12) If an anonymous union is declared globally (outside all functions), it must be ________. A) empty B) declared static C) explicitly declared "global" D) initialized and used outside any function E) None of these

B

12) When a constructor function accepts no arguments, or does not have to accept arguments because of default arguments, it is called a(n) ________. A) empty constructor B) default constructor C) stand-alone function D) arbitrator function E) None of these

B

13) Data types that are created by the programmer are known as ________. A) variables B) abstract data types (ADT) C) functions D) parameters E) None of these

B

14) If you do not declare an access specification, the default for members of a class is ________. A) inline B) private C) public D) global E) None of these

B

16) This allows you to access structure members. A) structure access operator B) dot operator C) #include <structaccess> directive D) getmember function E) None of these

B

20) This type of member function may be called only from a function that is a member of the same class. A) public B) private C) global D) local E) None of these

B

21) A structure ________ contain members of the same data type. A) cannot B) can C) shouldn't D) None of these

B

23) If Circle is a structure, the statement: Circle *pcirc = nullptr; A) declares an empty structure variable called *pcirc B) declares a structure pointer called pcirc initialized with a null pointer C) is illegal in C++ D) initializes a null pointer with the value of the Circle pointer E) None of these

B

26) When a member function is defined outside of the class declaration, the function name must be qualified with the ________. A) class name, followed by a semicolon B) class name, followed by the scope resolution operator C) name of the first object D) private access specifier E) None of these

B

29) Look at the following declaration. enum Tree { OAK, MAPLE, PINE }; What is the value of the following relational expression? OAK > PINE A) true B) false C) This is an error. You cannot compare enumerators with relational operators.

B

5) This is used to protect important data. A) public access specifier B) private access specifier C) protect() member function D) class protection operator, @ E) None of these

B

6) Passing a structure as a constant reference parameter to a function ________. A) can potentially result in changes to the structure's members B) guarantees not to result in changes to the structure's members C) will always change the structure's members D) All of these E) None of these

B

7) This is like a structure, except all members occupy the same memory area. A) array B) union C) structure pointer D) array of pointers E) None of these

B

9) A ________ is a member function that is automatically called when a class object is ________. A) destructor, created B) constructor, created C) static function, deallocated D) utility function, declared E) None of these

B

9) If Circle is a structure tag, the statement: Circle doSomething(Circle c2) can be the header line for a function that ________. A) determines and returns the area of a circle B) takes a Circle structure as a parameter, does something, and returns a Circle structure C) operates on a constant reference to a Circle structure D) takes two Circle parameters and does something E) None of these

B

Starting Out with C++ from Control Structures to Objects, 8e (Gaddis) Chapter 11 Structured Data 11.1 Multiple Choice Questions 1) This describes only the general characteristics of an object. A) initialization B) abstraction C) detailed specification D) initiation E) None of these

B

Starting Out with C++ from Control Structures to Objects, 8e (Gaddis)Chapter 13 Introduction to Classes13.1 Multiple Choice Questions1) Objects are created from abstract data types that encapsulate ________ and ________ together. A) numbers, characters B) data, functions C) addresses, pointers D) integers, floats E) None of these

B

___________: the class which is inherited from: -Sometimes called the "Parent" class -Sometimes called the "Superclass"

Base

22) This is automatically called when an object is destroyed. A) constructor function B) specification deallocator C) destructor function D) coroner function E) None of these

C

22) Which of the following statements outputs the value of the gpa member of element 1 of the student array? A) cout << student1.gpa; B) cout << firstStudent.gpa; C) cout << student[1].gpa; D) cout << student1->gpa; E) None of these

C

27) If a local variable and a global variable have the same name within the same program, the ________ resolution operator must be used. A) variable B) ambiguity C) scope D) global E) None of these

C

30) What is the output of the following program? #include <iostream> using namespace std; class TestClass { public: TestClass(int x) { cout << x << endl; } TestClass() { cout << "Hello!" << endl; } }; int main() { TestClass test; return 0; } A) The program runs, but with no output. B) 0 C) Hello! D) The program will not compile.

C

18) Assuming that Rectangle is a class name, the statement: Rectangle *BoxPtr; A) declares an object of class Rectangle B) assigns the value of *BoxPtr to the object Rectangle C) defines a Rectangle pointer variable called BoxPtr D) is illegal in C++ E) None of these

C

19) To dereference a structure pointer, the appropriate operator is ________. A) the ampersand, & B) an asterisk, * C) the structure pointer operator, -> D) the dereference operator, <- E) None of these

C

21) The constructor function always has the same name as ________. A) the first private data member B) the first public data member C) the class D) the first object of the class E) None of these

C

26) A declaration for an enumerated type begins with the ________ key word. A) enumerated B) enum_type C) enum D) ENUM

C

29) Assume that myCar is an instance of the Car class, and that the Car class has a member function named accelerate. Which of the following is a valid call to the accelerate member function? A) Car->accelerate(); B) myCar::accelerate(); C) myCar.accelerate(); D) myCar:accelerate();

C

3) This is required after the closing brace of the structure declaration. A) square bracket B) period C) semicolon D) colon E) None of these

C

30) Look at the following structure declaration. struct Employee { string name; int idNum; }; In this declaration, Employee is: A) a member B) an array C) a tag D) None of these

C

4) Examples of access specifiers are the key words: A) near and far B) opened and closed C) private and public D) table and row E) None of these

C

4) Look at the following statement. bookList[2].publisher[3] = 't'; This statement ________. A) is illegal in C++ B) will change the publisher's name of the second book in bookList to 't' C) will store the character 't' in the fourth element of the publisher member of booklist[2] D) will ultimately result in a runtime error E) None of these

C

7) This directive is used to create an "include guard," which allows a program to be conditionally compiled. This prevents a header file from accidentally being included more than once. A) #include B) #guard C) #ifndef D) #endif E) None of these

C

8) When the body of a member function is defined inside a class declaration, it is said to be ________. A) static B) global C) inline D) conditional E) None of these

C

Write the syntax the defines a derived class constructor Cube that takes arguments from the base class constructor Rectangle (not inline)

Cube::Cube(double w, double len, double h) : Rectangle(w, len)

11) You may use a pointer to a structure as a ________. A) function parameter B) structure member C) function return type D) All of these E) None of these

D

32) What is the output of the following program? #include <iostream> using namespace std; class TestClass { private: int val; void showVal() { cout << val << endl; } public: TestClass(int x) { val = x; } }; int main() { TestClass test(77); test.showVal(); return 0; } A) The program runs, but with no output. B) 77 C) 0 D) The program will not compile.

D

10) Which of the following assigns a value to the hourlyWage member of employee[2]? A) employee[2]->hourlyWage = 50.00; B) employee2.hourlyWage = 7.50; C) hourlyWage[2].employee = 29.75 D) employee[2].hourlyWage = 100.00; E) None of these

D

11) The destructor function's return type is ________. A) tilde B) int C) float D) Nothing. Destructors have no return type. E) None of the above

D

15) In a procedural program, you typically have ________ stored in a collection of variables, and a set of ________ that perform operations on the data. A) numbers, arguments B) parameters, arguments C) strings, operators D) data, functions E) None of these

D

2) Which of the following is an example of a C++ primitive data type? A) unsigned short int B) long double C) unsigned char D) All of these E) None of these

D

25) In C++ 11 you can use a new type of enum, known as a(n) ________, (also known as an enum class) to have multiple enumerators with the same name, within the same scope. A) universal enum B) auto enum C) multi-cast enum D) strongly typed enum E) None of these

D

28) For the following code, which statement is not true? class Point { private: double y; double z; public: double x; }; A) x is available to code that is written outside the class. B) The name of the class is Point. C) x, y, and z are called members of the class. D) z is available to code that is written outside the class.

D

28) Look at the following declaration. enum Tree { OAK, MAPLE, PINE }; In memory, what value will the MAPLE enumerator be stored as? A) "MAPLE" B) 2 C) 'M' D) 1 E) 1.0

D

____________: the class inherits from the base class: -Sometimes called the "Parent" class. -Sometimes called the "Subclass"

Derived

10) The constructor function's return type is ________. A) int B) float C) char D) structure pointer E) None of these

E

15) The name of the structure is referred to as its ________. A) data type B) argument C) parameter D) tag E) None of these

E

21) Look at the following structure declaration. struct Circle { double centerX; double centerY; double radius; }; Assume that circle1 and circle2 are variables of the Circle type, and their members have been initialized. True/False: The following if statement correctly determines whether the two variables' members contain the same data: if (circle1 == circle2)

FALSE

10) True/False: It is possible to output the contents of all members of a structure variable using a cout << statement followed by the name of the structure variable.

FALSE

14) True/False: In C++, if you overload the < operator, you must also overload the > operator.

FALSE

15) True/False: A non-static member function may not access a static member variable.

FALSE

15) True/False: A union can only have one member.

FALSE

16) True/False: The names of the enumerators in an enumerated data type must be enclosed in quotation marks.

FALSE

18) True/False: You cannot directly assign an enumerator to an int variable.

FALSE

2) True/False: Class objects can be defined prior to the class declaration.

FALSE

22) The following union declaration appears on a system uses 4-byte ints and 8-byte doubles. union Numbers { int integerNumber; double doubleNumber; }; Numbers myNumber; myNumber.integerNumber = 1; True/False: After this code executes, the myNumber variable will occupy 4 bytes of memory.

FALSE

3) True/False: The constructor function may not accept arguments.

FALSE

4) True/False: A destructor function can have zero to many parameters.

FALSE

6) True/False: A function cannot modify the members of a structure.

FALSE

6) True/False: More than one destructor function may be defined for a class.

FALSE

8) True/False: You must declare all data members of a class before you declare member functions.

FALSE

9) True/False: You must use the private access specification for all data members of a class.

FALSE

True/False: By default, files are opened in binary mode

False

True/False: Only one file stream object can be declared per C++ program

False

True/False: To write to a file, you use the file_write() function.

False

True/False: When data is read from a file, it is automatically stored in a variable.

False

True/False: When you store data in a variable, it is automatically saved in a file.

False

When a class declares an entire class as its friend, the friendship status is reciprocal. That is, each class's member functions have free access to the other's private members.

False

In order, the three-step process of using a file in a C++ program involves:

Open the file, read/write/save data, close the file

(conceptual) What happens if base classes have member variables/functions with the same name?

Solutions: -Derived class redefined the multiply-defined function -Derived class invokes the member function in a particular base class using the scope resolution operator :: Without one of these solutions a compiler error will occur

10) True/False: A private member function is useful for tasks that are internal to the class, but is not directly called by statements outside the class.

TRUE

11) True/False: If you do not declare a destructor function, the compiler will furnish one automatically.

TRUE

11) True/False: It is possible for a structure variable to be a member of another structure variable.

TRUE

11.2 True/False Questions 1) True/False: A struct can contain members with varying data types.

TRUE

12) True/False: The expression *s->p; indicates that s is a structure pointer and p, which is also a pointer, is a member of the structure pointed to by s.

TRUE

12) True/False: When an object is defined without an argument list for its constructor, the compiler automatically calls the object's default constructor.

TRUE

13) True/False: It is possible for a structure to contain as a member a pointer to its own structure type.

TRUE

13) True/False: One purpose that constructor functions are often used for is to allocate memory that will be needed by the object.

TRUE

13.2 True/False Questions 1) True/False: Whereas object-oriented programming centers on the object, procedural programming centers on functions.

TRUE

14) True/False: An anonymous union declaration actually creates the member variables in memory.

TRUE

14) True/False: One purpose that destructor functions are often used for is to free memory that was allocated by the object.

TRUE

15) True/False: When using smart pointers to dynamically allocate objects in C++ 11, it is unnecessary to delete the dynamically allocated objects because the smart pointer will automatically delete them.

TRUE

16) True/False: The this pointer is automatically passed to non-static member functions of a class.

TRUE

17) True/False: You cannot directly assign an integer value to an enum variable.

TRUE

19) True/False: When you use a strongly typed enumerator in C++ 11, you must prefix the enumerator with the name of the enum, followed by the :: operator.

TRUE

2) True/False: Any mathematical operations that can be performed on regular C++ variables can be performed on structure members.

TRUE

20) True/False: In C++ 11, if you want to retrieve a strongly typed enumerator's underlying integer value, you must use a cast operator.

TRUE

3) True/False: Structure variables may be passed as arguments to functions.

TRUE

4) True/False: The structure pointer operator is used to dereference a pointer to a structure, not a pointer that is a member of a structure.

TRUE

5) True/False: More than one constructor function may be defined for a class.

TRUE

5) True/False: The expression s->m; indicates that s is a structure pointer and m is a structure member.

TRUE

7) True/False: If a function is legally prototyped to return an integer value, it can return a structure member that is an integer data type.

TRUE

7) True/False: Object-oriented programming is centered around the object, which encapsulate together both the data and the functions that operate on the data.

TRUE

8) True/False: You can define any number of union variables, but each variable can only store the value of one member at a time.

TRUE

9) True/False: When a programmer creates an abstract data type, he or she can decide what values are acceptable for the data type, as well as what operations may be performed on the data type.

TRUE

True/False: An alternative to using the open member function is to use the file stream object declaration itself to open the file. Example: fstream DataFile("names.dat", ios::in | ios::out);

True

True/False: The ios::hardfail bit is set when an unrecoverable error occurs

True

True/False: The ios::out flag causes the file's existing contents to be deleted if the file already exists.

True

When a file is opened, the file stream object's "read position" is ________.

at the beginning of the file

Which statement opens a file in such a way that information will only be written to its end?

dataFile.open("info.dat", ios::out | ios::app);

Which of the following statements opens the file info.txt for both input and output?

dataFile.open("info.txt", ios::in | ios::out);

All stream objects have ________, which indicate the condition of the stream.

error state bits

A non-static member function may not access a static member variable.

false

This state bit can be tested to see if the end of an input stream is encountered.

ios::eofbit

This member function writes a single character to a file.

put

This term means non-sequentially accessing information in a file.

random access

Closing a file causes any unsaved information still held in the file buffer to be ________.

saved to the file

13) This type of member function may be called from a statement outside the class. A) public B) private C) undeclared D) global E) None of these

A

16) A class is a(n) ________ that is defined by the programmer. A) data type B) function C) method D) attribute E) None of these

A

2) In OOP terminology, an object's member variables are often called its ________, and its member functions are sometimes referred to as its behaviors, or ________. A) values, morals B) data, activities C) attributes, activities D) attributes, methods E) None of these

D

3) A C++ class is similar to one of these. A) inline function B) header file C) library function D) structure E) None of these

D

What is true about the following statement? out.open("values.dat", ios::app);

If the file already exists, its contents are preserved and all output is written to the end of the file.

This data type can be used to create files and read information from them into memory. ifstream istream ofstream instream None of these

None of these

C++ allows you to redefine the way ____________ work when used with class objects.

Standard operators

A member function that is declared _____________ may not access any non-static data members in the class.

Static

If a member variable is declared ____________, all objects of that class have access to that variable.

Static

This type of member variable may be accessed before any objects of the class have been created.

Static

True/False: If a file already exists, you can open it with the flags ios::in | ios::out to preserve its contents.

True

_________ _________ _________ is a class that can have no objects. It serves as a basis for derived classes that may/will have objects

abstract base class

____________ refers to when a "has a" relationship is formed between classes.

aggregation

________ __________ __________ determines how members of a base class are inherited by the derived class

class access specification

It is a good idea to make a copy constructor's parameters __________ by specifying the _________ keyword in the parameter list.

constant, const

If you do not furnish one of these a default will be provided for you by the compiler. A.copy constructor B.constructor c.destructor d. All of these

d

An object of the ___________ class has: *all members defined in the child class *all members defined in the parent class

derived

An object of the derived class can use: *all public members defined in the child class *all public members defined in the parent class

derived

When an object of a derived class is destroyed, the __________ class's destructor is called first, then the _________ class's destructor

derived, base

In the following function header, FeetInches FeetInches::operator++(int) the word (int) is known as a:

dummy parameter

assume a derived class redefined a function of it's base class. when the ____________ binding is used, the function that gets used is the one that is associated with the actual object argument type (not the parameter type).

dynamic

A public data member may be declared a friend of a private function.

false

C++ permits you to overload the sizeof operator and the this pointer.

false

If you overload the prefix ++ operator, the postfix ++ operator is automatically overloaded.

false

This type of function is not a member of a class, but it has access to the private members of the class.

friend

(conceptual) why is it a good idea to make destructors virtual if the class could ever become a base class?

if a destructor is not made virtual and it becomes a base class, then when an instance of the base class is assigned to a based class pointer, then only the destructor for the base class will be called. To fix this, make the base class destructor "virtual"

_________ refers to when an "is a" relationship is formed between classes.

inheritance

This state bit is set when an attempted operation has failed

ios::failbit

Each object of a class has its own copy of the class's

member variables

Outside of a C++ program, a file is identified by its ________. Inside a C++ program, a file is identified by a(n) ________.

name, file stream object

When a class contains an instance of another class, it is known as

object composition

To set up a file to perform file I/O, you must declare:

one or more file stream objects

When you overload an operator, you cannot change the number of ________ taken by the operator.

operands

A(n) ________ operator can work with programmer-defined data types.

overloaded

When you redefine the way a standard operator works when it is used with class objects, you have ____________ the operator.

overloaded

____________: specification that prevents objects of a derived class from being treated as objects of the base class (this is the default).

private

Commonly, you will make your class data members ____________ and your class member functions ____________.

private, public

_____________ member access specification is like private, but is accessible by objects of the derived class

protected

_____________: specification that is restrictive, but does all derived classes to know the details of parents

protected

___________: specification that allows an object of a derived class to be treated as an object of the base class (not vise-versa)

public

a ________ _________ _________ is a member function of an abstract class that must be overridden in every instance of the derived class. abstract classes have at least one of these.

pure virtual function

____________ a function means defining a function in the derived class with the same name and parameter list as a function in the base class.

redefining

A reason to overload the ____________ is to write classes that have array-like behaviors.

square brackets [ ] operator

assume a derived class redefines a function of it's base class. when the default ____________ binding is used, which function that gets called depends on the type of object variable used in the function call (whether the function's parameter takes a type of the base or derived)

static

by default, does C++ engage in static binding or dynamic binding?

static

This is a special built-in pointer that is available to a class's member functions.

the "this" pointer

In C++, redefined functions are statically bound and overridden functions are dynamically bound

true

When you overload an operator, you can change the operator's original meaning to something entirely different.

true

base class constructors are called in order given in class declaration, not in order used in the class constructor

true

in order to assign a new instance of a derived class to a base class variable, you need to use a base class pointer

true

polymorphic behavior is only possible when an object is references by a reference variable or a pointer

true

a ___________ function expects to be redefined in a derived class and supports dynamic binding

virtual

what keyword do you use to ensure that a function will engage in dynamic binding (this is done in the function prototype in the base class)

virtual

(conceptual) how does a class become an abstract class?

when one or more of its member functions is a pure virtual function

If a base class is declared as public, how will the derived class "receive" its members: private x -? protected y -? public z -?

x inaccessible y protected z public

___________ provides a way to create a new class from an existing class.

Inheritance

When an object of a derived class is created, the _________ class's constructor is executed first, followed by the ____________ class's constructor

base, derived

When objects contain pointers, it is a good idea to create an explicit ________ function.

copy constructor

ofstream, ifstream, and fstream are:

data types

This data type can be used to create files and write information to them but cannot be used to read information from them.

ofstream

If you do not furnish one of these, an automatic memberwise copy will be performed when one object is assigned to another object.

overloaded assignment operator


Ensembles d'études connexes

Chapter 13: Reformations and Religious Wars Book Vocab

View Set

Biology- Cancer: Module 1 Homework

View Set

Cellular Respiration: Kreb's Cycle

View Set