Computer Science 215 Test 1 Name ______________________ 1. The C expression 'A' + 0x0d - 3 a. is illegal because of the b. has the decimal value 73 mixed modes in which the c constants appear c. has the decimal value 75 d. has none of the above values 2. Which best characterizes the behavior of the following statements x = y; if (x = 1) printf("%d\n", x); a. the "if" statement will b. The print statement will c cause a syntax error. never be executed c. the print statement will d. the print statement will be always be executed executed only if the value of "y" is non-zero. 3. Which is the correct way to test for end of file when reading in two integers at a time using scanf() a. while (scanf("%d %d", &i, &j)>= 0) b. while (scanf("%d %d", &i, &j)==2) b c. while (scanf("%d %d", &i, &j) != EOF) 4. If a C program is to read from the standard input using fscanf(stdin, "%d", &c); a. The program must contain a b. The program must contain a FILE *stdin; FILE stdin; declaration. declaration. c c. The program must NOT declare stdin because it must be predeclared in stdio.h 5. Which of the following format codes causes no conversion of the data being read or written with printf or scanf: a. %d b. %x c c. %c d. %f 6. The fgets() function a. reads a single byte from b. always reads a specified number a file of bytes from a file c c. stops reading when it finds d. stops reading when it finds a a '\n' newline character or null character or fills the fills the specified buffer specified buffer space. space. 7. Consider the following program fragment: unsigned int c; c = fgetc(stdin); fprintf(stdout, "%c\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: (Hint: the ASCII encoding of the character '3' is 0x33 = 51.) a. 3 b. 51 a c. c d. \n 8. Fill in the FINAL values in the table below after ALL of the code shown has completed execution: x = 22; name value address y = 9; ------------------------------ p = &y; int x | 22 | 120 r = &x; 4pts ------------------------------ *p = *r; int y | 22 | 116 *r = x; ------------------------------ p = r; int *p | 120 | 112 ------------------------------ int *r | 120 | 108 ------------------------------ 9. 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 c fine in all environments before the printf() is executed c. The program is incorrect, but it may appear to work correctly. 10. The proper way to use fread to copy stdin to stdout until end of file is: a. while (len = (fread(buff, 1, 1024, stdin) != 0)) fwrite(buff, 1, len, stdout); b b. while ((len = fread(buff, 1, 1024, stdin)) != 0) fwrite(buff, 1, len, stdout); c. while (len = fread(buff, 1, 1024, stdin) != 0) fwrite(buff, 1, len, stdout); d. All of the above are equivalent and correct. 11. Which best characterizes the behavior of the following program: main() { int *pi; pi = (int *)malloc(4); scanf("%f", &pi); } a. The program will correctly b. The program is incorrect. read a single floating point The & in the scanf() call must b 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. 12. 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 pi is declared as: int *pi; a. pi = (int *)malloc(4 * 100); b. pi = (int *)malloc(100); a c. pi = (int *)malloc(4 * sizeof(int)); 13. 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. pi = pi + sizeof(int); c. pi = pi + 100; d b. pi = pi + 4; d. pi = pi + 1; 14. The proper way to print the value of the current integer is: a. printf("%d \n", &pi); b. printf("%d \n", pi); c c. printf("%d \n", *pi); d. printf("%d \n", **pi); 15. 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 c. (*p2).r = 250; d. p1 = 250; 17. Given the following structure definition, which of the following are syntactically correct. struct new a. v[1].a[1] = 1; b. v[0].b[1] = 1; { b int a; c. v.b[1] = 1.5; d. v.a[1] = 1.5; float b[2]; }; struct new v[3]; 16. The correct way to access the "green" component of an rgb pixel stored at (row, col) in an image buffer pointed to by unsigned char *buf is: a. *(buf + row * numcols + 3 * col + 1); b. *(buf + 3 * row * numcols + 3 * col); e c. *(buf + row * numcols + col + 1); d. *(buf + 3 * row * numrows + 3 * col); e. none of the above 18. Compute the dot product of (1, -1, 1) dot (2, -1, 3) = 6 19. Compute the length of the vector (-1, 2, 1) (No calculators are needed -- you may leave the square root symbol in your answer). || (3, 4, 1) || = sqrt(26) Suppose that a raytracing scene object has been properly typedef'd as an obj_t and a list header structure is defined as follows: typedef struct list_type { obj_t *head; obj_t *tail; } list_t; 20. Which of the following is the correct way to add a new object pointed to by obj_t *new to the tail of a NON-EMPTY list. a. tail = new; b. new->next = tail; c tail->next = new; next = new; c. tail->next = new; d. head = tail; tail = new; tail = new; 21. Suppose V is the view point and W is the world coordinates of the point on the screen through which the ray passes. A unit vector in the direction of the ray is given by: a. V / || V || b. W / || W || d c. (V - W) / || (V - W)|| d. (W - V) / || (V - W) || 22. If the coordinates of the point at which the ray hits the object is H the distance from the viewpoint to the hitpoint is given by: a. H b. (H - V) c c. || (H - V) || d. (W - V) 23. Suppose a plane in 3 dimensional space has normal vector N and the P and Q are two different points that both lie in the plane. Which of the following will always have the value 0. a. N dot P b. P dot (N - Q) d c. P dot Q - N d. N dot (P - Q) 24. Suppose a ray is fired from viewpoint V with unit direction vector D. The ray is parallel to the plane if: a. V dot D = 0 b. V dot N = 0 c c. D dot N = 0 d. (N - D) dot V = 0 25. Suppose a plane has normal (0, 0, 1) and the point (1, 1, -5) lies on the plane. A vector is fired from viewpoint (3, 4, 3) with direction (0, 0, -1). What is the distance, t, to the hitpoint. (Hint: there are two ways to do this: the hard way is to try to remember and/or derive the formula and do a bunch of dot products; the easy way is to think about where the plane is located and oriented and compute the answer in your head via ONE scalar subtraction operation.) 8 26. What value will be printed by the following program: int fix(int a) main() { { a = 11; int a = 12; return(13); fix(a); } printf("a = %d \n", a); a = 12 27. What value will be printed by the following program: int fix(int *a) main() { { *a = 13; int a = 12; return(11); a = fix(&a); } printf("a = %d \n", a); } a = 11 28. Which of the following best characterizes what happens when a program attempts to invoke a function that is defined in another module but for which no prototype definition is supplied: (suppose main.c invokes function adder() in adder.c and the compile command is: gcc -g main.c adder.c) a. it will cause a syntax error b. it will cause an error at link at compile time because the time because the linker won't function name is not defined be able to find adder() d c. it will always work fine d. it will only work if adder() returns int and its parameters are of the correct type 29. 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; a c. *funptr = adder; d. funptr = adder(); 30. 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 c. s = (*funptr)(3, 4); d. s = *(funptr)(3,4); 31. We showed that the hit equation for the sphere could be reduced to a*t^2 + bt + = 0. 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 c. b^2 < 4ac 32. 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 c 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 33. (a continuation of 1) divided by: a. the distance from the light b. the distance from the light to to the viewpt the hitpoint b c. the distance from the hitpoint to the object 34. 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 a c. the center of the light is visible from the object. 35. What is the result when the matrix and vector shown below are multiplied: | 1 0 -1 | |1| -1 | 1 1 1 | |1| = 4 | 1 -1 -1 | |2| -2 36. The cross product of (0, 1, 0) x (1, 0, 0) is: a. (1, 1, 0) b. (0, 0, 1) c c. (0, 0, -1) d. (0, 1, 1,) 37. The projection of a unit vector V on another unit vector N is given by: a. V dot N b. (N dot V) N b c. (V dot N) V d. (N x V) N 38. Characterize the following implmentation of vl_unitvec3(): (assume vl_length3() is working correctly) vl_unitvec3(double *v1, double *v2) { *(v2 + 0) = *(v1 + 0) / vl_length3(v1); *(v2 + 1) = *(v1 + 1) / vl_length3(v1); *(v2 + 2) = *(v1 + 2) / vl_length3(v1); } a. it never works correctly b. it always works correctly d c. it will never work correctly d. it may or may not work correctly if v1 and v2 point to the if v1 and v2 point to the same vector same vector, depending on the value of the vector 39. Write a complete C language program that will read in a text file containing an arbitrary, but unknown, number of pairs integer values. It is known that the number of pairs is less than or equal to 100 pairs. The SUM of each pair should be printed out in the REVERSE of the order in which the integers were read in . As usual the input should be read using redirection from standard input and the output should go to standard output. Sample input Sample output -1 3 6 5 9 3 2 1 14 6 0 2 Hint: a correct implementation can be accomplished with 4 declarations and 4 lines of executable code. Deductions may be made for unnecessarily long and/or convoluted programs. #include main() { int count = 0; int *sums = alloca(100 * sizeof(int)); int x, y; while (scanf("%d %d", &x, &y) == 2) *(sums + count++) = x + y; while (--count >= 0) printf("%d\n", *(sums + count)); }