Computer Science 215 Quiz 1 Name ______________________ 1. The number of distinct values that a single byte of computer memory may take on is: a. 2 b. 8 c. 255 d. 256 2. In the C language a variable of type "char" a. is used to hold character, b. holds 8 bit integers but not numeric data c. holds 32 bit integers 3. The C expression 'A' + 0x0c + 2 a. is illegal because of the b. has the decimal value 65 mixed modes in which the constants appear c. has the decimal value 79 d. has the decimal value 77 4. Suppose the following is executed: int i, j; scanf("%3d %3d", &i, &j); and the input that is typed in is: 1234567 89 0 What values will be assiged to i and j i = j = 5. Now suppose statement is changed to: scanf("%d %d", &i, &j); and the input that is typed in is: 1234567 89 0 What values will be assiged to i and j i = j = 6. Suppose an 8 bit integer has the binary value 11111111 Its 2's complement is: a. 00000000 b. 11111111 c. 11111110 d. 00000001 7. If a C program is to read from the standard input using fscan(stdin, "%d", &c); a. The program must contain a b. The program must contain a FILE *stdin; FILE stdin; declaration. declaration. c. The program must NOT declare stdin because it must be predeclared in stdio.h 8. Consider the following program fragment: unsigned int c; fscanf(stdin, "%d", &c); fprintf(stdout, "%d\n", c); Now suppose I run the program and type number 3 followed by a ctl-d (EOF). The value printed on my screen will be: a. 3 b. 51 c. c 9. Which best characterizes the action taken when reading a file with fscanf() and with fgetc() a. fscanf() may consume multiple b. both fscanf() and fgetc() bytes from the file in a single consume exactly one byte call, but a call to fgetc() will per call. consume at most 1 byte. c. both fscanf() and fgetc() consume multiple bytes per call. 10. The fgets() function a. reads a single byte from b. always reads a specified number a file of bytes from a file c. stops reading when it finds d. stops reading when it finds a a '\n' newline character or null character or exceeds the fills the specified buffer specified buffer space. space. Consider the following program: int y = 34; int q = 23; main() { int y = 52; static int r; int s; { int y = 77; printf("%d ", y); } printf("%d \n", y); } 11. In this program the 3 declarations of y will a. cause a syntax error for b. will result in all three duplication of declaration declarations referring to the same memory location. c. will result in there being three separate locations in which a variable named y is stored. 12. The output of the program will be: a. no output at all because b. 52 52 of compile time syntax error c. 77 52 d. 52 77 13. Which best characterizes the behavior of the following program: main() { int* p; *p = 99; printf("The value 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. 14. Which best characterizes the behavior of the following program: main() { float *pf; pf = (float *)malloc(4); scanf("%f", &pf); } a. The program will correctly b. The program is incorrect. read a single floating point The & in the scanf() call must into the malloc'd memory be deleted to fix it. c. The presence of the & is optional.. The program will read the value into the malloc'd memory either way. 15. Suppose a pointer declared as int* ptr; is being used in a loop to access adjacent integer values in a malloc'd area of memory. Which is the correct way to increment the pointer each time through the loop: a. ptr = ptr + 1; b. ptr = ptr + sizeof(int); c. *ptr = ptr + 4; d. *ptr = *ptr + sizeof(int); 16. Suppose a pointer declared as int *p has the value 0x1000 and the statement p = p + 1; is executed. The value will now be: a. 0x1000 b. 0x1001 c. 0x1004 d. 0x1008 17. The proper expression that should be used to access a pixel at position (row, col) in a binary encoded grayscale image is: a. *(imageloc + row * numrows + col) b. *(imageloc + col * numrows + col) c. *(imageloc + col * numrows + row) d. *(imageloc + row * numcols + col) 18. Consider the following definitions: struct pix { unsigned char r; unsigned char g; unsigned char b; }; struct pix* p1; struct pix p2; circle ALL of the following assignments that are syntactically correct: a. (*p1).r = 250; b. p1.r = 250; c. p2->r = 250; d. p2.r = 250; 19. Given the following structure definition, which of the following is syntactically correct. struct new a. v[1].a[1] = 1; b. v[0].b[1] = 1; { int a; c. v.b[1] = 1.5; d. v.a[1] = 1.5; float b[2]; }; struct new v[3]; 20. Given the following two structures: struct image struct pix { { int rows; unsigned char r; int cols; unsigned char g; struct pix pixmap[1024][768]; unsigned char b; }; }; the order in which the structure definitions appears in a program must be: a. struct image before b. struct pix before struct pix struct image c. Either way works fine 21. Given the following definition: struct image* p; Which is the correct way to set the value of the red component of the pixel at row 10 column 15 to 250. a. p.pix[10][15] = 250; b. p->pixmap[10][15] = 250; c. p[10][15] = 250; d. p->pix[10][15] = 250; 22. What value will be printed by the following program: int fix(int a) main() { { a = 12; int a = 11; return(13); fix(a); } printf("a = %d \n", a); } a = 23. The following program: void try_to_mod(struct large t); struct large_t main() { { double tab[64 * 1024]; try_to_mod(table); } table; } a. will not compile because it b. will actually pass a pointer is illegal to pass a structure to the structure when try_to_mod() as parameter is called. c. will cause 512K bytes of data to be copied on to the stack at the time of the call. 24. Suppose "funptr" is a properly declared pointer to functions having two int parameters and returning int. The correct way to cause it to point to "adder" is: a. funptr = adder; b. funptr = &adder; c. *funptr = adder; d. funptr = adder(); 25. After the pointer is correctly initialized, the proper way to invoke the function adder through the pointer is: a. s = funptr(3, 4); b. s = (*funptr(3, 4)); c. s = (*funptr)(3, 4); d. s = *(funptr)(3,4); 26. Suppose N and U are vectors and "a" is a scalar. Answer the follow V or S depending upon whether each of the following entities is a vector (V) or a scalar (S). ____ a. N dot U ____ b. N + U ____ c. a N ____ d. (N dot U) N 27. Suppose the location of the viewpoint is V and the world coordinates of point on the screen through which a ray passes is W. A unit vector in the direction of the ray is then given by a. V / ||W|| b. W / ||W|| c. (W - V) / || W - V || d. (V - W) / || V - W || Suppose that the viewpoint is on one side of the screen and a sphere is on the other. The distance from the viewpoint to the hitpoint is given by t = (-b +/- sqrt(b^2 - 4ac))/2a 28. The correct root of the equation to use in determining the hitpoint its a. t = (-b - sqrt(b^2 - 4ac))/2a b. t = (-b + sqrt(b^2 - 4ac))/2a c. the root which is positive should be used 29. The way in which it may be determined that the ray misses the sphere is that a. both values of t are negative b. 2a = 0 c. b^2 < 4ac d. t = 0 30. The location of the hitpoint H is given by H = B + tD where a. B represents the point where b. D represents the point where the ray passes through the the ray passes through the screen and D is a unit vector screen and B is a unit vector in the direction of the ray in the direction of the ray c. B represents the viewpoint d. D represents the viewpoint and D is a unit vector and B is a unit vector in the direction of the ray in the direction of the ray 31. The diffuse intensity the light reflected by an object at the hitpoint is a product of the emissivity of the light, the diffuse reflectivity of the object and the dot product of a. unit vectors pointing b. a unit normal at the object in the direction of the hitpoint and a unit vector light and the direction in the direction of the viewpt of the viewpt from the hit c. a unit normal at the object d. unit vectors pointing from the hitpoint and a unit vector light to the object and the in the direction of the light light to the viewpoint 32. (a continuation of 1) divided by: a. the distance from the light b. the distance from the light to to the viewpt the hitpoint c. the distance from the hitpoint to the object 33. If the dot product in #1 is negative it means that a. the hit object occludes itself b. the hit object is occluded by from the light another object c. the center of the light is visible from the object. 34. What values will the following program print out: main() { int c; int d; int a = 10; c = ++a; d = a++; printf("%d %d %d \n", a , c, d); } 35. What values will the following program print out: main() { int a, b; a = 4; b = (a > 3) ? (a -= 2) : (a += 3); printf("%d %d" \n, a , b); } 36. The correct approach to avoiding memory leaks is to free a malloc'd object a. whenever a function that has b. whenever the LAST pointer to the a local variable that points object is about to be reset or to to it returns go out of active scope. c. both of the above are true. 37. Assuming "lst" points to the head of one of our object lists, which best characterizes the following code fragment: obj_t *temp = malloc(sizeof(obj_t)); temp = lst->head; a. it will cause a memory leak b. it will cause a seg fault c. it is the correct way to set temp to point to the first object in the list. 38. Write a complete C language program that will read in a text file containing an arbitrary, but unknown, number of integer numbers and print out a single line containing the average value and the largest value (use integer aritmetic and don't worry about truncation versus rounding in computing the average). As usual the input should be read using redirection from standard input and the output should go to standard output. Sample input -1 5 2 Sample output 2 5