Computer Science 101 Quiz 1 Name ______________________ ----- PLEASE ALSO WRITE YOUR NAME OF THE BACK OF THE PAPER ------ ----- PLEASE DO NOT FOLD THE PAPER ------ ----- You may leave when you finish ------ 1. Which of the following is NOT one of the three major classes of hardware components found in a computer system: a. processing elements b. programming elements c. storage elements d. communication elements 2. Which of the following is NOT part of the instruction set of the "human computer" discussed in class. a. halt b. sub c. jumpc d. start 3. Consider the adding program given in the notes: Box 0 store 100,103 !Store the upper limit of 100 in box 103 1 store 0,100 !Initialize the sum of all values added so far to 0 2 store 1,101 !Initialize the value to be added to be 1 initially 3 store 1,102 !We also need a value to increment the value to be added 4 add 101,100,100 !add contents of box 101 to box 100 leaving sum in 100 5 add 102,101,101 !increment the value to be added to box 100 by 1 6 jumpc 101,le,103,4 !as long as the value in box 101 is less than or equal !to the limit 103 continue to add 7 halt What will be the values in box 100 and 101 the third time the instruction in box 6 is executed? box 100 = box 101 = 4. The effect of the human computer instruction add 20,30,60 is to: a. store a 50 in box 60 b. store a 90 in box 20 c. store the sum of the values d. store the sum of the values found in boxes 20 and 30 in found in boxes 60 and 30 in box 60. box 20. Computer Science 101 Quiz 1 Name ______________________ ----- PLEASE WRITE YOUR NAME OF THE BACK OF THE PAPER ------ 1. The number of bits in the "standard" byte found in all modern computers is a. 1 b. 8 c. 16 d. 32 2. The number of hexadecimal digits needed to represent the value of a 32 bit word is: a. 32 b. 8 c. 4 d. 1 3. Convert 111001 binary to decimal 4. Convert 45 decimal to binary 5. a. Write the value of the binary number 11100111 in hexadecimal b. Write the hexadecimal number 3C in binary Computer Science 101 Quiz 3 Name ______________________ 1. What is the binary two's complement of the value 10101000 2. The sum of a number x and its two's complement is a. 0 b. 1 c. 2x d. -1 3. Find the 8 bit unsigned sum of the following two 8 bit unsigned numbers 101000101 011010101 --------- 4. Consider the following 8 bit SIGNED sums: Circle ALL those in which OVERFLOW will cause WRONG answer to be computed. (You don't have to compute the sum) a. 01101111 b. 11111111 c. 1111111 01000000 11111110 0000001 -------- -------- ------- 5. a. The binary encoding 0110 0100 is the ascii encoding of what letter. (Be sure that your letter clearly shows the correct (upper/lower) case) b. What is the binary encoding of the case coverted encoding of the same letter Computer Science 101 Quiz 4 Name ______________________ 1. 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 systems we use in the lab: ____ a. unsigned char ____ b. short 2. Circle all of the following variable names that would be ILLEGAL in a C program: a. _hello b. hellNO c. 5ello d. ShorT 3. Suppose v1 = 7 and v2 = 2 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)) b. ((v1 + 5) * v2) / (3 * v1) 4. 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 int main; { return(0); } Computer Science 101 Quiz 5 Name ______________________ 1. 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 = x = y = z = 2. 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); fprintf(stdout,"d", value); return (0); } 4. What value will the following code print: int x = 5; int y = 7; int z = 10; if ((x < 7) || (z < x)) printf("%d", x); else printf("%d", y); 5. What value will the following code print: int x = 5; int y = 7; int z = 10; if ((x < 7) && !(y < z)) printf("%d", x); else printf("%d", y); Computer Science 101 Quiz 6 Name ______________________ 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. DO NOT write the value of any variable that doesn't change. A correct solution will occupy all lines. Code Line # sum value howmany ---------------------------------------------- 1 0 ? ? ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- ---------------------------------------------- Computer Science 101 Quiz 7 Name ______________________ Supposed you are assigned the mission of computing the average of a collection of integers to be read from the standard input. 1. The program must compute a count of the number of values in the file AND the sum of all the values in the file. These computations a. must be done in separate b. must be done in a single while loops while loops c. can be done equally easily d. do not require any looping either way at all. 2. If the actual average is computed using the statement avg = sum / count; This statement must appear a. Before the while() loop(s) b. After the while() loop(s) c. Within the while() loop(s) d. The program can work correctly with it in any of the three locations. 3. Now suppose your mission is to read in a collection of pairs of integers and print out all pairs whose sum is 12. The print statement that is used to print the pairs must go: a. Before the while() loop b. After the while() loop c. Within the while() loop d. The program can work correctly with it in any of the three locations. 4. 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); 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); 5. 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. if ((val < -9) || (val > 9)) d. if (!((val >= -9) || (val <= 9))) fprintf(...) fprintf(...) Computer Science 101 Quiz 8 Name ______________________ 1. In computing a recurrence in which the new value is the sum of the two previous value the correct order for the update is: 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. new = old + older; b. older = old; old = new; new = old + older; older = old; old = new; c. new = old + older; d. These are all equivalent so older = old; any of them will work fine. old = new; 2. Which ONE 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);} c. int sum( d. int sum( int a, int a, int b, int b) { return (a+b);} { return (a+b);} 3. If I have correctly written the sum function and try to call it using: total = sum(3, 4); a. I'll get an error because b. I'll get an error because I I must pass it variables must pass it variables not constants. named a and b c. It'll work and total will be d. It won't work because the correct form set to 7. is sum(3, 4) = total; 4. Which of the following is NOT and advantage of breaking a large program up into a collection of functions: a. it facilitates top down b. it reduces program execution (CPU) design time c. it controls nesting of if's d. it improves program readability and while's 5. The proper way to run a program so that the standard input comes from a file named in.txt and the standard output goes to out.txt is: a. ./a.out in.txt out.txt b. ./a.out < in.txt | out.txt c. ./a.out < in.txt out.txt d. ./a.out < in.txt > out.txt Computer Science 101 Quiz 9 Name ______________________ 1. Which of the following is a correct function prototype: a. int sum(a, b) b. int sum(int a, int b){}; c. int sum(int a, int) d. int sum(int a, int b); 2. If I have a collection of prototypes for functions that I have written in a file named myhdrs.h in the current working directory the proper way to include it is: a. #include "myhdrs.h" b. #include myhdrs.h c. #include d. #include {myhdrs.h} 3. A color ppm image having dimension 200 rows by 300 columns contains how much image data: a. 500 bytes b. 60000 bytes c. 180000 bytes d. 1500 bytes 4. 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); c. fprintf(stdout, "%c%c%c\n", 255, 0, 0); d. fprintf(stdout, "%c %c %c\n", 255, 0, 0); 5. The following is a correct ppm header in hexadecimal. 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 c. the image will appear blue d. The image will appear instead of red purple instead of red Computer Science 101 Quiz A Name ______________________ 1. 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. table[0] ... table[9] d. table[1] ... table[9] 2. In C, if I accidentally use a value of ndx that is NOT in the valid set of indexes in the statement: table[ndx] = 13; (Circle all that apply) a. the error will be detected b. Other data within my by the C compiler and my program program may be overwritten can not be compiled until I with a value of 13. correct the problem. c. The array will be automagically d. If ndx is sufficiently far enlarged in size so that no out of bounds execution of problem will occur. my program may be terminated because of a memory protection violation. 3. After the following loop executes what will be the value of nums[2]? nums[0] = 3; ndx = 1; while (ndx < 100) { nums[ndx] = 2 * nums[ndx - 1] - 2; ndx = ndx + 1; } nums[2] = 4. 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 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. Computer Science 101 Quiz B Name ______________________ 1. Suppose table[k] = 44 and table[k + 1] = 22 and the following statements are executed: table[k] = table[k + 1]; table[k + 1] = table[k]; After these two statments are executed what values will be held in: table[k] = table[k + 1] = 2. Suppose table[k] = 44 and table[k + 1] = 22 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] = table[k + 1] = 3. Suppose table[k] = 44 and table[k + 1] = 22 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] = table[k + 1] = 4. Consider the "swapper" function 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 5. Suppose we want to SORT an array of N elements so that the values are in strictly increasing order. We can do this by: a. calling the "swapper" function b. calling the swapper function once N times c. no amount of calling swapper will ever sort the array. Computer Science 101 Quiz C Name ______________________ 1. 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 bits b. the space character c. the new-line control d. the return control character character 2. When a string of characters is read into a character array using the %s format code, the end-of-string indicator (referenced in question 1) will be: a. automatically added by b. must be added by the program fscanf() c. must be present in the input data 3. 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 cases. c. pass the value of the character and the value of the string 4. "Whitespace" characters are handled by fscanf() when the %c and %s format codes are specified as follows: a. they are read in with %c but b. they are read in with %s but skipped over with %s skipped over with %c c. they are read in with both d. they are skipped over by both 5. 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 a newline occurs indicator is found in the in the input input c. whenever any "whitespace" character is found in the input. Computer Science 101 Quiz D Name ______________________ 1. Suppose I run a program as: a.out lithuania 400 300 The value of argc will be: a. 3 b. 2 c. 1 d. 4 2. If I want to assign the value of the second command line parameter (400 in problem 1) to an integer variable called width, the correct way to do so is: a. sscanf(argv[1], "%d", width); b. sscanf(argv[1], "%d", &width); c. sscanf(argv[2], "%d", width); d. sscanf(argv[2], "%d", &width); 3. Given the following definitions struct pixel_type { unsigned char r; unsigned char g; unsigned char b; }; struct pixel_type pixmap[600 * 800]; which is the correct way to set the red component of element 15 to 250. a. pixmap.r[15] = 250; b. pixmap.[15]r = 250; c. pixmap[15].r = 250; d. pixmap.r[250] = 15; Given the following definitions typedef struct image_type { char filename[64]; /* name of disk resident file */ char id[4]; /* P5 gray or P6 color */ int width; /* Width in pixel cols */ int height; /* depth in pixel rows */ pixel_t pixels[1024*1024]; /* The pixel data */ } image_t; struct image_type input; 4. Which of the following is the correct way to set the "width" to 600 a. image_t.width = 600; b. input.width = 600; c. input.width[0] = 600; d. image_t.width[0] = 600; 5. Which of the following is the correct way to set the red component of the first pixel: a. image[0].pixels.r = 250; b. image.pixels[0].r = 250; c. image.pixels.r[0] = 250; Computer Science 101 Quiz E Name ______________________ 1. Given the following definitions circle ALL of the following assignments that are syntactically correct: struct pix struct pix p1; { struct pix *p2; unsigned char r; unsigned char g; unsigned char b; }; a. p1->r = 250; b. p2->r = 250; c. p2.r = 250; d. p1.r = 250; 2. Given the prototype: void blue_pix(struct pix *p); Suppose the caller of blue_pix() has declared: struct pix pixval; The syntactically correct way to invoke blue_pix is: a. blue_pix(pixval); b. blue_pix(*pixval); c. blue_pix(&pixval); d. all of these work fine 3. Now suppose the prototype is: void blue_pix(struct pix p); The syntactically correct way to invoke blue_pix is: a. blue_pix(pixval); b. blue_pix(*pixval); c. blue_pix(&pixval); d. all of these work fine 4. Suppose the mission of blue_pix() is to set the color of the callers pixel to blue (r = 0, g = 0, b = 255). Which best characterizes the approaches used in questions 2 and 3: a. both will correctly set b. question 2 will set pixval pixval to blue. to blue but question 3 won't modify pixval. c. neither approach can be used d. question 3 will set pixval to modify pixval. to blue but question 2 won't modify pixval. 5. Suppose an image has width 300 and height 200. The index in the pixel array of the pixel at row 3 col 52 is: ndx = Computer Science 101 Quiz F Name ______________________ 1. Given the following definitions struct pixel_type { unsigned char r; unsigned char g; unsigned char b; }; struct pixel_type pixmap[600 * 800]; which is the correct way to set the red component of element 15 to 250. a. pixmap.r[15] = 250; b. pixmap.[15]r = 250; c. pixmap[15].r = 250; d. pixmap.r[250] = 15; Given the following definitions typedef struct image_type { char filename[64]; /* name of disk resident file */ char id[4]; /* P5 gray or P6 color */ int width; /* Width in pixel cols */ int height; /* depth in pixel rows */ pixel_t pixels[1024*1024]; /* The pixel data */ } image_t; struct image_type image; 2. Which of the following is the correct way to set the red component of the first pixel: a. image[0].pixels.r = 250; b. image.pixels[0].r = 250; c. image.pixels.r[0] = 250; 3. Given the prototype: void blue_pix(struct pix *p); Suppose the caller of blue_pix() has declared: struct pix pixval; The syntactically correct way to invoke blue_pix is: a. blue_pix(pixval); b. blue_pix(*pixval); c. blue_pix(&pixval); d. all of these work fine 4. Now suppose the prototype is: void blue_pix(struct pix p); The syntactically correct way to invoke blue_pix is: a. blue_pix(pixval); b. blue_pix(*pixval); c. blue_pix(&pixval); d. all of these work fine 5. Suppose the following fwrite() succeeds. What will be the value of howmany? howmany = fwrite(img->pixels, sizeof(pixel_t), 40000, out); howmany = Computer Science 101 Quiz G Name ______________________ 1. The correct format code for printing a double precision floating point value in scientfic notation is: a. %e b. %f c. %le d. %g 2. In the format specifier %12.6f the 6 means: a. 6 different floats are to b. 6 digits to the right of be printed the decimal are to be printed. c. 6 total digits are to be d. 6 digits to the left of printed. the decimal are to be printed. 3. Which best characterizes spacing of int and float values on the number line. a. both are evenly spaced b. neither is evenly spaced c. ints are evenly spaced d. floats are evenly spaced but not floats but not ints Suppose a local structure variable has been allocated as: struct work_t temp; 4. Which of the following will produce a compiler warning and a possible execution time error. a. return(&temp); b. return(temp); c. both d. neither one. 5. Which of the following will cause the structure to be physically copied regardless of its size. a. return(&temp); b. return(temp); c. both d. neither one. Computer Science 101 Quiz H Name ______________________ 1. What is the value of the following expressions: a. (5 / 2) * 4.0 = b. (5 / 2.0) * 4 = Consider the following sentence in the image manipulation language: fade { factor 0.4 in0 dive.ppm out fade.ppm } 2. What format code should be used to read in the word "fade" a. %c b. %d c. %lf d. %s 3. What format code should be used to read the } a. %c b. %d c. %lf d. %s 4. In parsing this sentence which of the following words will be passed to the table_lookup() function (Circle all that apply) a. fade b. factor c. dive.ppm d. { 5. The number of elements in a lookup table named table is: a. sizeof(table) b. sizeof(table/table[0])) c. sizeof(table[0]) / sizeof(table) d. sizeof(table)/sizeof(table[0]) Computer Science 101 Quiz I Name ______________________ 1. What will be the value of y after the following code is executed: y = 4; p = &y; *p = 7; a. 4 b. 7 c. the address of y d. Undefined 2. Which best characterizes the behavior of the following program: main() { int *p; *p = 99; printf("The value of p is %d \n", *p); } a. The program is correct and b. The program is incorrect should be expected to work and will fail in all environments fine in all environments before the printf() is executed c. The program is incorrect, but it may appear to work correctly in some environments. 3. Suppose the following declarations are made in a program to be run on an Intel x86 computer. char *x; short *y long *z; a. Each of the three pointer b. The three pointer variables consume variables consumes 4 bytes 1, 2, and 4 bytes respectively of space. c. Each of the three pointer variables consumes 1 byte of space. 4. Given the following declarations int x; int *p; Identify the assignments that are syntactically correct and will compile without warning or error. ___ a. p = x; ___ b. p = &x; ___ c. *p = 15; ___ d. x = *p; Computer Science 101 Quiz J Name ______________________ 1. Which is the proper way to dynamically allocate storage for an array of 100 integers. (For questions 2, 3, 4 assume that the pointer variable loc is declared as: int *loc; a. loc = (int *)malloc(sizeof(int) * 100); b. loc = (int *)malloc(100); c. loc = (int *)malloc(4 * sizeof(int)); 2. Suppose the values of the 100 integers are to be printed in a for () loop. In each interation through the loop, the proper way to update the pointer used to point to the value to be printed is: a. loc = loc + 4; b. loc = loc + 1; c. loc = loc + sizeof(int); d. loc = loc + 100; 3. The proper way to print the value of the current integer is: a. printf("%d \n", &loc); b. printf("%d \n", loc); c. printf("%d \n", *loc); 4. The proper way to read an element into the array is: a. scanf("%d", &loc); b. scanf("%d", loc); c. scanf("%d", *loc); Computer Science 101 Quiz K Name ______________________ For problems 1 and 2 assume that the variable loc was declared int *loc and has been set to the address of an element in an array of ints. 1. The proper way to print the value of the current integer is: a. printf("%d \n", &loc); b. printf("%d \n", loc); c. printf("%d \n", *loc); 2. The proper way to read an element into the array is: a. scanf("%d", &loc); b. scanf("%d", loc); c. scanf("%d", *loc); 3. Given the following declarations int x; int *p; circle all that are syntactically correct: a. p = *x; b. x = *p; c. p = &x; d. *p = &x; 4. Complete the following function: (hint: it requires three simple assignment statements) void swapper(int *x, int *y) // swap values pointed to by x and y { int temp; } Computer Science 101 Quiz L Name ______________________ 1. The number of ints created by the following declaration int twod[3][6]; a. 9 b. 10 c. 18 d. 36 2. Which of the following is the correct way to pass the address of row 2 of the array to a function declared as fun(int *vals); a. fun(twod) b. fun(twod[2][0]) c. fun(&twod[2]) d. fun(twod[2]) 3. Which of the following array elements lies nearest in memory to twod[0][0] a. twod[0][5] b. twod[1][1] c. twod[1][0] d. twod[2][5] 4. Which of the following statement(s) is a correct way to read a value into twod[2][1]? (Circle all that are correct) a. fscanf(in, "%d", twod[2][1]); b. fscanf(in, "%d", &twod[2][1]); c. fscanf(in, "%d", twod[2] + 1); 5. Fix the following loop so that each row of the table will be printed on a separate line. for (i = 0; i < 3; i++) { for (j = 0; j < 3; j++) { fprintf(stderr, "%6.2lf", tab[i][j]); } } Computer Science 101 Quiz M Name ______________________ For each of the following tasks identify the I/O mechanism that should be used to accomplish it -- if multiple approaches work circle them all. 1. Read a single whitespace delimited "word" into a character array a. fgets() b. fscanf() with %s format c. fscanf() with %c format d. fread() 2. Read a single character with no data conversion. a. fgets() b. fscanf() with %s format c. fscanf() with %c format d. fgetc() 3. Read all the characters until a newline is found into a character array a. fgets() b. fscanf() with %s format c. fscanf() with %c format d. fread() 4. In this program the 3 declarations of y will int y = 52; int main() { int y = 34; a. will result in there being { three separate locations int y = 77; in which a variable named printf("%d ", y); y is stored. } printf("%d \n", y); } b. cause a syntax error for c. will result in all three duplication of declaration declarations referring to the same memory location. 5. The output of the program will be: a. no output at all b. 77 77 c. 77 52 d. 77 34 Computer Science 101 Quiz N Name ______________________ 1. For the following function identify which of the variables will reside on the heap (H) or on the stack (S) int a; static int b; int incr(int c) { int d; static int e; return(c + 1); } _____ a _____ b _____ c _____ d _____ e 2. Which of the variables declared above could be modified by a function in ANOTHER SOURCE module. _____ a _____ b _____ d _____ e Computer Science 101 Quiz O Name ______________________ 1. Complete the following truth table: x y | not (x or y) ---------------------- 0 0 | | 0 1 | | 1 0 | | 1 1 | 2. In the numeric specification we used in class the function of question 1 is: a. f3 b. f7 c. f8 c. f15 3. The boolean functions of two variables that are DIRECTLY implemented in C are: a. f11 and f15 b. f1 and f7 c. f12 and f6 d. f12 and f14 4. Suppose x, y, z are Boolean variables and x = 1, y = 0, z = 1 What is the value of: (x or y) and ((not (x xor z)) or (y and z))