Skip to main content
留学咨询

辅导案例-CS 158/159

By May 15, 2020No Comments

CS 158/159 Final Exam Tuesday April 30, 2019, 10:30am – 12:30pm Elliott Hall of Music Instructor: Alan Bunning 50 Questions * 3 Points Each = 150 Points Exam Rules/Notices: 1. Fill in your name and your student ID# on the answer sheet (write it in AND fill in the bubbles). All student ID numbers must include ten digits beginning with two leading zeros (write it in AND fill in the bubbles). An incorrect student ID number will result in a zero for this exam. 2. Do not fill in the section or the test form. 3. You must be in your assigned seat to receive credit for this exam. 4. Make sure you have all 50 questions to this exam. You should answer every question. All questions only have one best answer. Please be careful when filling in your answers, any answer determined to be unreadable by Instructional Data Processing will be considered incorrect. Only the answer on your answer sheet can be considered when grading. An operator precedence table is provided on the back of this exam. 5. NO QUESTIONS WILL BE ANSWERED DURING THE EXAM. We do not answer questions during the exam due to the limited number of staff members present. It is easier for us to “throw out” a test question with an error than it is to answer questions or to make an announcement during the examination period. Manage your time accordingly! 6. You must assume that all C programs and code segments are implemented on the guru.itap machine and would be compiled with the gcc compiler as set up during the first week of the semester. When segments of code are presented you may assume that the appropriate include statements are present and that the code would be inside of a fully operational function or program. 7. Protect your work and keep your focus on your own exam. It is considered dishonest if you copy from another student or to permit another student to copy from you. If a spotter has any reason to suspect questionable behavior, you may be asked to move to another seat. Anyone found to violate course policies will receive a failing grade for the course and will be referred to the Office of the Dean of Students. 8. You are NOT permitted to use any resources during this exam. This includes but is not limited to; texts, notes, calculators, cell phones, MP3 players, and computers. If your cell phone audibly rings during the exam you will be required to immediately submit your exam and leave the testing facility. 9. When you are finished with the exam, please proceed back to the lobby to submit your answer form and to exit the facility. You will be required to show photo identification before we can accept your work. You will keep this exam form. You may not return to your seat once you have submitted your exam. 10. Your score (and exam answers) will appear on Blackboard as soon as possible. Do not contact course staff members about mistakes (if any) until answers are posted. 11. When time is called ALL writing must stop. You are not permitted to continue to make revisions to any answer on your exam once time has been called. 12. Final course grades will be posted by the afternoon of Tuesday of next week. Grades are ONLY assigned based on the total points earned. If you just miss the cut off for a certain grade, do not email your instructor asking for leniency because no exceptions will be made for that. It is also now too late to appeal the scores of any earlier assignments. NO QUESTIONS WILL BE ANSWERED DURING THE EXAM. 1. Given the array below: 15 12 8 11 17 13 and the array after two passes through a sorting algorithm: 8 11 15 12 13 17 identify which of the following algorithms has been applied. A) Selection Sort B) Bubble Sort C) Insertion Sort D) More than one of the above. 2. Given the array below: 15 12 8 11 17 13 which of the following is NOT a possible configuration of the array after two passes through the insertion sorting algorithm? A) {8, 12, 15, 11, 17, 13} B) {15, 12, 11, 8, 17, 13} C) {15, 12, 8, 11, 13, 17} D) None of the above. 3. Given the original array: 15 12 8 11 17 13 Using which of the following algorithms would result in a sorted array after just two sort passes? A) Selection Sort B) Bubble Sort C) Insertion Sort D) None of the above. Use the code segment below for problems 4 – 7 int x[8] = {1, 3, 5, 2, 4, 1, 3, 2}; int i = 0; do { i += x[i % 7]; x[i % 7] += 1; }while(i < 20); printf("i = %d\n", i); printf("x[1] = %d\n", x[1]); printf("x[3] = %d\n", x[3]); 4. Which of the following is the output generated by the first print statement in the code segment above? A) i = 20 B) i = 21 C) i = 22 D) None of the above. 5. Which of the following is the output generated by the second print statement in the code segment above? A) x[1] = 3 B) x[1] = 4 C) x[1] = 5 D) None of the above. 6. Which of the following is the output generated by the third print statement in the code segment above? A) x[3] = 2 B) x[3] = 3 C) x[3] = 4 D) None of the above. 7. Which of the following would be the final values of the first and last variables when using the binary search with the array below given a target value of 19? 6 11 13 15 16 18 21 24 26 27 31 35 A) first = 7 last = 6 B) first = 6 last = 6 C) first = 6 last = 5 D) None of the above. Use the program below for problems 8 – 10 #include void changeArray(int[], int); int main() { int x[8] = {4, 5, 3, 2, 8, 1, 2}; int i; for(i = 0; i < 8; i++) { changeArray(x, x[i]); } printf("x[2] = %d\n", x[2]); printf("x[4] = %d\n", x[4]); printf("x[7] = %d\n", x[7]); return(0); } void changeArray(int x[], int element) { int i; for(i = element; i < 8; i++) { x[i] += 2; } } 8. Which of the following is the output generated by the first print statement in the program above? A) x[2] = 5 B) x[2] = 7 C) x[2] = 3 D) None of the above. 9. Which of the following is the output generated by the second print statement in the program above? A) x[4] = 14 B) x[4] = 16 C) x[4] = 12 D) None of the above. 10. Which of the following is the output generated by the third print statement in the program above? A) x[7] = 8 B) x[7] = 12 C) x[7] = 10 D) None of the above. Use the program below for problems 11 – 13 #include int exchangeValues(int, int*); int main() { int x[9] = {1, 3, 5, 7, 2, 4, 6, 8}; int i; for(i = 1; i < 9; i += 2) { x[i + 1] = exchangeValues(x[i], &x[i - 1]); } printf("x[3] = %d\n", x[3]); printf("x[6] = %d\n", x[6]); printf("x[8] = %d\n", x[8]); return(0); } int exchangeValues(int a, int *b) { int i; i = a; a = *b; *b = i; return(a); } 11. Which of the following is the output generated by the first print statement in the program above? A) x[3] = 2 B) x[3] = 5 C) x[3] = 7 D) None of the above. 12. Which of the following is the output generated by the second print statement in the program above? A) x[6] = 6 B) x[6] = 7 C) x[6] = 8 D) None of the above. 13. Which of the following is the output generated by the third print statement in the program above? A) x[8] = 0 B) x[8] = 1 C) x[8] = 8 D) None of the above. Use the program below for problems 14 – 15 #include int calcRange(int[], int, int); int main() { int x[10] = {1, 3, 5, 2, 4, 6, 2, 1, 8, 2}; int best = 0; int bestLoc; int i; for(i = 0; i < 8; i++) { if(best < calcRange(x, i, i + 2)) { best = calcRange(x, i, i + 2); bestLoc = i; } } printf("best: %d\n", best); printf("bestLoc: %d\n", bestLoc); return(0); } int calcRange(int y[], int a, int b) { int i; int sum = 0; for(i = b; i >= a; –i) { sum += y[i]; } return(sum); } 14. Which of the following is the output generated by the first print statement in the program above? A) best: 11 B) best: 12 C) best: 17 D) None of the above. 15. Which of the following is the output generated by the second print statement in the program above? A) bestLoc: 2 B) bestLoc: 3 C) bestLoc: 4 D) None of the above. Use the program below for problems 16 – 18 #include #define SIZE 8 int sort(int[], int); int main() { int x[SIZE] = {7, 5, 3, 1, 8, 6, 4, 2}; int i
; int index; for(i = 1; i < SIZE; i++) { index = sort(x, SIZE - i - 1); if(i == 1 || i == SIZE - 1) { printf("i: %d index: %d\n", i, index); } } return(0); } int sort(int x[], int i) { int temp; while(i < SIZE - 1 && x[i] > x[i + 1]) { temp = x[i]; x[i] = x[i + 1]; x[i + 1] = temp; i += 1; } return(i); } 16. Which of the following sorting algorithms has been implemented in the program above? A) Selection Sort B) Bubble Sort C) Insertion Sort D) None of the above. 17. Which of the following is the first line of output generated by the program above? A) i: 1 index: 6 B) i: 1 index: 7 C) i: 1 index: 8 D) None of the above. 18. Which of the following is the second line of output generated by the program above? A) i: 7 index: 6 B) i: 7 index: 7 C) i: 7 index: 8 D) None of the above. Use the program below for problems 19 – 20 #include #define SIZE1 5 #define SIZE2 7 void populateData(int[][SIZE2], int, int); int main() { int x[SIZE1][SIZE2]; int i; int j; for(i = 0; i < SIZE1; i++) { for(j = 0; j < SIZE2; j++) { populateData(x, i, j); } } return(0); } void populateData(int y[][SIZE2], int i, int j) { y[i][j] = (i == 0 || j == 0) ? i + j : i * j; } 19. Which of the following statements regarding the two dimensional array in the program above is FALSE? A) The smallest integer value in the array is 0. B) The largest integer value in the array is 24. C) The largest integer in the second row is greater than the largest integer in the first row. D) None of the above. 20. Which of the following statements regarding the populateData user-defined function in the program above is TRUE? A) The size of the first dimension of the array is not necessary in the declaration and definition of the function. B) The size of the second dimension of the array is not necessary in the call of the function. C) Indicating the size of a dimension of the array in a function call will determine whether that particular dimension is passed by value or by address to the function. D) None of the above. 21. Which of the following statements regarding the binary searching algorithm is TRUE? A) The binary searching algorithm will always find a target in an array faster than the sequential searching algorithm. B) The binary searching algorithm should always be used for searching. C) With each comparison made in the binary search approximately half of the remaining elements in the array are eliminated as possible locations of the target. D) None of the above. Use the code segment below for problems 22 – 24 int x[10] = {4, 9, 6, 7, 4, 8, 2, 1, 8, 5}; int start = 0; int end = 9; int temp; while(start < end) { if(x[start] > x[0]) { temp = x[start]; x[start] = x[end]; x[end] = temp; end–; } else { start++; } } printf(“start: %d\n”, start); printf(“x[2]: %d\n”, x[2]); printf(“x[8]: %d\n”, x[8]); 22. Which of the following is the output generated by the first print statement in the code segment above? A) start: 3 B) start: 5 C) start: 7 D) None of the above. 23. Which of the following is the output generated by the second print statement in the code segment above? A) x[2]: 2 B) x[2]: 4 C) x[2]: 1 D) None of the above. 24. Which of the following is the output generated by the third print statement in the code segment above? A) x[8]: 8 B) x[8]: 5 C) x[8]: 7 D) None of the above. 25. Which of the following statements regarding the sorting algorithms introduced this semester is FALSE? A) After completing the first pass of the insertion sorting algorithm there are two values in the sorted list. B) On the final pass of the bubble sorting algorithm there are two values brought into the sorted listed. C) A value placed into the sorted list during the process of the selection sort will not move again on any remaining passes. D) None of the above. Use the program below for problems 26 – 27 #include #include void changeString(char[], char[]); int main() { char firstName[10] = “Pafdue”; char lastName[15] = “Lurayette”; changeString(firstName, lastName); printf(“firstName: %s\n”, firstName); printf(“String compare: %d\n”, strcmp(firstName, lastName)); return(0); } void changeString(char str1[], char str2[]) { int length = 0; do { if(str1[length] > str2[length]) { str2[length] = str1[length]; } else { str1[length] = str2[length]; } length++; }while(length < strlen(str1)); } 26. Which of the following is the output generated by the first print statement in the program above? A) firstName: Purdue B) firstName: Purdye C) firstName: Purduette D) None of the above. 27. Which of the following describes the integer output generated by the second print statement in the program above? A) The integer is positive. B) The integer is zero. C) The integer is negative. D) None of the above. 28. Which of the following statements regarding the null terminating character and character arrays is FALSE? A) The null terminating character is used to separate the data in the array from the unused elements of the array. B) The null terminating character is not needed when the amount of data present is equal to the capacity of the array. C) The null terminating character can be the first character in the array. D) None of the above. 29. Which of the following statements regarding arrays is TRUE? A) The use of the address operator is necessary in a scanf function when accepting input into an array element using the indexing notation []. B) The name of an array variable represents both the starting and ending location of the memory to which it has been assigned. C) The passing of a whole array to function can be done either by address or by value. D) None of the above. 30. Which of the following statements regarding index range checking in arrays is FALSE? A) Attempting to access an index beyond the range of valid index values for an array may cause a run-time error. B) It is possible that when attempting to access an index beyond the range of valid index values for an array that the output results may not be as expected. C) The problem of index range checking is limited to only those values that are greater than the largest index values for an array. D) None of the above. 31. Which of the following commands will sort the lines of the lab00.c file and append them to end of a second file called out_test? A) sort >> lab00.c > out_test B) sort < lab00.c << out_test C) sort < lab00.c >> out_test D) None of the above. 32. Which of the following would be the final values of the first and last variables when using the binary search with the array below given a target value of 7? 6 11 13 15 16 18 21 24 26 27 31 35 A) first = 1, last = 0 B) first = 0, last = 0 C) first = 0, last = -1 D) None of the above. 33. Which of the following statements regarding counter-controlled repetitive processes is FALSE? A) A counter-controlled process may be implemented with both pretest or post-test looping constructs. B) Counter-controlled processes are those that only increment or decrement by a constant value. C) The for loop and recursion are to be limited to use with counter-controlled processes only. D) None of the above. 34. Which of the following statements regarding symbolic/defined constants and course standards is TRUE? A) A symbolic/defined constant should be use camel casing to distinguish them from variable names. B) Problems associated with defined constants could be difficult to resolve as the programmer does not view the statement with the error after the substitution takes place. C) Creation of a symbolic/defined constant should be made in the same function within which it will be used. D) None of the above. 35. Which of the following statements regarding the conditional expression is FALSE? A) It is not possible to nest conditional expressions. B) The conditional expression has two operators and three operands. C) The result of a conditional expression can be assigned to a variable. D) None of the above. 36. Which of the following statements re
garding parameter passing is TRUE? A) It is never possible to determine which parameters are passed by address to a function by viewing its declaration. B) It is never possible to determine which parameters are passed by address to a function by viewing its definition. C) It is never possible to determine which parameters are passed by address to a function by viewing an example function call. D) None of the above. 37. Which of the following statements regarding type conversions is FALSE? A) When the data types of two operands for the division operator are different, the lower-ranked type is promoted to the rank of the higher type. B) In an assignment statement, promotion occurs if the right expression has a lower rank than the variable on the left and demotion occurs if the right expression has a higher rank. C) An explicit type conversion is the programmer taking control and specifying the data type of an operand in an expression. D) None of the above. 38. Given the logical expression below: x != 0 || y != 0 && z != 0 Where x, y, and z are integer variables, which of the following logical expressions is a complement? A) x && y || z B) !(x != 0 || y != 0) || z == 0 C) x == 0 && y == 0 || z == 0 D) None of the above. Use the program below for problems 39 – 41 #include int switchFx(int, int, int); int main() { int result; result = switchFx(10, 5, 2); printf(“result #1: %d\n”, result); result = switchFx(1, 0, 1); printf(“result #2: %d\n”, result); return(0); } int switchFx(int x, int y, int z) { int score = 0; switch(x + y % z) { case 0: score++; break; case 1: score++; default: score += 2; } return(score); } 39. Which of the following is the output generated by the first print statement in the program above? A) result #1: 1 B) result #1: 2 C) result #1: 3 D) None of the above. 40. Which of the following is the output generated by the second print statement in the program above? A) result #2: 1 B) result #2: 2 C) result #2: 3 D) None of the above. 41. Which of the following function calls would result in the program above either crashing or failing to compile? A) result = switchFx(0, 0, 1); B) result = switchFx(1, 1, 0); C) result = switchFx(1.5, 2.5, 1.0); D) None of the above. Use the program below for problems 42 – 44 #include #define APPROX_PI 3.14 int main() { float r = 3; float circ; circ = 2 * APPROX_PI * r; printf(“-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n”); printf(“Circumference: %*.1f\n”, (int) circ, circ); printf(“-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n”); printf(“Diameter: %-10.*f cm\n”, (int) APPROX_PI, r * 2); printf(“Area: %.1f\n”, (int) circ / 2 * r); return(0); } 42. Which of the following is the output generated by the first two print statements in the program above? -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- Circumference: 18.8 -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- Circumference: 18.8 -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- Circumference: 18.8 None of the above. 43. Which of the following is the output generated by the third and fourth print statements in the program above? -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- Diameter: 6.000 cm -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- Diameter: 6.000 cm -=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=- Diameter: 6.000 cm None of the above. 44. Which of the following is the output generated by the final print statement in the program above? A) Area: 27.0 B) Area: 28.2 C) Area: 28.3 D) None of the above. 45. Which of the following statements regarding the programming and documentation standard of the course is FALSE? A) Every user-defined function is limited to at most one return statement. B) The break statement is only acceptable to terminate the switch construct. C) The local variable declarations should not overlap with the executable statement section of a function. D) None of the above. A B C D A B C D Use the program below for problems 46 – 47 #include int valueChanges(int, int); int main() { int x = 5; int y = 3; int result = 0; do { result += valueChanges(x, y); }while(x++ < 10 || ++y < 10); printf("result: %d\n", result); printf("x: %d y: %d\n", x, y); return(0); } int valueChanges(int x, int y) { int z = 0; if(x % 2 > 0) { z += x % 2; } else if(y % 3 > 0) { z += y % 3; } return(z); } 46. Which of the following is the output generated by the first print statement in the program above? A) result: 6 B) result: 8 C) result: 9 D) None of the above. 47. Which of the following is the output generated by the second print statement in the program above? A) x: 11 y: 10 B) x: 17 y: 10 C) x: 11 y: 11 D) None of the above. 48. Which of the following statements regarding file functions is TRUE? A) The fscanf function requires the name of the external data file as one of its parameters. B) The fopen function requires the name of the external data file as its only parameter. C) The fclose function requires the name of the external data file as its only parameter. D) None of the above. Use the code segment below for problem 49 int x = 4; int *y; int *z; y = &x; z = y; (*z)–; printf(“result: %d\n”, *y + *z); 49. Which of the following is the output generated by the print statement in the code segment above? A) result: 5 B) result: 6 C) result: 7 D) result: 8 50. Which of the following statements regarding the end of the semester is TRUE? A) It is now too late to appeal the scores of any earlier assignments. B) Final course grades will be posted by the afternoon of Tuesday of next week. C) Grades are assigned based ONLY on the total points earned, and NO EXCEPTION will be made if you only miss a letter grade by a few points. D) All of the above! (hint: pick this one, all of the above are true!) ASCII Table Char Dec Char Dec Char Dec Char Dec delimiter 0 space 32 @ 64 ` 96 (soh) 1 ! 33 A 65 a 97 2 ” 34 B 66 b 98 3 # 35 C 67 c 99 4 $ 36 D 68 d 100 5 % 37 E 69 e 101 6 & 38 F 70 f 102 7 ‘ 39 G 71 g 103 8 ( 40 H 72 h 104 (ht) 9 ) 41 I 73 i 105 10 * 42 J 74 j 106 11 + 43 K 75 k 107 12 , 44 L 76 l 108 13 – 45 M 77 m 109 (so) 14 . 46 N 78 n 110 15 / 47 O 79 o 111 16 0 48 P 80 p 112 (dc1) 17 1 49 Q 81 q 113 (dc2) 18 2 50 R 82 r 114 (dc3) 19 3 51 S 83 s 115 (dc4) 20 4 52 T 84 t 116 21 5 53 U 85 u 117 (syn) 22 6 54 V 86 v 118 23 7 55 W 87 w 119 (can) 24 8 56 X 88 x 120 (em) 25 9 57 Y 89 y 121 (sub) 26 : 58 Z 90 z 122 27 ; 59 [ 91 { 123 28 < 60 \ 92 | 124 (gs) 29 = 61 ] 93 } 125 (rs) 30 > 62 ^ 94 ~ 126 (us) 31 ? 63 _ 95 127 (stx) (etx) (eot) (enq) (ack) (bel) (bs) (nl) (vt) (np) (cr) (si) (dle) (nak) (etb) (esc) (fs) (del) This page lists C operators in order of precedence (highest to lowest). Their associativity indicates in what order operators of equal precedence in an expression are applied. Operator Description Associativity () [] ++ — Parentheses (function call) Brackets (array subscript) Postfix increment/decrement left-to-right ++ — + – ! (type) * & sizeof Prefix increment/decrement Unary plus/minus Logical negation Cast (change type) Dereference Address Determine size in bytes right-to-left * / % Multiplication/division/modulus left-to-right + – Addition/subtraction left-to-right < <= > >= Relational less than/less than or equal to Relational greater than/greater than or equal to left-to-right == != Relational is equal to/is not equal to left-to-right && Logical AND left-to-right || Logical OR left-to-right ?: Ternary conditional right-to-left = += -= *= /= %= Assignment Addition/subtraction assignment Multiplication/division assignment Modulus assignment right-to-left , Comma (separate expressions) left-to-right Answers 1. B 2. B 3. B 4. A 5. C 6. A 7. C 8. C 9. A 10. A 11. C 12. C 13. B 14. B 15. B 16. C 17. B 18. A 19. C 20. A 21. C 22. A 23. A 24. B 25. D 26. B 27. C 28. B 29. A 30. C 31. C 32. A 33. B 34. B 35. A 36. D 37. D 38. D 39. B 40. C 41. B 42. C 43. A 44. A 45. D 46. C 47. B 48. D 49. B 50. D

admin

Author admin

More posts by admin