CS EXAM REVIEW

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

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

55

What will the following code display? int numbers[] = {99, 87, 66, 55, 101}; for (int i = 1; i < 4; i++) cout << numbers[i] << " ";

87 66 55

The linear search repeatedly divides the portion of an array being searched in half.

False

The weak_ptr can share ownership of a piece of dynamically allocated memory.

False

You are more likely to find an item by using a binary search than by using a linear search.

False

In C++11, the nullprt keyword was introduced to represent the address 0

True

On average, an item is just likely to be found near the beginning of an array as the near end.

True

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

True

The unique_ptr is the sole owner of a piece of dynamically allocated memory.

True

The vector object automatically expands in size to accommodate the item stored in it

True

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

True

When you pass an array as an argument to a function, the function can modify the contents of the array

True

When writing functions that accept multi-dimensional arrays as arguments, __________ must be explicitly stated in the parameter list.

all but the first dimension

A two-dimensional array of characters can contain

all of these

The __________, also known as the address operator, returns the memory address of a variable.

ampersand ( & )

What does the following code do? const int SIZE = 5; double x[SIZE]; for (int i = 2; i <= SIZE; i++) { x[i] = 0.0; }

an error will occur when the code runs

Select all that apply. Which of the following can be used as pointers?

array names

This following statement shows an example of ___________. int grades][ ] = {100, 90, 99, 80};

implicit array sizing

With pointer variables you can __________ manipulate data stored in other variables.

indirectly

A(n) ____ can be used to specify the starting values of an array

initialization list

Arrays must be _____ at the time they are _____.

initialized, declared

Select all that apply. Of the following, which statements have the same meaning?

int *ptr = nullptr; int* ptr = nullptr;

Which of the following is a valid C++ array definition?

int scores[25];

If you are using an older computer that does not support the C++11 standard, you should initialize pointers with

the integer 0 or the value NULL

What will the following statement output? cout << &num1;

the memory address of the variable named num1

The range based for loop in C++ 11 is designed to work with a built in variable known as

the range variable

An element of a two-dimensional array is referred to by

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

An array can store a group of values, but the values must be...

the same data type

The number of comparisons made by a binary search is expressed in powers of two

true

Which of the following defines a unique_ptr named uniq that points to a dynamically allocated int?

unique_ptr<int> uniq( new int );

Which statement correctly uses C++11 to initalize a vector of ints named n with the values 10 and 20?

vector<int> n {10, 20};

A function may return a pointer but the programmer must ensure that the pointer

still points to a valid object after the function ends

An array with no elements is

illegal in C++

Regardless of the algorithm being used, a search through an array is always performed

NONE of these

When you pass a pointer as an argument to a function, you must

None of these

The __________ and __________ operators can be used to increment or decrement a pointer variable

++, --

What will the following code display? int numbers[4] = {99, 87}; cout << numbers[3] << endl;

0

How many elements does the following array have? int values [1000];

1000

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;

2

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.

20,000

What will the following code output? int number = 22; int *var = &number; cout << *var << endl;

22

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;

25/5 = 5

Which of the following statements is not valid C++ code?

All of these are invalid

Unlike regular variables, ___ can hold multiple values.

Arrays

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

False

What does the following statement do? double *num2;

Declares a pointer variable named num2

A linear search can only be implemented with integer values

False

Although two dimensional array are a novel idea, there is no known way to pass one to a function

False

Assume array1 and array2 are the names of two arrays. To assign the contents of array2 to array1, you would use the following statement: array1 = array2;

False

Before you can perform a selection sort, the data must be stored in ascending order.

False

C++ limits the number of array dimension to two

False

If you attempt to store data past an array's boundaries, it is guaranteed to cause a compiler error

False

In C++ 11 the range based for loop is best used in sistuations where you need the element subscript for some purpose.

False

What does the following statement do? vector<int> v(10, 2);

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

What does the following statement do? vector<int> v(10);

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

The following function should swap the values contained in two integer variables, num1 and num2. What, if anything, is wrong with this function? void swap(int num1, int num2) { int temp = num2; num2 = num1; num1 = temp; }

The swap function must use reference parameters

Which of the following is true about this statement: sum += *array++;

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

A pointer can be used as a function argument, giving the function access to the original argument.

True

A selection sort and a binary search can be applied to STL vectors as well as arrays.

True

An array name is a pointer constant because the address stored in it cannot be changed during runtime.

True

An individual array element can be processed like any other type of C++ variable

True

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

True

An array can easily be stepped through by using a

a for loop

To assign the contents of one array to another you must use

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

A pointer variable is designed to store

a memory address

A pointer variable may be initialized with

a valid address in the computer's memory

Select all that apply. Select as many of the following options that make this sentence true: The contents of pointer variables may be changed with mathematical statements that perform

addition and subtraction

The following statement __________ int *ptr = new int;

assigns an address to the variable ptr

Subscript numbering in C++...

begins with zero

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

binary search

A __________ search is more efficient than a __________ search.

binary, linear

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

bubble sort

An array's size declarator must be a ___ with a value greater than _____>

constant integer expression, zero

Which of the following statements is not valid C++ code?

delete [] array;

When an array is sorted from highest to lowest, it is said to be in

descending order

The indivudal values contained in an array are known as

elements

This vector function returns true if the vector has no elements.

empty

An array initialization must be all on one line

false

It is ________ to pass an argument to a function that contains an individual array element, such as scores[3].

legal in c++

A(n) ___ search uses a loop to sequentially step through an array.

linear

The __________ is adequate for searching through small arrays.

linear search

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

linear search

The name of an array stores the ___ of the first array element

memory address

A binary search begins with the __________ element of an array.

middle

Not all arithmetic operations can be performed on pointers. For example, you cannot __________ or __________ pointers.

multiply, divide

To pass an array as an argument to a function, pass the ____ of the array.

name

The _____ is automatically appended to a character array when it is initialized with a string constant.

null terminator

In C++11, the __________ keyword was introduced to represent address 0.

nullptr

A two-dimensional array can have elements of __________ data type(s).

one

This vector function removes an item from a vector

pop back

In the following statement, what does int mean? int *ptr = nullptr;

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++;

ptr will hold the address of numbers[1]

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

selection sort

The ____ sort usually performs fewer exchanges than the ___ sort.

selection, bubble

The advantage of a linear search is its

simplicity

This vector function returns the number of elements in a vector.

size

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.

smart pointer

Array elements must __________ before a binary search can be performed.

sorted

Algorithms used to arrange random data in some order are __________ algorithms.

sorting

The following statement __________ cin >> *num3;

stores the keyboard input into the variable pointed to by num3

An array of string objects that will hold five names would be declared with which of the following.

string names[5];

By using the same ___ you can build relationships between data stored in two or more arrays

subscript

To access an array element, use the array name and the element's ______

subscript

When you work with a dereferenced pointer, you are actually working with

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

What will the following code output? int number = 22; int *var = &number; cout << var << endl;

the address of number

If a variable uses more than one byte of memory, for pointer purposes its address is

the address of the first byte of storage

When the less than operator (<) is used between two pointer values, the expression is testing whether

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

Dynamic memory allocation occurs

when a new variable is created by the compiler

If you leave out the size declarator in an array definition...

you must furnish an initialization list

What will the following C++11 code display? vector<int> numbers {3, 5}; for (int val : numbers) cout << val << endl;

3 5

What is the last legal subscript that can be used with the following array? int values[5];

4

The bubble sort is an easy way to arrange data in ascending order but it cannot arrange data in descending order.

False

With pointer variables you can access but not modify data in other variables.

False

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.

True

Each individual element of an array can be accessed by the array name and the element subscript

True

If an array is partially initialized, the uninitialized elements will be set to zero

True

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

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.

True

It is legal to subtract a pointer variable from another pointer variable.

True

Every byte in the computer's memory is assigned a unique

address

Which of the following statements displays the address of the variable numb?

cout << &numb;

Use the delete operator only on pointers that were

created with the new operator

Before you can perform a bubble sort, the data must be stored in descending order.

false

The following statement is a valid C++ definition: double money[25.00];

false

Data that is to be sorted in ascending order is ordered

from the lowest value to the highest value

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?

int temp = num2; num2 = num1; num1 = temp;

This vector function is used to insert an item into a vector.

push back

A two dimensional array can be viewed as

rows and columns

Given the following declaration, where is the value 77 stored in the scores array? int scores[] = {83, 62, 77, 97, 86}

scores[2]

A __________ algorithm is a method of locating a specific item of information in a larger collection of data.

search

Which of the following is valid C++ array definition?

int sizes[10];

Which statement correctly defines a vector object for holding integers?

vector<int> v;

Assuming ptr is a pointer variable, what will the following statement output? cout << *ptr;

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


Ensembles d'études connexes

Mini quiz sur vendredi ou la vie sauvage

View Set