Computer Science I - Final Exam

It is the search algorithm that repeatedly divides the portion of an array being searched in half.

binary search?

This is the maximum number of comparisons that a binary search function will make when searching for a value in a 1,000-element array.

10

It is the average number of comparisons performed by the linear search on an array of N elements.

N/2

This is how the contents of an array can be reordered to improve the average performance of the linear search when it is known that some items are searched for more frequently than others.

To store the items frequently searched for to near the beginning of the array

It is the number of inversion the bubble sort will yield on the following arrangement: 1 2 3 4 5 7 0

6

This is what is output from the following statement: char ch = toupper(tolower('A')); cout << ch;

A

It is what the following program segment will display? char dog[] = "Fido"; cout << strlen(dog) << endl;

4

Using the atof c-string function, we may return either of these types.

either a double or a float

We can use this string class member function to place copies of characters, such as a 'b', into our string object:

append member function

It is the statement that we would write to compare one string object (str1), to another (str2), to find if they were equal.

if str1 == str2;

It is the output produced by the following code? string s1, s2("Hello"); s1 = s2; s2[0] = 'J'; cout << s1 << " " << s2;.

hello jello

It is the output when A string is a joy forever! was entered by the user for the following code: string s; cout << "Enter a line of input:\n"; getline(cin, s); cout << s << "<END OF OUTPUT";

A string is a joy forever!<END OF OUTPUT

This is what will output from the following: cout << static_cast<char>(toupper('a'));

A

This is what will be the next line of output from the code below if a b c d e f g has been entered char c1, c2, c3, c4; cout << "Enter a line of input:\n"; cin.get(c1); cin.get(c2); cin.get(c3); cin.get(c4); cout << c1 << c2 << c3 << c4 << "END OF OUTP

a b END OF OUTPUT

It is the declaration that is not equivalent to any of the others:
char stringVar[10] = "Hello";
char stringVar[10] = {'H', 'e', 'l', 'l', 'o', '\0'};
char stringVar[10] = {'H', 'e', 'l', 'l', 'o'};
char stringVar[6] = "Hello";
char stringVar[] = "Hello";

char stringVar[10] = {'H', 'e', 'l';, 'l', 'o'};

Supposing that s1 , s2 , and s3 are objects of type string, and both s1 and s2 have string values, it is the value of s3 after the following statement executes: s3 = s1 + s2;

What is the concatenated s1 and s2 strings

This dereferences a pointer, allowing code to work with the value that the pointer points to.

What is indirection operator or the dereference operator *

This is what we use to assign the address of a variable to a pointer

a &, the address of operator

It is the cause of the error with the following code: float myFloat; int *pint = &myFloat;

a type mismatch

It is what is used when passing variables to a targeted pointer parameter in a function.

the &, or address of

It is what is required when inputting information in a function that uses pointer parameter (to store the keyboard input into the variable pointed to in the cin statement)

the dereference operator, such as cin >> *ptrVar;

It is what the following statement does using pointer notation: *(ptrToArray + 17) = 199;

store 199 in the 18th element of the array where ptrToArray is pointing to

It is what the following code is: const int *numbers;

a pointer to a constant

It is the error in the following code: int
pint = nullptr; pint = new int; if (pint == nullptr)
pint = 100; else cout << "Memory allocation error\n";

the program segment is storing a value at the address 0.

It is how cout statement that uses the ptr variable to display the contents of the value variable would be coded as for the following : double value = 29.7; double *ptr = &value;

cout << *ptr ;

Given: const int numbers[SIZE] = { 18, 17, 12, 14 }; and supposing we want to pass the array to the function processArray as such: processArray(numbers, SIZE); It is the function header for the processArray function.

void processArray(const int *arr, int size)

If we want a search function to search an array for some value and return either the index where the value was found, or -1 if not found, which of the following prototypes would be appropriate?

int search(const int array[], int target, int numElements);

To declare a c-string and initialize it to the value of "phonebook",

char s1[10]="phonebook";

Which of the following correctly declares a dynamic array of strings?

p1 = new string[13];

The notation vector<Base_Type> means that the vector is

a template class.

When the extraction operator is used to read data into a string

it skips all white spaces

Assuming that the pointer variable p1 is of the correct type and size is an integer with some value > 1, which of the following declarations are legal?

p1 = new string[size];, p1 = new ifstream[size];, and p1 = new char[size*size];

Which of the following function declarations correctly expect an array as the first argument?

void f1(int array[100], int size); and void f1(float array[], int size);

Which of the following correctly uses C++11's range-based for statement to iterate through every element of the array variable arr?

for (auto x : arr)

What is the proper way to declare a vector of strings named names?

vector<string> names;

Which of the following function declarations will accept the following two-dimension array?
int pages[10][30]

void f1(int pages[][30], int size);

(From the CS LAB) Regarding efficiency, if we have two nearby loops that operate on the same set of elements, we should combine their operational parts and use only one set of loop control operations. For example, these loops should be combined into one:

Loop fusion

What is the output of the following code?
int
p1,
p2;
p1 = new int;
p2 = new int;
*p1 = 11;
*p2 = 0;
p2 = p1;
cout<<
p1<<" "<<
p2<<endl;

11 11

Given that p1 is a pointer variable of the string class, which of the following are legal statements?

cout << *p1

(From the CS LAB) For our class we use hpp files for our functions to separate files. This practice is known as separate compilation. When we create a function, we placed an include guard at the beginning of the function, the ifndef and define. These ensu

if there is not a copy of this file on the compiler's preprocessor list, then go ahead and define it.

If the name of a file to open is in the string variable name fileName, which of the following will correctly open the file for output?

out_file.open(fileName.c_str());

Which sort algorithm does the following outline define?
for i between 0 and number_used-1 inclusive
put the ith smallest element at array[i]

selection

Which of the following correctly declares a user-defined data type that defines a pointer to a float?

typedef float* floatPtr;

Which of the following declare an array of 5 characters, and initializes them to some known values?

char array[5]={'a','b','c','d','e'}; and char array[5]={''};

(From the CS LAB) Error Handling is how we can manage possible run-time errors that were not due to the defective coding internal to our program. During the process of error handling we generate these from errors encountered as the preferred way for progr

exceptions

If a program requires a dynamically allocate two-dimensional array, you would allocate the memory by using

p1 = new int*[numRows];
for(int i-0; i < numRows; i++)
p1[i]=new int[numColumns];