Computer Science 101 Midterm Exam Name ______________________ 1. Consider the following human computer program: Box# 0 store 3,103 a. When the program completes what value 1 store 0,100 will be in box# 100? 2 store 1,101 3 store 1,102 4 add 101,100,100 6 5 add 102,101,101 6 jumpc 100,le,103,4 7 halt b. What value will be in box# 101? 4 c. What value will be in box# 102? 1 2. The number of different values that can be represented using a 4 bits is: a. 4 b. 16 b c. 8 d. 256 3. Convert the binary number 10101 to decimal 21 4. Convert the decimal number 47 to binary 101111 5. a. Write the value of the binary number 10101011 in hexadecimal AB b. Write the hexadecimal number 3C in binary 00111100 6. What is the binary two's complement of the value 10101010 --------- 01010110 7. The sum of a number x and its two's complement is a. 0 b. 1 a c. 2x d. -1 8. Suppose v1 = 3 and v2 = 5 what are the values of the following expressions under the assumption that INTEGER ARITHMETIC is used by the computer in evaluting them. a. v1 + ((5 * v2) / (3 * v1)) 5 b. ((v1 + 5) * v2) / (3 * v1) 4 10. In the assignment statement n = fscanf(stdin, "%d %d %d", &x, &y, &z); if everything works correctly what should be the value of "n" a. 0 b. 1 d c. 2 d. 3 11. Suppose my program contains: n = fscanf(stdin, "%d %d %d", &x, &y, &z); and I enter the values: 1234 56a7 9122 What values will be assigned to n, x, y, and z? If a variable will not be assigned a value just write "unchanged". n = 2 x = 1234 y = 56 z = unchanged 12. int sum; // the sum of the single digit numbers int value; // the value just read int howmany; // howmany values were read 1 sum = 0; 2 howmany = fscanf(stdin, "%d", &value); 3 while (howmany == 1) { 4 if (value > 4) 5 sum = sum + value; 6 howmany = fscanf(stdin, "%d", &value); } 7 fprintf(stdout, "The sum was %d \n", sum); Suppose the input to this program is 5 1 6 Complete the execution trace table. Write the line number of EVERY statement that is executed even if the statement only tests but doesn't modify a variable. Write the new value of a variable in the appropriate column each time the variable is updated. A correct solution will exactly fill the table. Code Line # sum value howmany ---------------------------------------------- 1 0 ? ? ---------------------------------------------- see quiz7key.f07 ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- 13. For the following while loop to operate correctly while (cond) s1; a. it is necessary that statement b. it is necessary that statement s1 be able to change the value s1 NOT be able to change the value a of cond of cond c. It makes no difference whether or not s1 can change the value of cond 14. Identify the error(s) in the following program and write a correct version of any defective statement(s). The program's objective is to read a single integer from the standard input and write its value to the standard output. #include int main() { int value; int num; num = fscanf(stdin,"%d", value); &value fprintf(stdout,"%d", value); return (0); } 15. In the subsequence identification problem we discussed in class, value held the current value, old held the previously read value and older held the one before that. The correct order for these statements to properly update the three variables is: a. hm = fscanf(stdin, "%d", &value); b. old = value; old = value; hm = fscanf(stdin, "%d", &value); c older = old; older = old; c. older = old; d. These are all equivalent so old = value; any of them will work fine. hw = fscanf(stdin, "%d", &value); 16. Consider the problem of processing a large collection of pairs of integers and printing all the pairs whose sum is 10. The fprintf() statement should appear: a. before the while loop b. within the while loop b c. after the while loop 17. Consider the problem of processing a large collection of integers and printing "yes" if the collection contains a 13 and "no" if it does not. The fprintf() statement should appear: a. before the while loop b. within the while loop c c. after the while loop 18. Which of the following tests can be used to print the value of val if and only if the decimal representation of val has more than a single digit. (Circle all that apply) a. if ((val < -9) && (val > 9)) b. if (!((val >= -9) || (val <= 9))) fprintf(...) fprintf(...) c,d c. if ((val < -9) || (val > 9)) d. if (!((val >= -9) && (val <= 9))) fprintf(...) fprintf(...) 19. The proper way to write a red pixel into a ppm file is: a. fprintf(stdout, "%c %c %c", 255, 0, 0); b. fprintf(stdout, "%c%c%c", 255, 0, 0); b c. fprintf(stdout, "%c%c%c\n", 255, 0, 0); d. fprintf(stdout, "%c %c %c\n", 255, 0, 0); 20. Suppose I declare an array as: int table[10]; The valid set of indexes that I can use to reference elements of the array are: a. table[0] ... table[10] b. table[1] ... table[10] c c. table[0] ... table[9] d. table[1] ... table[9] 21. After the following loop executes what will be the value of nums[2]? nums[0] = 2; ndx = 1; while (ndx < 100) { nums[ndx] = 2 * ndx + nums[ndx - 1]; ndx = ndx + 1; } nums[2] = 8 22. Suppose table[k] = 22 and table[k + 1] = 44 and the following statements are executed: temp = table[k]; table[k] = table[k + 1]; table[k + 1] = temp After these two statments are executed what values will be held in: table[k] = 44 table[k + 1] = 22 23. Suppose table[k] = 22 and table[k + 1] = 44 and the following statements are executed: temp = table[k + 1]; table[k] = table[k + 1]; table[k + 1] = temp After these two statments are executed what values will be held in: table[k] = 44 table[k + 1] = 44 24. Consider the following code which is designed to read values into consecutive array elements and set "counter" to the number of values read. counter = 0; howmany = fscanf(stdin, "%d", &values[0]); while (howmany == 1) { howmany = fscanf(stdin, "%d", &values[counter]); counter = counter + 1; } Which best describes the operation of the program: a. it works correctly b. it counts correctly but the values read are assigned to b incorrect array elements. c. it assigns the values to the d. counter will be incorrect AND proper array elements but sets values will be read into counter incorrectly incorrect array locations. 25. Considering the "swapping" problem in which the program proceeds from index 0 comparing each adjacent pair of values in the array and swapping values if table[k] > table[k + 1]. What is the correct output for this input: 4 19 3 4 25 1 7 4 3 4 19 1 7 25 26. Suppose counter holds the number of values that have been read in. The following code is designed to compute the sum of all the values, but it is broken. Show how to fix it. ndx = 0; sum = 0; while (ndx < counter) { sum = sum + values[ndx]; --> ndx = ndx + 1; } 27. Suppose ndx represents an offset in a grayscale image, the proper way to compute the row and column indicies of the corresponding pixel is: a. row = numrows / ndx; b. row = numrows / ndx; col = numcols / ndx; col = numcols % ndx; c. row = numrows / ndx; d. row = numcols / ndx; col = numcols / ndx; col = numcols % ndx; 28. Consider the problem of counting the number of instances of values between 0 and 9 in an input file. Which is the proper way to do this. a. num = fscanf(stdin, "%d", &ndx); b. num = fscanf(stdin, "%d", &ndx); while (num == 1) while (num == 1) { { a ctrs[ndx] = ctrs[ndx] + 1; ctrs[ndx] = ctrs[ndx] + ndx; num = fscanf(stdin,"%d",&ndx); num = fscanf(stdin,"%d",&ndx); } } c. num = fscanf(stdin, "%d", &ndx); d. num = fscanf(stdin, "%d", &ndx); while (num == 1) while (num == 1) { { ctrs[ndx] = ndx + 1; ctrs[ndx] = ndx; num = fscanf(stdin,"%d",&ndx); num = fscanf(stdin,"%d",&ndx); } } 29. The correct way to pass the value to be printed to fprintf() when using the the %c and %s format codes is: a. pass the address of the b. pass the value of the character character and the address to be printed but the address of the string in both of the string to be printed b cases. c. pass the value of the character and the value of the string 30. Which of the the following is syntactically correct a. int sum( b. int sum{ int a; int a, int b; int b, { return (a+b);} { return (a+b);} d c. int sum( d. int sum( int a, int a, int b, int b) { return (a+b);} { return (a+b);}