Computer Science 101 Test 1 Name ______________________ 1. Convert 1212 base 3 to base 10 27 + 18 + 3 + 2 = 50 2. Convert 112 base 10 to base 5 112 / 5 = 22 r 2 22 / 5 = 4 r 2 4 / 5 = 0 r 4 422 3. Write the value of the binary number 11011011 in hexadecimal DB 4. Write the binary twos complement of the 8 bit binary value 00001101 11110011 5. The binary encoding 0100 0010 is the ascii encoding of what letter. (Be sure that your letter clearly shows the correct (upper/lower) case) B 6. For each of the following data types, how many bits are used to store each variable of the type by the C compiler on the Sun systems we will be using in lab: _8__ a. unsigned char _32_ b. int 7. Circle all of the following variable names that would be ILLEGAL in a C program: a. _jello b. _5ello C c. 5_jello d. jEllo_5 8. The following "C" program has two errors that must be corrected before it will compile correctly. For each line that contains a defect, write a corrected version of the line to the right of the defective line. #include stdio.h #include int main; int main() { return(0); } 9. In the assignment statement n = fscanf(stdin, "%d %d", &x, &y); if everything works correctly what should be the value of "n" a. 0 b. 1 C c. 2 d. 3 10. Suppose my program contains: n = fscanf(stdin, "%d %d %d", &x, &y, &z); and I enter the values: 1234 5678 9a22 What values will be assigned to n, x, y, and z? If a variable will not be assigned a value just write "unchanged". n = 3 x = 1234 y = 5678 z = 9 11. What is the output of the following program: d int main() { int x = 5; if (x = 1) point1 fprintf(stdout, "point 1 \n"); point2 point4 if (x == 1) fprintf(stdout, "point 2 \n"); if (x = 0) fprintf(stdout, "point 3 \n"); if (x == 0) fprintf(stdout, "point 4 \n"); } 12. This program which is attempting to read a single value from the standard input and write it to the standard output has two errors. Write a correct version of each defective statement. #include int main() { int value; fscanf(stdin,"%d", value); fscanf(stdin,"%d", &value); printf(stdout,"%d", value); fprintf(stdout,"%d", value); return (0); } 13. Consider the problem of computing the Fibonacci recurrence discussed in class. Identify the correct way to perform the basic update a. new = old + older; b. old = new; old = new; older = old; C older = old; new = older + old; c. new = older + old; d. All are correct older = old; old = new; 14. Consider the problem of finding the maximum value in an input file. Suppose the value maxval is used to hold the maximum. At the start of the program maxval should be initialized to: a. 0 b. 10000000 D c. -9999999 d. the first value in the file 15. Suppose I wish to run an executable program called p4 and cause its standard input to be read from file called p4.txt. The proper way for me to do this is: a. p4 -i p4.txt b. p4 p4.txt C c. p4 < p4.txt d. p4 - p4.txt 16. A color ppm image having dimension 200 rows by 400 columns contains how much image data: a. 500 bytes b. 60000 bytes D c. 80,000 bytes d. 240,000 bytes 17. The following is a correct ppm header: 50 36 0a 32 30 30 20 31 35 30 20 32 35 35 0a Suppose I build a solid color bright red image but When building the header if I accidentally add an extra space character to the header creating this: 50 36 0a 32 30 30 20 31 35 30 20 32 35 35 0a 20 ff 00 00 ff 00 00 .. a. it will have no adverse effect b. The image will appear when I view the image green instead of red B c. the image will appear blue d. The image will appear instead of red purple instead of red 18. 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[0] ... table[10] 19. Which of the following is the correct way to swap the values of adjacent elements in an array. a. temp = table[k + 1]; b. table[k + 1] = table[k]; table[k] = table[k + 1]; table[k] = table[k + 1]; table[k + 1] = temp C c. temp = table[k]; d. temp = table[k]; table[k] = table[k + 1]; table[k + 1] = temp; table[k + 1] = temp table[k] = table[k + 1]; 20. The C language has no "official" "character string" data type. Instead character strings are represented by arrays of characters with the end of the string represented by: a. a byte of binary 0's b. the space character A c. the new-line control d. the return control character character 21. The C language has no "official" "character string" data type. Instead character strings are represented by arrays of characters with the end of the string represented by: a. a byte of binary 0's b. the space character c. the new-line control d. the return control character character 22. When the %s format code is used to read a character string, reading will stop a. only when the end-of-string b. only when end-of-file occurs indicator is found in the in the input C input c. whenever any "whitespace" character is found in the input. 23. When using fprintf the correct way to use the %c and %s format codes for the last parameter passed to fprintf to contain a. the address of the b. 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. the value of the character and the value of the string 24. If I want to assign the value of the first command line parameter in a program invoked as a.out 400 to an integer variable called width, the correct way to do so is: a. width = argv[1]; b. width = sscanf(argv[1], "%d"); D c. sscanf(argv[1], "%d", width); d. sscanf(argv[1], "%d", &width); 25. In the two-d image example, the location of the col = 0; is row = 0; while (row < height) { col = 0; while (col < width) { a. incorrect and needs to be b. is correct as shown and moved to just after row = 0; moving it will break the program B c. will work correctly in either place. 26. Suppose the following code is executed char words[20]; int howmany; howmany = fscanf(stdin, "%s", &words[0]); while (howmany == 1) howmany = fscanf(stdin, "%s", &words[0]); and the contents of the standard input is: xyz 123 abc at EXIT from the loop a. words[0] contains the string b. words[0] contains 'a' "abc" words[1] contains "123" words[1] contains '2' C and words[2] contains "xyz" words[2] contains 'z' c. words[0] contains 'a' c. words[0] contains 'x' words[1] contains 'b' words[1] contains 'y' words[2] contains 'c' words[2] contains 'z' 27. Which of the the following is syntactically correct a. int sum( b. int sum{ int a; int a, int b; int b, { } { } D c. int sum( d. none of the above int a, int b, { } 28. What value will be printed by the following program: #include int addem( int a, int b) { int c; c = a + b; return(4); } main() { int sum; sum = addem(1, 2); printf("%d\n", sum); } 4