CSCE 206: Structured Programming in C++ 2017 Spring Exam 3 Monday, April 17, 2017 Total - 100 Points B Instructions: Total of 11 pages, including this cover and the last page. Before starting the exam, count the pages to make sure you have all 11. Read the questions and instructions carefully to see what kind of answer is expected. Some code snippets may not have the header and std namespace inclusion, but assume that there are included. Put the Exam Code on your scantron in the lower right corner There are 50 questions worth 2 points each. On my honor, as an Aggie, I have neither given nor received unauthorized aid on this academic work Signature: Name (Print): UIN: Section: 1
TRUE/FALSE: Select A if the statement is true and B if the statement is false. 1) C++ performs a check to make sure that a program does not try to access a subscript beyond the bound of an array. 2) The following code snippet prints 11 38 int main() { int table[1][2] = {38,11}; cout << table[0][1]<<" "; cout << table[0][0]; return 0; } 3) The maximum number of dimensions an array can have is two. 4) To copy one array to another, each element must be copied one-by-one. 5) The binary search performs at most log 2 N comparisons 6) If arr is an array of integers, the following statement will display the contents of arr. cout << arr << endl; 7) With pointer variables you can access, but you cannot modify, data in other variables. 8) The output of the following program is 0. int main() { vector<int> V1(20,20); vector<int> V2(V1); cout << V2[18]; return 0; } 9) When an array that has been passed to a function is modified, the original array is modified as well. 10) The first element of an array has an index of 0. 2
11) In a two-dimensional array declaration, the first size declarator specifies the number of rows, and the second size declarator specifies the number of columns. 12) For an array called a with 10 elements, a[10] will return the last element a. 13) A bubble sort can only arrange data into ascending order. 14) A vector object automatically expands in size to accommodate the items stored in it. 15) When a multi-dimensional array is passed to a function as a papameter, it is required to specify all but 1 st dimension in the prototype and header. MULTIPLE CHOICE: Choose the the single option that best completes the statement or answers the question. 16) The effect of using the ++ operator with an int pointer variable is ( e.g., ptr++) A) The value the pointer points to is incremented by one B) The address the pointer is holding is incremented by one C) The address the pointer is holding is incremented by the size of a single int D) The ++ can t be used on pointers E) The outcome is compiler dependent 17) What is the output of the following program? int a[] = {1, 1, 2, 3, 5}; for (int i = 0; i <= 5; i+=2) { cout << a[i] << " "; } A) 1 1 2 3 5 B) 1 2 5 C) 1 1 2 3 5 plus a garbage value D) 1 2 5 plus a garbage value E) 1 2 5 0 3
18) What is the output of the following code snippet? int i = 0; int a[] = {11, 38}; cout << a[i]++ << " " << a[i++]; A) 12 38 B) 11 12 C) 38 11 D) 38 12 E) Garbage value 19) Which of the following is a valid way to access the third element of an array named arr? A) *(arr) + 3 B) *(arr + 3) C) arr[3] D) A and C E) B and C 20) This vector function returns the number of elements in a vector. A) length() B) num_elements() C) elements() D) size() E) count() 21) In a local array, all elements are initialized to by default. A) 0 B) 1 C) -1 D) Uninitialized or garbage value E) Local value 22) How many swaps does a bubble sort require to sort the array {1,2,3,4} in descending order? A) 5 B) 4 C) 7 D) 6 E) Depends on the compiler 4
23) Which of the following are valid array definitions? A) int array[5]; B) int array[] = {0, 0, 0, 0, 0}; C) int array[3] = {1, 2, 3}; D) A and B E) A, B, and C 24) A variable that holds an address is called. A) An address variable B) A pointer variable C) A reference variable D) A dereference variable E) A stack variable 25) 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 26) Sorting algorithms can arrange values into which of the following orders: A) Equally spaced order B) Ascending C) Descending D) B and C E) A and B 27) An array that is initialized with fewer elements than its size is called a. A) Incomplete array B) Partially-filled array C) Semi-filled array D) fewer elements array E) Will cause a compilation error 5
28) Two or more array that contain related data are called. A) Adjacent arrays B) Multi-dimensional arrays C) Parallel arrays D) Equivalent arrays E) Related arrays 29) What is the output of the following program? int n = 0; int *nptr = n; cout << *nptr; A) The value of n B) The address of n C) The address of nptr D) A compiler error E) The value stored in nptr 30) What is the output of the following code snippet char i = 't'; char* ptr = &i; cout<<sizeof(ptr)<<" "; cout<<sizeof(*ptr); A) 1 1 B) 1 4 C) 4 1 D) 4 4 E) 4 8 31) This vector function returns false if the vector contains more than 0 elements. A) has_no_elements() B) null_size() C) empty() D) is_empty() E) zero_size() 6
32) The keyword is used to dynamically allocate memory. A) allocate B) mem C) mallalloc D) new E) memnew 33) The sort is usually more efficient than the sort. A) Selection, bubble B) Binary, linear C) Bubble, selection D) ANSI, ASCII E) Bubble_Advanced, bubble 34) What is the output of the following code snippet const int t = 100; int q = 36; const int* ptr = &t; cout << *ptr << " "; ptr = &q; cout << *ptr; A) 36 100 B) 100 36 C) 36 36 D) Compilation error E) Run time error 35) 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) 99 87 66 E) 87 101 7
36) Given an array containing {1, 4, 6, 12, 18, 26, 33}, a linear search will take comparisons to find the value 26 and a binary search will take comparisons. A) 2, 2 B) 2, 6 C) 6, 2 D) 2, 5 E) 5, 2 37) What is the output of the following program? int n = 10, *nptr = &n; cout << *nptr; A) The value of n (i.e. 10) B) The address of n C) The address of nptr D) No output E) Garbage value 38) Which statement correctly defines a vector object for holding integers? A) vector v<int>; B) int vector v; C) vector<int> v; D) int<vector> v; E) <int> vector v; 39) The keyword is used to free dynamically allocated memory. A) delete B) free C) release D) erase E) empty 8
40) What is the output of the following code? int arr[] = {0, 6, 2, 7}; int *arrptr = arr; cout << arrptr[2]; A) 2 B) 6 C) A garbage value D) 0 E) 7 41) A variable that can store multiple values of the same type is called a(n): A) Array B) List C) Collection D) Set E) Multi-element variable 42) This algorithm starts at the first element of an array and steps through, examining each element until it locates the value it is searching for. A) Linear Search B) Binary Search C) Bubble Sort D) Selection Sort 43) The, also known as the address operator, returns the memory address of a variable. A) & B) * C)! D) % E) &* 44) What is the main disadvantage of the binary search algorithm? A) It is inefficient B) The elements must be sorted first C) It only works for binary numbers D) It is difficult to program E) There are no disadvantages 9
45) When you work with a dereferenced pointer, you are working with. A) The actual value of the variable whose address is stored in the pointer variable B) A copy of the value pointed to by the pointer variable C) A reference of the variable pointed to by the pointer variable D) A value of a string variable E) Both B and C 46) What is the output of the following code snippet? int a[3] = {5}; for (int i = 0; i < 3; i++) cout << a[i] << " "; A) 5 5 5 B) 5 0 0 C) 0 0 0 D) 0 0 5 E) 5 0 5 47) Before performing linear search, the array elements must be, A) Summed B) Set to zero C) Sorted in either increasing or decreasing order D) Positive numbers E) No constraints 48) When the less than ( < ) operator is used between two pointer variables ( ptr1 < prt2 ), the expression is testing whether. A) The first variable was declared before the second variable B) The value pointed to by the first variable is less than the value pointed to by the second variable C) The value pointed to by the first variable is greater than the value pointed to by the second variable D) The address stored in the first variable comes before the address stored in the second variable E) This operation is illegal 10
49) What is the output of the following code snippet char arr[100]; int i; for(i = 0; i < 10; i++) { *(arr + i) = 65 + i + 2; } *(arr + i) = '\0'; cout << arr; A) ABCDEFGHIJ B) AAAAAAAAAA C) JJJJJJJJ D) CDEFGHIJKL E) 67697173757779818385 50) The operator is used to declare an array. A) [] B) & C) * D)? E) () 11