Test 3 (Chapters 7, 8, and 9)

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

A linear search can only be implemented with integer values. (T/F)

ANS: False

Although two-dimensional arrays are a novel idea, there is no known way to pass one to a function. (T/F)

ANS: False

An array name is a pointer constant because the address stored in it cannot be changed at runtime. (T/F)

ANS: True

An individual array element can be processed like any other type of C++ variable. (T/F)

ANS: True

Assuming myValues is an array of int values and index is an int variable, both of the following statements do the same thing. (T/F) cout << myValues[index] << endl; cout << *(myValues + index) << endl;

ANS: True

C++ does not perform array bounds checking, making it possible for you to assign a pointer the address of an element out of the boundaries of an array. (T/F)

ANS: True

Each individual element of an array can be accessed by the array name and the element subscript. (T/F)

ANS: True

If an array is partially initialized, the uninitialized elements will be set to zero. (T/F)

ANS: True

If you are using the bubble sort algorithm to sort an array in descending order, the smaller values move toward the end. (T/F)

ANS: True

In C++11 you can use smart pointers to dynamically allocate memory and not worry about deleting the memory when you are finished using it. (T/F)

ANS: True

In C++11, the nullptr keyword was introduced to represent the address 0. (T/F)

ANS: True

How many elements does the following array have? int values[1000]; a. 1000 b. 999 c. 1001 d. cannot tell from the code

a. 1000

Using a linear search to find a value that is stored in the last element of an array that contains 20,000 elements, __________ elements must be compared. a. 20,000 b. only the first two c. only half d. 2,000 e. None of these

a. 20,000

What will the following code display? int numbers[] = {99, 87, 66, 55, 101}; cout << numbers[3] << endl; a. 55 b. 66 c. 101 d. 87

a. 55

Which of the following statements is not valid C++ code? a. int ptr = &num1; b. int ptr = int *num1; c. float num1 = &ptr2; d. All of these are valid e. All of these are invalid

e. All of these are invalid

Regardless of the algorithm being used, a search through an array is always performed a. from lowest to highest element b. from highest to lowest element c. beginning with the middle element d. using a binary search algorithm e. None of these

e. None of these

When you pass a pointer as an argument to a function, you must a. declare the pointer value again in the function call b. dereference the pointer value in the function prototype c. use the #include<func.ptr.h> statement d. not dereference the pointer in the function's body e. None of these

e. None of these

An array can easily be stepped through by using a a. a for loop b. a reference variable c. an array prototype d. a null value e. None of these

a. a for loop

Select all that apply. Which of the following can be used as pointers? a. array names b. numeric constants c. keywords d. None of these

a. array names

The following is the pseudocode for which type of algorithm? For maxElement = each subscript in the array, from the last to the first For index = 0 To maxElement - 1 If array[index] > array[index + 1] swap array[index] with array[index + 1] End If End For End For a. bubble sort b. binary sort c. bubble search d. selection sort e. None of these

a. bubble sort

Data that is to be sorted in ascending order is ordered a. from lowest value to highest value b. from highest value to lowest value c. with a binary search algorithm d. by identifying the middle value and going up and down from there e. None of these

a. from lowest value to highest value

A(n) __________ can be used to specify the starting values of an array. a. initialization list b. array name c. subscript d. element e. None of these

a. initialization list

If you leave out the size declarator in an array definition a. you must furnish an initialization list b. you are not required to initialize array elements c. all array elements default to zero values d. your array will contain no elements e. None of these

a. you must furnish an initialization list

Assume you have two integer variables, num1 and num2. Which of the following is the correct way to swap the values in these two variables? a. int temp = num1; num2 = num1; num1 = num2; b. int temp = num2; num2 = num1; num1 = temp; c. num1 = num2; num2 = num1; d. int temp = num1; num2 = temp; temp = num2; num1 = temp; e. None of these

b. int temp = num2; num2 = num1; num1 = temp;

The __________ and __________ operators can be used to increment or decrement a pointer variable. a. addition, subtraction b. ++, -- c. modulus, division d. All of these e. None of these

b. ++, --

What will the following code display? int numbers[4] = {99, 87}; cout << numbers[3] << endl; a. 87 b. 0 c. 99 d. garbage e. This code will not compile

b. 0

What will the following code output? int number = 22; int *var = &number; cout << *var << endl; a. the address of number b. 22 c. an asterisk followed by 22 d. an asterisk followed by the address of number

b. 22

What does the following statement do? vector<int> v(10); a. It creates a vector object and initializes all its elements to the value 10. b. It creates a vector object with a starting size of 10. c. It creates a vector object and initializes the first element with the value 10. d. It creates a vector object that can only store values of 10 or less.

b. It creates a vector object with a starting size of 10.

The following is the pseudocode for which type of algorithm? Set found to false Set position to -1 Set index to 0 While found is false and index < number of elements If list[index] is equal to search value found = true position = index End If Add 1 to index End While Return position a. linear sort b. linear search c. binary search d. selection sort e. None of these

b. linear search

The name of an array stores the __________ of the first array element. a. value b. memory address c. element number d. data type e. None of these

b. memory address

A __________ algorithm is a method of locating a specific item of information in a larger collection of data. a. sort b. search c. standard d. linear e. None of these

b. search

To help prevent memory leaks from occurring in C++11, a __________ automatically deletes a chunk of dynamically allocated memory when the memory is no longer being used. a. null pointer b. smart pointer c. dereferenced pointer d. None of these

b. smart pointer

Algorithms used to arrange random data in some order are __________ algorithms. a. standard search b. sorting c. linear d. binary search e. None of these

b. sorting

To access an array element, use the array name and the element's ___________. a. data type b. subscript c. value d. name e. None of these

b. subscript

The __________ is adequate for searching through small arrays. a. binary search b. the linear search c. unary search d. bubble sort e. None of these

b. the linear search

What will the following C++11 code display? vector<int> numbers {3, 5}; for (int val : numbers) cout << val << endl; a. 5 b. 3 c. 3 d. Nothing. This code has an error.

c. 3

When you work with a dereferenced pointer, you are actually working with a. a variable whose memory has been allocated b. a copy of the value pointed to by the pointer variable c. the actual value of the variable whose address is stored in the pointer variable d. None of these

c. the actual value of the variable whose address is stored in the pointer variable

If a variable uses more than one byte of memory, for pointer purposes its address is a. the address of the last byte of storage b. the average of all the addresses used to store that variable c. the address of the first byte of storage d. the address of the second byte of storage e. None of these

c. the address of the first byte of storage

When the less than operator (<) is used between two pointer values, the expression is testing whether a. the value pointed to by the first is less than the value pointed to by the second b. the value pointed to by the first is greater than the value pointed to by the second c. the address of the first variable comes before the address of the second variable in the computer's memory d. the first variable was declared before the second variable e. None of these

c. the address of the first variable comes before the address of the second variable in the computer's memory

Which statement correctly uses C++11 to initalize a vector of ints named n with the values 10 and 20? a. vector n<int>(10, 20); b. vector<int> n = {10, 20}; c. vector<int> n {10, 20}; d. int vector n ({10}, {20});

c. vector<int> n {10, 20};

What will the following code output? int *numbers = new int[5]; for (int i = 0; i <= 4; i++) *(numbers + i) = i; cout << numbers[2] << endl; a. five memory addresses b. 0 c. 3 d. 2 e. 1

d. 2

What is the output after the following code executes? int numerator = 5; int denominator = 25; int temp = 0; temp = numerator; numerator = denominator; denominator = temp; cout << numerator << "/" << denominator << " = " << (numerator/denominator) << endl; a. 5/25 = numerator/denominator b. 5/25 = 0 c. 5/25 = 0.2 d. 25/5 = 5 e. 25/5 = 25/5

d. 25/5 = 5

What is the last legal subscript that can be used with the following array? int values[5]; a. 0 b. 5 c. 6 d. 4 e. 1

d. 4

What does the following statement do? vector<int> v(10, 2); a. It creates a vector object and initializes the first two elements with the values 10 and 2. b. It creates a vector object with a starting size of 2 and initializes the first element with the value 10. c. It creates a vector object with a starting size of 10 and initializes the first element with the value 2. d. It creates a vector object with a starting size of 10 and initializes all the elements with the value 2.

d. It creates a vector object with a starting size of 10 and initializes all the elements with the value 2.

A pointer variable is designed to store a. any legal C++ value b. only floating-point values c. an integer d. a memory address e. None of these

d. a memory address

Subscript numbering in C++ a. can be set at runtime b. can begin with a value defined by the programmer c. varies from program to program d. begins with zero e. None of these

d. begins with zero

Which of the following statements displays the address of the variable numb? a. cout << numb; b. cout << *numb; c. cin >> &numb; d. cout << &numb; e. None of these

d. cout << &numb;

An array initialization must be all on one line. (T/F)

ANS: False

Before you can perform a bubble sort, the data must be stored in descending order. (T/F)

ANS: False

Before you can perform a selection sort, the data must be stored in ascending order. (T/F)

ANS: False

C++ limits the number of array dimensions to two. (T/F)

ANS: False

If you attempt to store data past an array's boundaries, it is guaranteed to cause a compiler error. (T/F)

ANS: False

In C++11 the range-based for loop is best used in situations where you need the element subscript for some purpose. (T/F)

ANS: False

The ampersand (&) is used to dereference a pointer variable in C++. (T/F)

ANS: False

The bubble sort is an easy way to arrange data in ascending order but it cannot arrange data in descending order. (T/F)

ANS: False

The following statement is a valid C++ definition: (T/F) double money[25.00];

ANS: False

The linear search repeatedly divides the portion of an array being searched in half. (T/F)

ANS: False

The weak_ptr can share ownership of a piece of dynamically allocated memory. (T/F)

ANS: False

To assign the contents of array2 to array1, you would use the following statement: (T/F) array1 = array2;

ANS: False

With pointer variables you can access but not modify data in other variables. (T/F)

ANS: False

You are more likely to find an item by using a binary search than by using a linear search. (T/F)

ANS: False

A pointer can be used as a function argument, giving the function access to the original argument. (T/F)

ANS: True

A selection sort and a binary search can be applied to STL vectors as well as arrays. (T/F)

ANS: True

A vector object automatically expands in size to accommodate the items stored in it. (T/F)

ANS: True

In C++11 you cannot use a range-based for loop to modify the contents of an array unless you declare the range variable as a reference variable. (T/F)

ANS: True

It is legal to subtract a pointer variable from another pointer variable. (T/F)

ANS: True

On average, an item is just as likely to be found near the beginning of an array as near the end. (T/F)

ANS: True

The amount of memory used by an array depends on the array's data type and the number of elements in the array. (T/F)

ANS: True

The number of comparisons made by a binary search is expressed in powers of two. (T/F)

ANS: True

The unique_ptr is the sole owner of a piece of dynamically allocated memory. (T/F)

ANS: True

To use any of the smart pointers in C++11 you must use the following directive in the header file: (T/F) #include <memory>

ANS: True

When you pass an array as an argument to a function, the function can modify the contents of the array. (T/F)

ANS: True

Select all that apply. Of the following, which statements have the same meaning? a. int *ptr = nullptr; b. int ptr = nullptr; c. *int ptr = nullptr; d. int* ptr = nullptr; e. int ptr* = nullptr;

a. int *ptr = nullptr; d. int* ptr = nullptr;

For example, you cannot __________ or __________ pointers. a. multiply, divide b. +=, -= c. add, subtract d. increment, decrement e. None of these

a. multiply, divide

In C++11, the __________ keyword was introduced to represent address 0. a. nullptr b. NULL c. weak_ptr d. shared_ptr e. None of these

a. nullptr

A two-dimensional array can have elements of __________ data type(s). a. one b. two c. four d. any number of e. None of these

a. one

A two-dimensional array can be viewed as a. rows and columns b. arguments and parameters c. increments and decrements d. All of these e. None of these

a. rows and columns

This vector function returns the number of elements in a vector. a. size b. num_elements c. elements d. length

a. size

A function may return a pointer but the programmer must ensure that the pointer a. still points to a valid object after the function ends b. has not been assigned an address c. was received as a parameter by the function d. has not previously been returned by another function e. None of these

a. still points to a valid object after the function ends

An array of string objects that will hold five names would be declared with which of the following statements? a. string names[5]; b. string names(5); c. string names 5; d. String[5] = names;

a. string names[5];

What will the following code output? int number = 22; int *var = &number; cout << var << endl; a. the address of number b. 22 c. an asterisk followed by 22 d. an asterisk followed by the address of number

a. the address of number

If you are using an older computer that does not support the C++11 standard, you should initialize pointers with a. the integer 0 or the value NULL b. the null terminator '\0' c. a nonzero value d. Any of these e. None of these

a. the integer 0 or the value NULL

An array can store a group of values, but the values must be a. the same data type b. integers and floating-point numbers c. integers d. constants e. None of these

a. the same data type

Assuming ptr is a pointer variable, what will the following statement output? cout << *ptr; a. the value stored in the variable whose address is contained in ptr b. the string "*ptr" c. the address of the variable whose address is stored in ptr d. the address of the variable stored in ptr e. None of these

a. the value stored in the variable whose address is contained in ptr

A pointer variable may be initialized with a. any nonzero integer value b. a valid address in the computer's memory c. an address less than zero d. any nonzero number e. None of these

b. a valid address in the computer's memory

Every byte in the computer's memory is assigned a unique a. pointer b. address c. dynamic allocation d. name e. None of these

b. address

When writing functions that accept multi-dimensional arrays as arguments, __________ must be explicitly stated in the parameter list. a. all dimensions b. all but the first dimension c. the size declarator of the first dimension d. all element values e. None of these

b. all but the first dimension

The __________, also known as the address operator, returns the memory address of a variable. a. asterisk (* ) b. ampersand ( & ) c. percent sign ( % ) d. exclamation point ( ! ) e. None of these

b. ampersand ( & )

An array with no elements is a. legal in C++ b. illegal in C++ c. automatically furnished with one element whose value is set to zero d. automatically furnished with one element, the null terminator e. None of these

b. illegal in C++

It is _________ to pass an argument to a function that contains an individual array element, such as scores[3]. a. illegal in C++11 b. legal in C++ c. not recommended by the ANSI committee d. not good programming practice e. None of these

b. legal in C++

What will the following statement output? cout << &num1; a. the value stored in the variable named num1 b. the memory address of the variable named num1 c. the number 1 d. the string &num1 e. None of these

b. the memory address of the variable named num1

An element of a two-dimensional array is referred to by a. the array name followed by the column number of the element b. the row subscript of the element followed by the column subscript of the element c. a comma followed by a semicolon d. the row subscript of the element followed by the array name e. None of these

b. the row subscript of the element followed by the column subscript of the element

Dynamic memory allocation occurs a. when a new variable is created by the compiler b. when a new variable is created at runtime c. when a pointer fails to dereference the right variable d. when a pointer is assigned an incorrect address e. None of these

b. when a new variable is created at runtime

What will the following code display? int numbers[] = {99, 87, 66, 55, 101}; for (int i = 1; i < 4; i++) cout << numbers[i] << " "; a. 99 87 66 55 101 b. 87 66 55 101 c. 87 66 55 d. Nothing. This code has an error.

c. 87 66 55

(MUST EDIT) What, if anything, is wrong with this function? void swap(int num1, int num2) { int temp = num2; num2 = num1; num1 = temp; } a. You must first initalize temp to 0 before using it. b. The variable temp should first be set to num1, not num2. c. The swap function must use reference parameters. d. The last line should be temp = num1. e. Nothing is wrong with this function.

c. The swap function must use reference parameters.

Which of the following is true about this statement: sum += *array++; a. This statement is illegal in C++. b. This statement will cause a compiler error. c. This statement assigns the dereferenced pointer's value, then increments the pointer's address. d. This statement increments the dereferenced pointer's value by one, then assign that value. e. None of these

c. This statement assigns the dereferenced pointer's value, then increments the pointer's address.

To assign the contents of one array to another, you must use a. the assignment operator with the array names b. the equality operator with the array names c. a loop to assign the elements of one array to the other array d. Any of these e. None of these

c. a loop to assign the elements of one array to the other array

Select all that apply. The contents of pointer variables may be changed with mathematical statements that perform a. multiplication b. division c. addition d. subtraction e. modulus

c. addition d. subtraction

Unlike regular variables, __________ can hold multiple values. a. constants b. named constants c. arrays d. floats e. None of these

c. arrays

The following statement __________ int *ptr = new int; a. results in a compiler error b. assigns an integer less than 32767 to the variable ptr c. assigns an address to the variable ptr d. creates a new pointer named int e. None of these

c. assigns an address to the variable ptr

The following is the pseudocode for which type of algorithm? Set first to 0 Set last to the last subscript in the array Set found to false Set position to -1 While found is not true and first is less than or equal to last Set middle to the subscript halfway between array[first] and array[last] If array[middle] equals the desired value Set found to true Set position to middle Else If array[middle] is greater than the desired value Set last to middle - 1 Else Set first to middle + 1 End If End While Return position a. linear sort b. linear search c. binary search d. selection sort e. None of these

c. binary search

A __________ search is more efficient than a __________ search. a. character, string b. integer, double c. binary, linear d. linear, binary e. None of these

c. binary, linear

An array's size declarator must be a __________ with a value greater than __________. a. number, one b. number, zero c. constant integer expression, zero d. variable, -1 e. None of these

c. constant integer expression, zero

Use the delete operator only on pointers that were a. never used b. not correctly initialized c. created with the new operator d. dereferenced inappropriately e. None of these

c. created with the new operator

Which of the following statements deletes memory that has been dynamically allocated for an array? a. int array = delete memory; b. int delete[ ]; c. delete [] array; d. new array = delete; e. None of these

c. delete [] array;

This vector function returns true if the vector has no elements. a. has_no_elements b. null_size c. empty d. is_empty

c. empty

With pointer variables you can __________ manipulate data stored in other variables. a. never b. seldom c. indirectly d. All of these e. None of these

c. indirectly

Arrays must be __________ at the time they are __________. a. sized, executed b. re-scoped, deleted c. initialized, declared d. compiled, typed e. None of these

c. initialized, declared

A(n) __________ search uses a loop to sequentially step through an array. a. binary b. unary c. linear d. relative e. None of these

c. linear

To pass an array as an argument to a function, pass the __________ of the array. a. contents b. size, expressed as an integer c. name d. value of the first element e. None of these

c. name

In the following statement, what does int mean? int *ptr = nullptr; a. The variable named *ptr will store an integer value. b. The variable named *ptr will store an asterisk and an integer value c. ptr is a pointer variable and will store the address of an integer variable. d. The variable named *ptr will store the value in nullptr. e. None of these

c. ptr is a pointer variable and will store the address of an integer variable.

After the code shown executes, which of the following statements is true? int numbers[] = {0, 1, 2, 3, 4}; int *ptr = numbers; ptr++; a. ptr will hold the address of numbers[0] b. ptr will hold the address of the second byte within the element numbers[0] c. ptr will hold the address of numbers[1] d. this code will not compile

c. ptr will hold the address of numbers[1]

Given the following declaration, where is the value 77 stored in the scores array? int scores[] = {83, 62, 77, 97, 86} a. scores[0] b. scores[1] c. scores[2] d. scores[3] e. scores[5]

c. scores[2]

The __________ sort usually performs fewer exchanges than the __________ sort. a. bubble, selection b. binary, linear c. selection, bubble d. ANSI, ASCII e. None of these

c. selection, bubble

The advantage of a linear search is its a. complexity b. efficiency c. simplicity d. speed e. None of these

c. simplicity

By using the same ___________ you can build relationships between data stored in two or more arrays. a. array name b. data types c. subscript d. arguments e. None of these

c. subscript

A two-dimensional array of characters can contain a. strings of the same length b. strings of different lengths c. uninitialized elements d. All of these e. None of these

d. All of these

What does the following code do? const int SIZE = 5; double x[SIZE]; for (int i = 2; i <= SIZE; i++) { x[i] = 0.0; } a. Each element in the array is initialized to 0.0. b. Each element in the array except the first is initialized to 0.0. c. Each element in the array except the first and last is initialized to 0.0. d. An error will occur when the code runs.

d. An error will occur when the code runs.

What does the following statement do? double *num2; a. Declares a double variable named num2 b. Declares and initializes a pointer variable named num2 c. Initializes a pointer variable named num2 d. Declares a pointer variable named num2 e. None of these

d. Declares a pointer variable named num2

When an array is sorted from highest to lowest, it is said to be in a. reverse order b. forward order c. ascending order d. descending order e. None of these

d. descending order

The individual values contained in an array are known as a. parts b. items c. constants d. elements e. None of these

d. elements

This following statement shows an example of ___________. int grades][ ] = {100, 90, 99, 80}; a. default arguments b. an illegal array declaration c. an illegal array initialization d. implicit array sizing e. None of these

d. implicit array sizing

Which of the following is a valid C++ array definition? a. int array[0]; b. float $payments[10.23]; c. int numbers[5.6]; d. int scores[25]; e. None of these

d. int scores[25];

Which of the following is a valid C++ array definition? a. int nums[0]; b. float $payments[10]; c. void numbers[5]; d. int sizes[10]; e. None of these

d. int sizes[10];

A binary search begins with the __________ element of an array. a. first b. last c. largest d. middle e. None of these

d. middle

The __________ is automatically appended to a character array when it is initialized with a string constant. a. array name b. number of elements c. value of the first element d. null terminator e. None of these

d. null terminator

This vector function removes an item from a vector. a. remove_item b. delete_item c. erase d. pop_back

d. pop_back

This vector function is used to insert an item into a vector. a. insert_item b. add_item c. store d. push_back

d. push_back

The following is the pseudocode for which type of algorithm? For start = each array subscript, from the first to the next-to-last minIndex = start minValue = array[start] For index = start + 1 To size - 1 If array[index] < minValue minValue = array[index] minIndex = index End If End For swap array[minIndex] with array[start] End For a. bubble sort b. binary sort c. bubble search d. selection sort e. None of these

d. selection sort

Array elements must __________ before a binary search can be performed. a. summed b. set to zero c. positive integers d. sorted e. None of these

d. sorted

The following statement __________ cin >> *num3; a. stores the keyboard input in the variable num3 b. stores the keyboard input into the pointer num3 c. is illegal in C++ d. stores the keyboard input into the variable pointed to by num3 e. None of these

d. stores the keyboard input into the variable pointed to by num3

The range-based for loop in C++11 is designed to work with a built-in variable known as a. the counter b. the i variable c. an iterator d. the range variable e. None of these

d. the range variable

Which of the following defines a unique_ptr named uniq that points to a dynamically allocated int? a. unique_ptr<uniq> int( new int ); b. unique_ptr<int> int( new uniq ); c. unique_ptr<uniq> uniq( new int ); d. unique_ptr<int> uniq( new int ); e. None of these

d. unique_ptr<int> uniq( new int );

Which statement correctly defines a vector object for holding integers? a. vector v<int>; b. int vector v; c. int<vector> v; d. vector<int> v;

d. vector<int> v;


Ensembles d'études connexes

Unit 1: The Emergence of America as a World Power

View Set

INR3038 WEALTH AND POWER Midterm

View Set

3102 EAQ: Chapter 26- Female GU System

View Set

The Things They Carried Ch. 20 Vocabulary

View Set

Network Authentic & Security Test 1

View Set

Chapter 25 Test, Form B (India world geo)

View Set