CSIS 112 Final Exam

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

To use the assert function in your program, you should include the statement ____. #include <assert> #include <cassert> #include <iostream> #include <exception>

#include <cassert>

The formula to find the index of the middle element of a list is ____. (mid + last)/2 (first + last) - 2 (first + last) / 2 (first + mid ) * 2

(first + last) / 2

Three of the following expressions have the same value. Which of the following expressions has a value different from the others'? &ptr &*ptr *ptr ptr

*ptr

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

*this.x

A default constructor has how many parameters? 0 1 2 Variable number

0

The purpose of stack unwinding is to: 1. Attempt to catch exceptions that are not caught in their scope. 2. Improve catch blocks by allowing them to handle multiple exceptions. 3.Return control to the function that created the exception. 4. Aid the terminate command in shutting down the program.

1. Attempt to catch exceptions that are not caught in their scope.

Assuming the following is the beginning of the constructor definition for class BasePlusCommissionEmployee which inherits from class CommissionEmployee, BasePlusCommissionEmployee:: BasePlusCommissionEmployee ( string first, string last, string ssn, double sales, double rate, double salary ) : CommissionEmployee( first, last, ssn, sales, rate) The line following the single colon(:) 1. Invokes the CommissionEmployee constructor with arguments. 2. Causes a compiler error. 3. Is unnecessary because the CommissionEmployee constructor is called automatically. 4. Indicates composition.

1. Invokes the CommissionEmployee constructor with arguments.

Which of the following statements about polymorphism is false? 1. Polymorphism enables you to deal in specifics and let the execution-time environment concern itself with the generalities. 2. With polymorphism, new types of objects that can respond to existing messages can easily be incorporated into a system without modifying the base system. 3. With polymorphism, you can direct a variety of objects to behave in manners appropriate to those objects without even knowing their types. 4. To get polymorphic behavior among existing objects, those objects must be instantiated from classes in the same inheritance hierarchy.

1. Polymorphism enables you to deal in specifics and let the execution-time environment concern itself with the generalities.

Suppose you have written a program that inputs data from a file. If the input file does not exist when the program executes, then you should choose which option? 1. Terminate the program. 2. Include code in the program to recover from the exception. 3. Log the error and continue. 4. Include code in the header file.

1. Terminate the program.

Comparing pointers and performing pointer arithmetic on them is meaningless unless: 1. They point to elements of the same array. 2.You are trying to compare and perform pointer arithmetic on the values to which they point. 3.They point to arrays of equal size. 4. They point to arrays of the same type.

1. They point to elements of the same array.

Which of the following class definitions is correct in C++? 1. class studentType{public:void setData(string, double, int);private:string name;}; 2. class studentType{public:void setData(string, double, int);void print() const;private:string name;double gpa; } 3. class studentType{public void setData(string, double, int);private string name; }; 4. studentType class{public: void setData(string, double, int);private: string name; };

1. class studentType{public:void setData(string, double, int);private:string name;};

Classes can create new classes from existing classes. This important feature ____. 1. encourages code reuse 2. aids the separation of data and operations 3. provides public access to the internal state of an object 4. results in more software complexity

1. encourages code reuse

Consider the following class definitions: class bClass { public: void setX(int a); //Postcondition: x = a; void print() const; private: int x; }; class dClass: public bClass { public: void setXY(int a, int b); //Postcondition: x = a; y = b; void print() const; private: int y; }; Which of the following correctly sets the values of x and y? 1. void dClass::setXY(int a, int b) { bClass::setX(a); y = b; } 2. dClass::setXY(int a, int b) { x = a; y = b; } 3. void dClass::setXY(int a, int b) { x = bClass::setX(a); y = bClass::setY(b); } 4. void dClass::setXY(int a, int b) { x = bClass.setX(a); b = y; }

1. void dClass::setXY(int a, int b) { bClass::setX(a); y = b; }

What is the output of the following code? int *p; int x; x = 12; p = &x; cout << x << ", "; *p = 81; cout << *p << endl; 12, 12 12, 81 81, 12 81, 81

12, 81

The output of the statement: cout << pow(3.0, 2.0) + 5 << endl; is ____. 11.0 12.0 13.0 14.0

14.0

What is the output of the following program? #include <iostream> using namespace std; class bClass { public: void print() const; bClass(int a = 0, int b = 0); //Postcondition: x = a; y = b; private: int x; int y; }; class dClass: public bClass { public: void print() const; dClass(int a = 0, int b = 0, int c = 0); //Postcondition: x = a; y = b; z = c; private: int z; }; int main() { bClass bObject(2, 3); dClass dObject(3, 5, 8); bObject.print(); cout << endl; dObject.print(); cout << endl; return 0 ; } void bClass::print() const { cout << x << " " << y << endl; } bClass::bClass(int a, int b) { x = a; y = b; } void dClass::print() const { bClass:print(); cout << " " << z << endl; } dClass::dClass(int a, int b, int c) : bClass(a, b) { z = c; } 2 3 2 3 2 3 3 5 8 3 5 8 3 5 8 5 8 3 5 8

2 3 3 5 8

Which of the following is true about a derived class? 1. A derived class can directly access any member variable of the base class. 2. A derived class can redefine any public member function of the base class. 3. A derived class can have at most one base class. 4. A derived class can redefine any member function of the base class.

2. A derived class can redefine any public member function of the base class.

Which of the following assignments would be a compilation error? 1. Assigning the address of a base-class object to a base-class pointer. 2. Assigning the address of a base-class object to a derived-class pointer. 3. Assigning the address of a derived-class object to a base-class pointer. 4. Assigning the address of a derived-class object to a derived-class pointer.

2. Assigning the address of a base-class object to a derived-class pointer.

The inline keyword: 1. Increases function-call overhead. 2. Can reduce a function's execution time but increase program size. 3. Can decrease program size but increase the function's execution time. 4. Should be used with all frequently used functions.

2. Can reduce a function's execution time but increase program size.

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

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

Recursion is memory-intensive because: 1. Recursive functions tend to declare many local variables. 2. Previous function calls are still open when the function calls itself and the activation records of these previous calls still occupy space on the call stack. 3. Many copies of the function code are created. 4. It requires large data values.

2. Previous function calls are still open when the function calls itself and the activation records of these previous calls still occupy space on the call stack.

The function ____ returns a string containing an appropriate message. 1. where 2.what 3.when 4.log

2. what

What value does function mystery return when called with a value of 4? int mystery ( int number ) { if ( number <= 1 ) return 1; else return number * mystery( number - 1 ); } 0. 1. 4. 24.

24

A pointer cannot be assigned to: 1. Another pointer of the same type without using the cast operator. 2. A pointer to void without using the cast operator. 3. A pointer of a type other than its own type and void without using the cast operator. 4. Any other pointer by using the cast operator.

3. A pointer of a type other than its own type and void without using the cast operator.

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

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

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

3. Can delete an entire array of objects declared using new.

Which of the following options should you choose when an exception occurs in the program that analyzes an airline's ticketing transactions? 1. Terminate the program. 2. Include code in the program to recover from the exception. 3. Log the error and continue. 4. Include code in the header file.

3. Log the error and continue

Sequential search typically searches ____. 1. one quarter of the list 2. one third of the list 3. one half of the list 4. the entire list

3. one half of the list

What method should be used to pass an array to a function that does not modify the array and only looks at it using array subscript notation: 1. A nonconstant pointer to nonconstant data. 2. A nonconstant pointer to constant data. 3.A constant pointer to nonconstant data. 4. A constant pointer to constant data.

4. A constant pointer to constant data.

Select the false statement. Depending on the compiler: 1. A failed new operation can return a 0. 2. A failed new operation can throw a bad_alloc exception. 3. A failed new operation can throw an exception if the <new> header file has been included. 4. A failed new operation can automatically be caught at compile time.

4. A failed new operation can automatically be caught at compile time.

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

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

Assuming that all four of the following functions are defined, which one will be called by the function call square( 23.4 )? 1. template< typename T > T square( T num ) 2. template< typename T1, typename T2 > T1 square( T1 num1, T2 num2 ) 3. int square( int num ) 4. double square( double num)

4. double square( double num)

Which of the following statements are true? 1. Variables that are created during program execution are called static variables. 2. In C++, the member access operator arrow is >>. 3.In C++, the dot operator has a lower precedence than the dereferencing operator. 4.If p is a pointer variable, the statement p = p + 1; is valid in C++.

4.If p is a pointer variable, the statement p = p + 1; is valid in C++.

What is the value of x after the following statements execute? int x = 25; int *p; p = &x; *p = 46; nullptr 0 25 46

46

Given the following function: int next(int x) { return (x + 1); } what is the output of the following statement? cout << next(next(5)) << endl; 5 6 7 8

7

The output of the statement:cout << pow(2.0, pow(3.0, 1.0)) << endl;is ____. 6.0 7.0 8.0 9.0

8.0

In C++, the scope resolution operator is ____. : :: $ .

::

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.

A constructor can specify the return type: int. string. void. A constructor cannot specify a return type.

A constructor cannot specify a return type.

An activation record will be popped off the function call stack whenever: A function returns control to its caller. A function calls another function. A function calls itself. A function declares a local variable.

A function returns control to its caller.

A function that modifies an array by using pointer arithmetic such as ++ptr to process every value of the array should have a parameter that is: A nonconstant pointer to nonconstant data. A nonconstant pointer to constant data. A constant pointer to nonconstant data. A constant pointer to constant data.

A nonconstant pointer to nonconstant data.

catch blocks are not required to contain: Braces { }. Parentheses ( ). Some form of parameter type indication. A parameter name.

A parameter name.

When compiling a class's source code file (which does not contain a main function), the information in the class's header file is used for all of the following, except: Ensuring that the header of each member function matches its prototype. Ensuring that each member function knows about the class's data members and other member functions. Determining the correct amount of memory to allocate for each object of the class. All of these are uses that the compiler has for the header file information.

All of these are uses that the complier has for the header file information

Exception handling should not be used: As an alternative for program control. To make error handling uniform on large projects. To deal with errors that do not arise very often. To deal with errors for components that will be widely used in other applications, such as classes and libraries.

As an alternative for program control.

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.

To prevent class objects from being copied: Make the overloaded assignment operator private. Make the copy constructor private. Both Make the overloaded assignment operator private and Make the copy constructor private. None of these.

Both Make the overloaded assignment operator private and Make the copy constructor private.

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.

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.

In regards to default arguments, which of the following is false? When an argument is omitted in a function call, the default value of that argument is automatically inserted by the compiler and passed in the function call. They must be the rightmost (trailing) arguments in a function's parameter list. Default values can be constants. Default values cannot be global variables or function calls.

Default values cannot be global variables or function calls.

All of the following can cause a fatal execution-time error except: Dereferencing a pointer that has not been assigned to point to a specific address. Dereferencing a pointer that has not been initialized properly. Dereferencing a null pointer. Dereferencing a pointer that points to a variable that holds a value of 0.

Dereferencing a pointer that points to a variable that holds a value of 0.

Overloaded functions must have: Different parameter lists. Different return types. The same number of parameters. The same number of default arguments.

Different parameter lists.

If a set of functions have the same program logic and operations and differ only in the data type(s) each receives as argument(s) then a(n) ________ should be used. Overloaded function Recursive function Macro Function template

Function template

Which of the following statements is false? A pointer variable is a variable whose content is a memory address. In C++, pointer variables are declared using the reserved word pointer. The dereferencing operator is also known as the indirection operator and refers to the object to which its operand points. A memory leak is an unused memory space that cannot be allocated.

In C++, pointer variables are declared using the reserved word pointer.

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 the function that created it is exited.

It automatically destroys the object after the function that created it is exited.

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 a client code programmer uses a class whose implementation is in a separate file from its interface, that implementation code is merged with the client's code during the: Programming phase. Compiling phase. Linking phase. Executing phase.

Linking phase.

For a class template, the scope resolution operator ( ::) is needed: Only in the definitions of the member functions defined outside the class. Both in the prototype and definition of a member function. Only if multiple class-template specializations will be created from this class template. In neither the definition nor prototype of member functions.

Only in the definitions of the member functions defined outside the class.

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.

The main difference between a pure virtual function and a virtual function is: The return type. The member access specifier. That a pure virtual function cannot have an implementation. The location in the class.

That a pure virtual function cannot have an implementation.

Which statement about exception handling is false? Call the exception object's what member function to get the error message that's stored in the exception object. Bounds checking is performed at execution time with vector member function at, and if a subscript is outside of the bounds of the vector, the member function throws an out_of_bounds exception. The catch block contains the code that handles an exception if one occurs. None of the above statements is false.

The catch block contains the code that handles an exception if one occurs.

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 does the C++ compiler not examine in order to select the proper overloaded function to call? Types and order of the arguments in the function call. The number of arguments in the function call. The return type of the function. It examines All of these.

The return type of the function.

A function prototype is ____. a definition, but not a declaration a declaration and a definition a declaration, but not a definition a comment line

a declaration, but not a definition

A member function of a class that only accesses the value(s) of the data member(s) is called a(n) ____ function. accessor mutator constructor destructor

accessor

The function ____ can check whether an expression meets the required conditions; if the conditions are not met, it terminates the program. check look assert what

assert

Which of the following is a valid C++ statement? assert(0 = divisor); assert(divisor != 0); assert(divisor 0); assert(divisor is 0);

assert(divisor != 0);

class rectangleType { public: void setLengthWidth(double x, double y); //Postcondition: length = x; width = y; void print() const; //Output length and width; double area(); //Calculate and return the area of the rectangle; double perimeter(); //Calculate and return the parameter; rectangleType(); //Postcondition: length = 0; width = 0; rectangleType(double x, double y); //Postcondition: length = x; width = y; private: double length; double width; }; Consider the above class definition and the following object declaration: rectangleType bigRect(14,10); Which of the following statements is correct? bigRect.setLengthWidth(); bigRect.setLengthWidth(3.0, 2.0); bigRect.length = 2.0; bigRect.length = bigRect.width;

bigRect.setLengthWidth(3.0, 2.0);

The try block is followed by one or more ____ blocks. throw finally do catch

catch

Which of the following blocks is designed to catch any type of exception? catch(){ } catch(...){ } catch(*){ } catch(exception){ }

catch(...){ }

If the derived class classD overrides a public member function functionName of the base class classB, then to specify a call to that public member function of the base class, you use the statement classD::functionName(); classB::functionName(); classD.functionName(); classB.functionName();

classB::functionName();

Given the class definition: class CreateDestroy { public: CreateDestroy() {cout << "constructor called."; } ~CreateDestroy() { cout << "destructor called."; } }; What will the following program output? int main() { for ( int i = 1; i <= 2; ++i ) CreateDestroy cd; return 0; } constructor called, destructor called, constructor called, destructor called, constructor called, constructor called, constructor called, constructor called, destructor called, destructor called, Nothing.

constructor called, destructor called, constructor called, destructor called,

To guarantee that the member variables of a class are initialized, you use ____. accessors mutators constructors destructors

constructors

The code fragment:Increment::Increment( int c, int i ) : increment ( i ) { count = c; } does not cause any compilation errors. This tells you that: count must be a non- const variable. count must be a const variable. increment must be a non- const variable. increment must be a const variable.

count must be a non- const variable.

The new classes that we create from existing classes are called ____ classes. sibling base derived parent

derived

The class ____ is the base of the classes designed to handle exceptions. class exception logic_error runtime_error

exception

Given the declaration int *a;, the statement a = new int[50]; dynamically allocates an array of 50 components of the type ____. int int* pointer address

int

Given the following function prototype: ​int test(float, char); ​which of the following statements is valid? cout << test(12, &); cout << test("12.0", '&'); int u = test(5.0, '*'); cout << test('12', '&');

int u = test(5.0, '*');

Inheritance is an example of a(n) ____ relationship. is-a has-a handshaking had-a

is-a

In a bubble sort for list of length n, the first step is to compare elements ____. list[0] and list[n] list[0] and list[n-1] list[0] and list[1] list[n-1] and list[n+1]

list[0] and list[1]

Consider the following statement:ptrMemberVarType objectThree(objectOne);The values of the member variables of objectOne are being copied into the corresponding member variables of objectThree. This initialization is called the ____. member-wise assignment default assignment member-wise initialization default initialization

member-wise initialization

For sorting a list of length n, bubble sort uses ____ iterations. 1 n-1 n n+1

n-1

The C++ operator ____ is used to create dynamic variables. dynamic new virtual dereferencing

new

A catch block can have, at most, ____ catch block parameter(s). zero one two three

one

How many destructors can a class have? no explicit destructors one two any number

one

If a member of a class is ____, you cannot access it outside the class. public automatic private static

private

class rectangleType { public: void setLengthWidth(double x, double y); //Postcondition: length = x; width = y; void print() const; //Output length and width; double area(); //Calculate and return the area of the rectangle; double perimeter(); //Calculate and return the parameter; rectangleType(); //Postcondition: length = 0; width = 0; rectangleType(double x, double y); //Postcondition: length = x; width = y; private: double length; double width; }; Consider the above class definition. Which of the following object declarations is correct? rectangle rectangleType; class rectangleType rectangle; rectangleType rectangle; rectangle rectangleType.area;

rectangleType rectangle;

The class ____ is designed to deal with errors that can be detected only during program execution. error exception runtime_error logic_error

runtime_error

The code shown represents the ____________________ search algorithm. int unknownSearch(const int list[], int listLength, int searchItem) { int loc; bool found = false; loc = 0; while (loc < listLength && !found) if (list[loc] == searchItem) found = true; else loc++; if (found) return loc; else return -1; } binary sequential bubble insertion

sequential

In a ____ copy, two or more pointers of the same type point to the same memory. static shallow dynamic deep

shallow

A class object can be ____. That is, it can be created once, when the control reaches its declaration, and destroyed when the program terminates. static automatic local public

static

Which of the following correctly copies the contents of string2 into string1? Assume that string2 is equal to "goodbye" and string1 is equal to "good morning"? strcpy( string1, string2 ); strcpy( string1, string2, 6 ); Strncpy( string1, string2, 5 ); strncpy( string1, string2, 6 );

strcpy( string1, string2 );

When an exception is thrown, if the program does not handle the exception, then the function ____ is called to terminate the program. log what terminate close

terminate

The statements that may generate an exception are placed in a ____ block. throw finally try catch

try

When using exception handling, place any code that might throw an exception in a _________________. catch block try statement throw block what statement

try statement

The correct order in which an exception is detected and handled is: try, catch, throw throw, catch, try catch, throw, try try, throw, catch

try, throw, catch

class secretType { public: static int count; static int z; secretType(); secretType(int a); void print(); static void incrementY(); private: int x; static int y; }; secretType::secretType() { x = 1; } secretType::secretType(int a) { x = a; } void secretType::print() { cout << "x = " << x << ", y = " << y << "z = " << z << ", count = " << count << endl; } static void secretType::incrementY() { y++; } Consider the accompanying class and member functions definitions. How many constructors are present in the class definition? none one two three

two

Which statement declares intList to be an empty vector? vector intList(); vector<int> intList(0); vector<int> intList(10); vector<int> intList;

vector<int> intList;

If a function needs to return more than one value, as a rule of good programming style, you should change it to a(n) ____________________ function and use the appropriate reference parameters to return the values. int array void reference

void

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)


Conjuntos de estudio relacionados

Communication and Therapeutic Relationships (Exam 1_Davis Advantage)

View Set

Level 9 Texas Real Estate License Act

View Set

Chapter 2: Financial Aspects of Career Planning

View Set

Data Science - Big Data Assessment

View Set

Marketing Cloud Email Certification Salesforce (144) - Cody's Answers (unverified)

View Set

Advanced Molecular Cell Biology CH 3

View Set

Chapter 5 - Means of Egress Quiz

View Set

Fundamentals Exam 4 Ch 28 & 50; class notes and practice questions

View Set