Вы находитесь на странице: 1из 7

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, 1, 4, 9, 16 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 limit = … ; // some arbitrary non-negative integer


int num_squares = 0;
/* 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 function 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[10000] = …; // 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) {
int k = 0;
while (k == 0) {
k = k + 1;
}
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 = 0;
}
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 + 1;
}
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 + 1;
}
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 + 1;
}
}
a. There is no bound to the number of iterations performed. This loop will iterate
indefinitely.
b. The loop will not perform any iterations.
c. The loop will iterate approximately 32,000 times.
d. The loop will iterate approximately 1,000,000 times.
e. The loop will iterate approximately 2,000,000,000 times.

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 + 1;
y = x;
} The int x is
printf(“x is %d\n”, x);
printf(“y is %d\n”, y);
} The char y is
9. (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.

void doit(int x, int y) {


x = x + y;
}

int main(void) {
int x = 1;
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 = 0;
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 > 0) {
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, 1);
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) {
int a = 1;
int b = 2;
int c = 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. „a‟ + 1 == „b‟

e. „Z‟ – „A‟ == 25

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.

17. (2 pts) Puzzler: For what value (or values) of x would the following expression be true
x + !x != x

Вам также может понравиться