Computer Science 215 Quiz 1 Name ______________________ 1. The number of binary digits that comprise a single byte of computer memory is: a. 1 b. 2 c. 8 d. 255 2. The number of distinct values that a single byte of computer memory may take on is: a. 2 b. 8 c. 255 d. 256 3. 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 4. On 32 bit architectures such as the Intel and Sparc a C variable of type "short" is typically a. 8 bits b. 16 bits d. 32 bits d. 64 bits 5. The C expression 'A' + 0x0a + 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 75 d. has the decimal value 77 ----- PLEASE ALSO WRITE YOUR NAME OF THE BACK OF THE PAPER ------ ----- PLEASE DO NOT FOLD THE PAPER ------ ----- You may leave when you finish ------ Computer Science 215 Quiz 2 Name ______________________ 1. The floating point constant 2.5e+2 means a. 2.5 squared b. 2.5 * 2 squared c. 2.5 * 2 to the 10th power d. 2.5 * 10 squared 2. To read an integer value into the variable defined as char c; which of the following should be used: a. scanf("%c", &c); b. scanf("%d", &c); c. scanf("%c", c); d. scanf("%d", c); 3. To print the ASCII character (A, B, C, etc.) of the value declared as int c; which should be used: a. printf("%c", &c); b. printf("%d", &c); c. printf("%c", c); d. printf("%d", c); 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. Suppose the following code is executed: is: int i, j; scanf("%d %d", &i, &j); 2 A 4 a. i will be set to 2 b. i will be set to 2 j will be set to 65 j will be set to 4 c. i will be set to 2 d. neither i nor j will j will not be set at all be set Computer Science 215 Quiz 3 Name ______________________ 1. Suppose an 8 bit integer has the binary value 10110110 Its 2's complement is: a. 10110111 b. 01001001 c. 01001010 d. 01001000 2. 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 3. Consider the following program fragment: unsigned char c; c = fgetc(stdin); 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: (Hint: the ASCII encoding of the character '3' is 0x33 = 51.) a. 3 b. 51 c. c 4. 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 5. 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. Computer Science 215 Quiz 4 Name ______________________ 1. 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. 2. The fputs() function a. writes a single byte from b. always writes a specified number a file of bytes to a file c. stops writing when it finds d. stops writing when it finds a a '\n' newline character in null character in the buffer. the buffer. Consider the following program: int y = 34; int q = 23; main() { int y = 77; static int r; int s; { int y = 52; printf("%d ", y); } printf("%d \n", y); } 3. 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. 4. 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 5. In the above program which of the following sets of variables will all be stack resident: (circle all sets in which all of the variables are allocated on the stack) a. q, r, and s b. s c. r and s d. q and r Computer Science 215 Quiz 5 Name ______________________ 1. 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. 2. 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. 3. 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); 4. Suppose two pointer variables are declared as: char* p1; double* p2; In the Intel X86 architecture the amount of storage that will be allocated to hold the variables p1 and p2 is: a. 1 byte for p1 and b. 1 byte for both p1 and p2 8 bytes for p2 c. 4 bytes for p1 and 4 bytes for p2 5. 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 Computer Science 215 Quiz 6 Name ______________________ 1. In the last class, procedures for reading a file of image data into a dynamically allocated area of storage were discussed. In this procedure, the proper order in which the required system calls should be made is: a. fread() should be called to b. malloc() should be called to read the data before malloc() allocate the storage before is called to allocate the fread() is called to read in storage. the data. c. the program will work correctly regardless of the order in which fread() and malloc() are called. 2. The number of bytes per pixel that must be read in for an image that is encoded as described in class is: a. 1 for both the rgb and b. 3 bytes per pixel for grayscale grayscale images in the 1 byte per pixel for rgb examples discussed in class c. 3 bytes per pixel for both d. 1 byte per pixel for grayscale grayscale and rgb and 3 bytes per pixel rgb. 3. 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) 4. The Floyd-Steinberg algorithm is used to convert a. color images to grayscale b. grayscale images to black/white c. color images to black/white d. black/white images to color 5. The general class to which the Floyd-Steinberg algorithm belongs is: a. half-baked b. half-toning c. half-stoned d. half-fast Computer Science 215 Quiz 7 Name ______________________ 1. 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. 2. 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); 3. 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 4. 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. p2.r = 250; c. (*p1).r = 250; d. p1.r = 250; 5. Suppose two pointer variables are declared as: char* p1; double* p2; In the Intel X86 architecture the amount of storage that will be allocated to hold the variables p1 and p2 is: a. 1 byte for p1 and b. 1 byte for both p1 and p2 8 bytes for p2 c. 4 bytes for p1 and 4 bytes for p2 Computer Science 215 Quiz 8 Name ______________________ 1. Given the following structure definition, which of the following is syntactically correct. struct new a. v[2].a = 1; b. v.a[1] = 1; { int a; c. v.b[1] = 1.5; d. v[1].b = 1.5; float b[2] }; struct new v; 2. Given the following structure definition, which of the following is syntactically correct. struct new a. v[1].a = 1; b. v.a = 1; { int a; c. v.b[1] = 1.5; d. v.b[1] = 1.5; float b; }; struct new v[2]; 3. 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]; 4. 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 5. 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[10][15] = 250; b. p->pix[10][15] = 250; c. p.pix[10][15] = 250; d. p->pixmap[10][15] = 250; Computer Science 215 Quiz 9 Name ______________________ 1. 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 = 2. What value will be printed by the following program: int fix(int a) main() { { a = 11; int a = 12; return(13); a = fix(a); } printf("a = %d \n", a); } a = 3. 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 = 4. 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. 5. In the standard ray tracing paradigm a. the screen is between the b. the scene is between the viewer viewer and the scene and the screen c. the viewer is between the screen and the scene. Computer Science 215 Quiz A Name ______________________ 1. 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() c. it will always work fine d. it will only work if adder() returns int and its parameters are of the correct type 2. In the design proposed last class for the ray tracer a. structures of type sphere_t b. structures of type obj_t were linked via a pointer were linked via a pointer called "next". called "next" c. the pointer "next" was used to d. the pointer "next" was used point from obj_t to sphere_t to point from sphere_t to obj_t 3. 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(); 4. 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); 5. Which of the following is the best approximation of the actual score of the Clemson FSU game saturday. The objective function to be minimized is: | pred(clem) - actual(clem)| + |pred(fsu) - actual(fsu)| a. Clemson 45 b. Clemson 14 FSU 14 FSU 10 c. FSU 17 d. FSU 38 Clemson 6 Clemson 6 Computer Science 215 Quiz B Name ______________________ 1. Compute the vector sum of (1, 2, 4) + (2, -1, 3) = 2. Compute the dot product of (1, 2, 4) dot (2, -1, 3) = 3. What vector is obtained when (2, -2, 4) is multiplied by the scalar value 3? 3 (2, -2, 4) = 4. Compute the length of the vector (1, -2, 3) (No calculators are needed -- you may leave the square root symbol in your answer). || (1, -2, 3) || = 5. The lighting model to be used in the FIRST milestone that will actually create a ray traced image is a. ambient b. diffuse c. specular d. confused Computer Science 215 Quiz C Name ______________________ 1. 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 2. 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 3. 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 4. 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 5. Suppose B represents the base of a ray, D its direction, Q a point on a plane and N a normal to the plane. The ray will hit the plane in EXACTLY one spot unless a. N dot B = 0 c. N dot D = 0 c. N dot Q = 0 Computer Science 215 Quiz D Name ______________________ 1. 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 2. (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 3. 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. 4. 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); } 5. What values will the following program print out: main() { int a, b; a = 4; b = (a > 5) ? (a -= 2) : (a += 3); printf("%d %d" \n, a , b); } Computer Science 215 Quiz E Name ______________________ 1. 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 || 2. 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 3. If a C program mallocs an area of memory and fails to free it, a. A garbage collector built b. The OS will reclaim all of the into the C language will memory used by the program including reclaim it unfreed malloc'd areas when the program exits. c. The memory will remain unusable d. The memory will remain permanently until the system is rebooted allocated and the computer will eventually have to be sent back to the factory for memory replacement 4. 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. 5. 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. Computer Science 215 Quiz F Name ______________________ 1. Recursion is required in the raytracer to properly handle a. ambient light b. diffuse light c. specular light d. all of the above 2. When a light ray bounces off of the object which of the following should be <> from testing to see what the ray hits next a. any light b. any plane c. the object the ray just d. all of the above bounced off of 3. Suppose U is a unit vector in the reverse of the incoming direction of a light ray and N is a unit normal to the surface at the point at which the ray hits. An outgoing unit vector in the direction of the reflection is given by: a. 2 (U dot N) U - N b. 2 (U dot N) N - U c. 2 (N dot N) U - U d. 2 (U dot U) N - U