ee312_test1_spring2003

ee312_test1_spring2003 - EE 312 SP2003 Exam 1: NAME:...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 4
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 6
Background image of page 7
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: EE 312 SP2003 Exam 1: NAME: TA& Recitation Time: Section 1: Programs for you to write (25% total). For each problem in this section, complete the function so that it performs the specified behavior. For full credit, your program code must do exactly what is specified (no more, and no less) — e.g., if you’re not told to print something, then don’t print anything. Each question is worth 10 points in this section. You may not call any functions in your solutions (unless you also write the function). Please think before you write, and don’t make a mess. We can’t give you credit if we can’t read your answer! 1. (10 pts) Write a function that counts the number of perfect squares smaller than limit. The result should be stored in the variable num_squares. For example, if limit were equal to 30, then your program should set num_squares equal to 6 since there are six perfect squares smaller than 30 (they are, 0, l, 4, 9, l6 and 25). Recall that a perfect square is an integer whose square root is also an integer. For partial credit, write a flow chart for the function on the back of this page. int 1 imi t = ; / / some arbitrary non-negative integer int num_squares = O; / * define any other variables you think you need * / void countSquares<void) { / * your program here * / 2. Write both the functions described below a. (5 pts) Write the function isEven. The fimction should return a value representing “true” when the argument to the function is an even number. The function should return a value representing “false” otherwise. Note that zero is considered even. int isEven(int x) { /* your function here */ b. (10 pts) Write the function sumEvens that calculates the sum of all the even numbers in the array nums. Your function should store the result in the variable total. You can assume that nums has exactly 10,000 elements. You must use the function isEven correctly in your solution to receive full credit. Note that it is possible that none of the 10,000 elements will be even, or that some of them are even and some are odd, or even that all the elements are even. Your program should work for all of these cases. int nums [10 O O O] = / / an array, only the first N elements are actually used int total; /* declare any other variables you think you need */ void sumEvens (void) { / * your program here, don’t forget to use the isEven function */ Section 2: Program Analysis (55% total) For each of the programs in this section, analyze the program and explain what happens when the program is run. Not all of the programs in this section are well written — some may contain errors. For this section, you should simply indicate what the program will do as it is written. There is no partial credit in this section, so please be very careful. Some of the questions are puzzles that will require some deductive reasoning. The questions are worth 5 pts each in this section 3. (5 pts) What output is produced when this program is run? int main (void) { lntk=01 while (k == 0) { k=k+l, } printf(“k is %d\n”, k); } 4. (5 pts). What output is produced when this program is run? int main(void) { int k; if (k) { k = O; } printf(“k is %d\n”, k); } 5. (5 pts). What output is produced when this program is run? int main(void) { int k = 10; while(k < 10) { k = k + l; } printf(“k is %d\n”, k); } 6. (5 pts). What output is produced by the following program? int main(void) { int k = 0; while (k >= 0) { k = k * 2; k = k + l; } printf(“%d “, k); } 7. (5 pts). Approximately how many iterations are performed by the loop in the following program? Choose the best of the following options. int main(void) { int X = 1; while (X > 0) { X = x + l; } } . There is no bound to the number of iterations performed. This loop will iterate indefinitely. . The loop will not perform any iterations. . The loop will iterate approximately 32,000 times. . The loop will iterate approximately 1,000,000 times. . The loop will iterate approximately 2,000,000,000 times. DJ (DD-00" 8. (5 pts). If int variables are 32-bits long, and char variables are 8-bits long, what values will be output for x and y in the following program? int main(void) { int X = 0; char y = x; while (x = y) { x = x + l; y = X: } Theintxis __._____ printf(“x is %d\n”, x); printf(“y is %d\n”, y); } The charyis 9. (5 pts) What is the output of the following program. Be carefiil, the variable names were chosen very carefully to make it easy to make mistakes. void doit(int x, int y) { x=x+y; } int main(void) { int X = l; doit(x, 3); printf(“x is %d\n”, X); } 10. (5 pts) What is the output of the following program. Be careful, the variable names were chosen very carefully to make it easy to make mistakes. int x = O; void doit(void) { int X = 42; } int main(void) { doit(); printf(“x is %d\n”, X); } 11. (5 pts). What value is returned by this function. If you cannot give an exact answer, then express your answer as a mathematical function (e. g., give an expression like x2). Note that we promise that the parameter x will be greater than zero (if that matters). int func(int x) { int ans = 1; while (X > O) { ans = ans + ans; x = X — 1; } return ans; 12. (5 pts) What is the output of the following program. Assume that activation records are implemented as described in class. If under that assumption you still cannot determine the value that is output, then write “random” as your answer. void doit(int a, int b, int c, int d, int e) { int x = a + b + c + d + e; } void toit (void) { int X; printf(“x is %d\n”, x); } int main(void) { doit(5, 4, 3, 2, l); toit(); } 13. (5 pts) What is the output of the following program. Assume that activation records are implemented as described in class. If under that assumption you still cannot determine the value that is output, then write “random” as your answer. void doit(void) { inta= 1; int b = 2; intc=3; } void boost (void) { toit(); } void toit(void) { int b; printf(“b is %d\n”, b); } int main(void) { doit(); boost(); } Section 3: Questions Testing Your Programming Sense (20% total) 14. (5 pts) How many memory locations are required for the stack when the following program is executed. By “required” I mean the number of locations such that if the operating system allocated any fewer locations then the program could not run (there would not be enough room for all the activation records). Support your conclusion by drawing a picture of what the stack looks like when it is at its maximum size. Please assume that the activation record for main consists of exactly three memory locations. void fred(int x) { int k = 0; while (k < X) { k = k + 1; } } int main(void) { int k = 0; while (k < 1000) { fred(k); } } 15. (5 pts) Circle each of the following statements which are true. Draw an “X” through any statement which is false. Do not leave any statement blank. a. int x = ‘ a ’ ; / / is it legal to assign a character value to an int variable? b. char y = 42 ; / / is it legal to assign an ordinary number to a char value? C. \5! + \5/ :: ‘10! d. \al + l =: \bl e. \ZI _ \AI :: 16. (8 pts) Indicate if each of the following statements is True or False a. In C, the end of an array is always indicated by a NULL after the last array element. b. Any program that uses arrays could be rewritten as a program that uses only ordinary variables (no arrays) and be just as efficient (Please do not consider “pointers” when answering this question as we’ve not covered pointers yet in class.) c. In C, indexing into an array with a negative value or with a value beyond the end of the array is an error that will result in the program not compiling. d. The “linker” is the part of the system that creates the dynamic links used in an activation record. l7. (2 pts) Puzzler: For What value (or values) of x would the following expression be true x + !X != X ...
View Full Document

Page1 / 7

ee312_test1_spring2003 - EE 312 SP2003 Exam 1: NAME:...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online