C++ OOP Midterm

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

Assume that the function call operator() is overloaded for data type String in the usual sense of selecting a substring from a larger string. For a String object string1 with the character string "ABCDEFGHI", what string does string1(4, 2) return?

"EF"

Assuming that text is a variable of type string, what will be the contents of text after the statement cin text; is executed if the user types Hello World! then presses Enter?

"Hello"

Which of the following preprocessor directives does not constitute part of the preprocessor wrapper?

#include

Assuming that t is an array and tPtr is a pointer to that array, which expression refers to the address of element 3 of the array?

&t[3]

A header file is typically given the filename extension:

.h

What is the value of result after the following C++ statements execute? int a {4}; int b{12}; int c{37}; int d{51}; int result{d % a * c + a % b + a};

119

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.

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?

A constant pointer to constant data.

Which of the following statements is false?

A constructor cannot specify parameters.

A function prototype can always be omitted when:

A function is defined before it is first invoked.

An activation record will be popped off the function call stack whenever:

A function returns control to its caller.

A function that prints a string by using pointer arithmetic such as ++ptr to output each character should have a parameter that is:

A nonconstant pointer to constant data.

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 pointer cannot be assigned to:

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

Which of the following is false?

A string can be defined to store any data type.

Which of the following is false?

A subscript cannot be an expression.

Which of the following is not true of class template vector?

A vector can store only data of type int.

Which of the following tasks cannot be performed using a range-based for loop?

Accessing the element's subscript

Which of the following statements is true?

All of these are true.

Which statement about UML class diagrams is true?

All of these are true. In the UML, each class is modeled in a class diagram as a rectangle with three compartments. Correct! UML class diagrams can be used to summarize a class's attributes and operations. The top compartment contains the class name centered horizontally in boldface type and the middle compartment contains the class's attribute names, which correspond to the data members of a class.

Which of the following statements about UML class diagrams is false?

All of these are true. To distinguish a constructor from the class's operations, the UML requires that the word "constructor" be enclosed in guillemets and placed before the constructor's name. Correct! Like operations, the UML models constructors in the third compartment of a class diagram. It's customary to list constructors before other operations in the third compartment.

Assuming the following constructor is provided for class Time explicit Time(int = 0, int = 0, int = 0); which of the following is not a valid way to initialize a Time object?

All of these are valid ways to initialize a Time object.

A default constructor:

Both Is a constructor that must receive no arguments and Is the constructor generated by the compiler when no constructor is provided by the programmer.

A reference parameter:

Both Is an alias for its corresponding argument and Is declared by following the parameter's type in the function prototype by an ampersand (&)

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.

A recursive function is a function that:

Calls itself, directly or indirectly.

static member functions:

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

Constant variables:

Can be used to specify array sizes, thereby making programs more scalable.

Conversion constructors:

Can convert between user-defined types.

the delete[] operator

Can delete an entire array of objects declared using new

The inline keyword:

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

Referencing elements outside the array bounds with the [] operator:

Can result in changes to the value of an unrelated variable.

The data type bool:

Can take on values true and false.

Which statement about operator overloading is false?

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

If the line: friend class A; appears in class B, and the line: friend classB; appears in class C, then:

Class A can access private variables of class B.

A string array is commonly used for:

Command-line arguments.

The ________ creates object code and stores it on disk.

Compiler

Which of the following statements is false (assume we're referring to class Time)?

Compilers and IDEs typically invoke the linker for you after compiling your code.

The statement c += 3; is an example of what?

Compound Assignment Operator

Which of the following encompasses the other three?

Control structure.

The assignment operator (=) can be used to:

Copy data from one object to another.

To prevent class objects from being copied or assigned, you can:

Declare as private the class's copy constructor and overloaded assignment operator or Declare the class's copy constructor and overloaded assignment operator with with = delete after the parameter list.

The function prototype double mySqrt(int x);

Declares a function called mySqrt which takes an integer as an argument and returns a double.

Which of the following statements will not produce a syntax error?

Declaring an object to be const

In regards to default arguments, which of the following is false?

Default values cannot be global variables or function calls.

All of the following can cause a fatal execution-time error except:

Dereferencing a variable that is not a pointer.

Overloaded functions must have:

Different parameter lists.

Functions can:

Do all of these.

When an argument is passed-by-value, changes in the called function ________ affect the original variable's value; when an argument is passed call-by-reference, changes in the called function ________ affect the original variable's value.

Do not, do

Using square brackets ([]) to retrieve vector elements ________ perform bounds checking; using member function at to retrieve vector elements ________ perform bounds checking.

Does not, does

Specifying the order in which statements are to be executed in a computer program is called:

Program control.

An informal language that helps you develop algorithms without having to worry about the strict details of C++ language syntax is called __________.

Pseudocode

the array subscript operator [], when overloaded, cannot:

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

Comparing pointers and performing pointer arithmetic on them is meaningless unless:

They point to elements of the same array.

Which of the following lines would be the prototype for an overloaded cast operator function that converts an object of user-defined type Time into a double?

Time::operator double() const;

The unary scope resolution operator is used:

To access a global variable when a local variable of the same name is in scope.

The conventional way to distinguish between the overloaded preincrement and postincrement operators (++) is:

To make the argument list of postincrement include an int.

End-of-line comments that should be ignored by the compiler are denoted using:

Two forward slashes (//).

If the function int volume(int x = 1, int y = 1, int z = 1); is called by the expression volume(3), how many default arguments are used?

Two.

Parameterized stream manipulator setfill specifies the fill character that's 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:

Until explicitly set to a different setting.

Which of the following statements about a unique_ptr object is true?

You must explicitly delete the memory that's managed by a unique_ptr before the object goes out of scope.

Which of the following is not one of the C++ control structures?

break

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

Which of the following is a repetition structure?

do...while

To call a member function for a specific object, you specify the object's name, followed by a(n) ________, then the member function name and a set of parentheses.

dot operator

A main function can "drive" an object by calling its member functions—without knowing how the class is implemented. In this sense, main is referred to as a(n) ________ program.

driver

Which of the following is not a valid enumeration?

enum class Person {ME, YOU, ME};.

C++ is a(n) ________ programming language because you can define new class types as needed.

extensible

Assume that the array named items contains the integer values 0, 2, 4, 6 and 8. Which of the following set of statements uses the range-based for loop to display each value in items?

for (int item : items){ cout <<item << endl; }

Fill in the blank: Each time a function calls another function, an entry is pushed onto the __________. This entry, called a/an __________, contains the __________ that the called functions needs in order to return the calling function.

function-call stack; activation record; return address

Functions that are not members of a class are called ________ functions.

global

Fill in the blank: A variable name is any valid _______________ that is not a keyword.

identifier

Which of the following uses C++11's list initialization to initialize count to 0?

int count{0};

Which of the following is not a correct way to initialize a built-in array?

int n[5]{0, 7, 0, 3, 8, 2};

Converting from type ________ to type ________ will result in the loss of data.

int, char

A class's functions can throw exceptions, such as ________ to indicate invalid data.

invalid_argument

Which of the following is not included in <cmath>?

ln.

Variables are also known as:

lvalues, but can be used as rvalues.

The & operator can be applied to:

lvalues.

Which C++ code will execute the following: m = a + b * c / 10

m = (a + b * c) / 10;

Typically, you cannot call a member function of a class until you create a(n) ________ of that class.

object

C++ provides a number of features that "spruce up" the C language, but more importantly, it provides capabilities for ________ that were inspired by the Simula simulation programming language.

object-oriented programming

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

operator+

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)

Normally, constructors are:

public

What statement is used to exit a function?

return

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!()

The ________, ________ and ________ are the only three forms of control necessary.

sequence, selection, iteration

Which of the following is a parameterized stream manipulator used to format output?

setw

Which of the following gives the number of elements in the array int r[10]?

sizeof r / sizeof (int)

The Boost Libraries ________ pointers help you avoid some key errors associated with traditional pointers.

smart

You can sort an array with the Standard Library's:

sort function.

Given a built-in array of ints named values, which of the following statements would sort the array?

sort(begin(values), end(values));

Files ending in .cpp are known as ________ files.

source-code

The __________________ object enables a program to read data from the user.

std::cin

What is wrong with the following while loop? while (sum <= 1000) { sum = sum - 30; }

sum = sum - 30 should be sum = sum + 30 or else the loop may never end.

switch can be used to test:

int constants.

In what order would the following operators be evaluated? -, *, /, +, % Assume that if two operations have the same precedence, the one listed first will be evaluated first.

*, /, %, -, +

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

*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

The UML models operations by listing the operation name preceded by an access modifier. A(n) ________ indicates a public operation in the UML.

+

Which of the following can have a pointer as an operand?

++

For which of the following operators does C++ not specify the order of evaluation of its operands?

+.

A double subscripted array declared as array<array<int, 5>, 3> values; has how many elements?

15

Given that k is an integer array starting at location 2000, kPtr is a pointer to k and each integer is stored in 4 bytes of memory, what location does kPtr + 3 point to?

2012

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

24.

Which of the following is not an arithmetic operator?

=

Given the following function template template <typename T> T maximum(T value1, T value2) { if (value1 > value2) { return value1; } else { return value2; } } what would be returned by the following two function calls? maximum(2, 5); maximum(2.3, 5.2);

5 and 5.2.

Assuming the following pseudocode for the Fibonacci series, what is the value of the 5th Fibonacci number (fibonacci (5))? fibonacci(0) = 0 fibonacci(1) = 1 fibonacci(n) = fibonacci(n - 1) + fibonacci(n - 2)

5.

Consider the following code, assuming that x is an int with an initial value of 12 if(x = 6) { cout << x; } What is the output?

6

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 does not declare a 2-by-2 array and set all four of its elements to 0?

All of these initialize all four of the array elements to 0. array<array<int, 2>, 2> b; for (auto const &row : b){ for (auto &element : row){ element = 0; } } array<array<int,2>, 2> b = {0}; array<array<int,2>,2> b; b[0][0] = b[0][1] = b[1][0] = b[1][1] = 0;

Which of the following statements about regular expressions is true?

All of these statements are true.

Which of the following does counter-controlled iteration require?

All of these. A condition that tests for the final value. An initial value. An increment or decrement by which the control variable is modified each time through the loop.

Which of the following data types can be used to represent integers?

All of these. long short char

Which of the following statements initializes the unsigned int variable counter to 10?

All of these. unsigned int counter = {10}; unsigned int counter{10}; unsigned int counter = 10;

Returning references to non-const, private data:

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:

An access function.

How many times will the following loop print hello? i = 1; while (i <= 10) { cout << "hello"; }

An infinite number of times.

An error occurs if:

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

Using a while loop's counter-control variable in a calculation after the loop ends often causes a common logic error called:

An off-by-one error.

Variables defined inside a member function of a class have:

Block scope.

Linear search can be used on:

Any of these.

Consider the execution of the following for loop for (int x = 1; x < 5; increment) { cout << x + 1 << endl; } If the last value printed is 5, which of the following might have been used for increment?

Any of these. x += 1 ++x x++

Utility functions:

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

A switch statement should be used:

As a multiple-selection structure

float and double variables should be used:

As approximate representations of decimal numbers.

In order to calculate the ________ of an array of values, the array values must first be summed.

Average

Which statement is false?

Based on whether an operator is implemented as a member function or as a non-member function, the operator is used differently in expressions.

For operators overloaded as non-static member functions:

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

Which of the following data items are arranged from the smallest to the largest in the data hierarchy?

Bits, characters, fields, records, files.

Pointers may be assigned which of the following values?

Both An address. and nullptr.

To prevent class objects from being copied:

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

Which statement about exception handling is false?

Bounds checking is performed at execution time with vector member function at, and if a subscript is within the bounds of the array, the member function throws an out_of_bounds exception.

Which of the following statements is false?

By convention, variable-name identifiers begin with an uppercase letter, and every word in the name after the first word begins with a capital letter; this naming convention is known as camel case.

Which of the following is not a valid way to pass arguments to a function in C++?

By value with pointer arguments.

Today, virtually all new major operating systems are written in:

C or C++.

Which of the following is false?

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

Which of the following statements is false?

C++ fundamental types are portable.

Which of the following is true?

C++11 allows you to provide a default value for a data member when you declare it in the class declaration.

Which of the following is most closely associated with Moore's Law?

Every year or two, the capacities of computers have approximately doubled without any increase in price.

Recursion is to the base case as iteration is to what:

Failure of the loop continuation test.

Which of the following is true of function templates?

Formal type parameters act as placeholders for built-in types or user-defined types and are used to specify the types of arguments to the function, to specify the return type of the function, and to declare variables within the body of the function definition.

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.

Function template.

Every object of the same class:

Gets a copy of every member variable.

static data members of a certain class:

Have class scope.

Each standard library has a corresponding:

Header.

Which is the output of the following statements? std::cout << "Hello "; std::cout << "World";

Hello World

Which of the following statements about nested if...else statements is true?

In an if body, an inner if...else executes only if the outer if statement's condition is true.

Member access specifiers (public and private) can appear:

In any order and multiple times.

In a switch statement:

Multiple actions in a case do not need to be enclosed in braces.

An explicit constructor:

Must take exactly one argument.

Type-safe linkage is ensured by:

Name mangling.

Which of the following statements is false?

In use today are more than a trillion general-purpose computers and trillions more cellphones, smartphones and other handheld devices.

A function prototype does not have to:

Include parameter names.

A copy constructor:

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

A string array:

Is actually an array of pointers.

The conditional operator (?:):

Is the only ternary operator in C++.

Which of the statements a), b) and c) about C++14 is false?

It added several bugs from C++11.

Which of the following is false about a function to which a built-in array is being passed?

It always knows the size of the built-in array that is being passed.

Which of the following is false about the new operator and the object for which it allocates memory?

It automatically destroys the object after main is exited.

Which of the following is true of pseudocode?

It helps the programmer "think out" a program.

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

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.

Having a loop within a loop is known as:

Nesting.

Which of the following is not true of a destructor?

It releases the object's memory.

An array is not:

Made up of different data types.

When composition (one object having another object as a member) is used:

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

________ models software in terms similar to those that people use to describe real-world objects.

Object-oriented design

Which of the following statements is false (assume we're referring to class Time)?

Often a class's interface and implementation will be created and compiled by one programmer and used by a separate programmer who implements the client code that uses the class.

What does the following statement declare? int *countPtr, count;

One pointer to an int and one int variable.

A copy constructor must receive its argument by reference because:

Otherwise infinite recursion occurs.

The std::endl stream manipulator:

Outputs a newline and flushes the output buffer.

If grade has the value of 60, what will the following code display? if (grade >= 60) { cout << "Passed"; }

Passed

Consider the following function: void reverse(char *string1, const char *string2) { int stringsize{sizeof(string1)/sizeof(char)}; *(string1 + stringsize - 1) = '\0'; string1 = string1 + stringsize - 2; for (; *string2 != '\0'; string1--, string2++) { *string1 = *string2; } } What technique does the function use to refer to array elements?

Pointer/offset notation.

A client changing the values of private data members is:

Possible using public functions and references.

Recursion is memory-intensive because:

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.

What will the following program segment do? int counter{1}; do { cout << counter << " "; } while (++counter <= 10);

Print the numbers 1 through 10.

Which of the following is not one of the six logical units of a computer?

Printer.

________ involves reworking programs to make them clearer and easier to maintain while preserving their correctness and functionality.

Refactoring

Pointers cannot be used to:

Reference values directly.

Member function definitions:

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

Which of the following is not a disadvantage of default memberwise copy with objects containing pointers?

Requiring the explicit overloading of the assignment operator.

sizeof:

Returns the total number of bytes in a variable.

The prototypes of overloaded cast operator functions do not:

Specify a return type.

Which of the following is not one of the three general types of computer languages?

Spoken languages.

The OR (||) operator:

Stops evaluation upon finding one condition to be true.

Which of the following is false for pointer-based strings?

String literals are written inside of single quotes.

Which of the following statements does not overwrite a preexisting value stored in a memory location?

int a;

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

Test * const

What happens when two blocks, one nested inside of the other, both declare variables with the same identifier? (Assume that the outer block declares its variable before the opening left-brace of the inner block.)

The "outer" variable is hidden while the "inner" variable is in scope.

To implicitly overload the += operator:

The += operator cannot be overloaded implicitly.

Which of the following operators cannot be overloaded?

The . operator.

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?

The President.

________ is a graphical language that allows people who design software systems to use an industry standard notation to represent them.

The Unified Modeling Language

If a do...while structure is used:

The body of the loop will execute at least once.

To prevent modification of a built-in array's values when you pass the built-in array to a function:

The built-in array parameter can be preceded by the const qualifier.

The compiler will implicitly create a default constructor if:

The class does not define any constructors.

Call-by-reference can achieve the security of call-by-value when:

The const qualifier is used.

All of the following are true of functions except:

The definition of a function usually is visible to other functions.

Which of the following is not true of a constructor and destructor of the same class?

They both are able to have default arguments.

Which of the following is not true of pointers to functions?

They can not be assigned to other function pointers.

Which situation would require the operator to be overloaded as a non-member function?

The left operand is an int.

Which of the following is not included in a function's activation record?

The name of the function.

The argument list of a function call must match, or be consistent with, the parameter list of the called function in all of the following details, except:

The names of arguments/parameters in the list.

The linker links:

The object code with the libraries.

In a typical nested for loop (not a range-based for loop) used to process a two-dimensional array, following the end of each execution of the inner for loop:

The outer for loop increments its counter variable.

Because the postfix increment operator returns objects by value and the prefix increment operator returns objects by reference:

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?

The return type of the function.

If a variable is declared in the initialization expression of a for statement, then:

The scope of the variable is restricted to that for loop.

An uninitialized local variable contains:

The value last stored in the memory location reserved for that variable.

Which of the following statements is false?

There is no mechanism in C++ for a constructor to call another constructor in the same class.

The escape sequence for a newline is:

\n

The code: ", have a great day!"s is an example of:

a string-object literal.

A member-function call can supply ________ that help the function perform its task.

arguments

Which statement would be used to declare a 12-element integer array c?

array<int, 12> c;

Which of the following is not a correct way to initialize the array named n?

array<int, 5> n{0, 7, 0, 3, 8, 2};

Which of the following is correct when labeling cases in a switch structure?

case 1:

cin.getline(superstring, 30); is equivalent to which of the following?

cin.getline(superstring, 30, '\n');

Fill in the blank: When declaring a variable, it is good programming practice to provide a _______________ that explains the variable's purpose in the program.

comment

Given the class definition: class CreateDestroy { public: CreateDestroy() {cout << "Constructor called, ";} ~CreateDestroy() {cout << "Destructor called, ";} }; What will the following program output? int main () { CreateDestroy c1; CreateDestroy c2; return 0; }

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

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,

Which of the following is false?

continue and break statements may be embedded only within iteration statements.

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.

Which of the following is a double-selection statement?

if...else

When a compiler encounters a function parameter for a single-subscripted array of the form int a[], it converts the parameter to:

int *a

Which statement would be used to declare a 10-element integer array c?

int c[10];

Fill in the blank: The following is a function that is part of every C++ program: int _______________() {

main

Which of the following best describes the array name n in the declaration int n[10];?

n is a constant pointer to nonconstant data.

C++ Standard Library function getline, from the <string> header, reads characters up to, but not including, a(n)________ (which is discarded), then places the characters in a string.

newline

When using exception handling, place any code that might throw an exception in a:

try statement.

Each class you create becomes a new ________ you can use to declare variables and create objects.

type

Which of the following statements creates a uniform_int_distribution object for producing values in the range -10 to 20?

uniform_int_distribution<int> randomInt{-10, 20};

The return type ________ indicates that when a function completes its task, it does not return (i.e., give back) any information to its calling function.

void

Assuming that x and y are equal to 3 and 2, respectively, after the statement x -= y executes, the values of x and y will be:

x: 1; y: 2

Assuming that x is equal to 4, which of the following statements will not result in y containing the value 5 after execution?

y = x++;

In the expression n = x + rand() % y;

y is the scaling value.

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.operator+=(z)


Conjuntos de estudio relacionados

Therapeutic milieu, psychosocial assessment, group therapy Nursing

View Set

BIOL 1030 Chapter 12 Homework Questions

View Set

Practice Real Estate Final Exam 05

View Set

Introduction to the Solar System and Earth as a Planet: CH 7-8

View Set

Chapter 50 (PrepU), Meg Surg: Biliary Disorders

View Set

The Health Assistant Module Test

View Set