# Midterm Exam A

This is version A of the Midterm Exam. The questions in the other versions are similar.

## True or False Questions, 1 point each

To practice taking this section. Select TRUE or FALSE in the pop-up menus after each question to record your answer. To submit your answers for grading, click on the "submit for grading" button.

On lynx, use down arrow to move to the next question or choice. Hit return to make your selection.

1. In the C programming language, variable names can have at most 8 characters.

2. Because - associates left to right, the expression 5 - 3 - 1 is equivalent to 5 - ( 3 - 1).

3. Because * has higher precedence than -, the expression 5 * 3 - 4 * 1 is equivalent to (5 * 3) - (4 * 1).

4. The fraction 1/8 can be stored exactly (without round-off or truncation error) in a double variable.

5. The value of the expression (n+1)%3 is 1 whenever n is a multiple of 3.

6. A function with return type void always returns a zero value.

7. When a function is called the number of actual parameters should match the number of formal parameters given in the function's prototype.

8. To use a function, the function's prototype must be in a header file and included using a #include directive.

9. After a function has returned, the values stored in its local variables are copied into variables in the main function with the same name.

10. When a return statement is executed inside a for loop, the statement immediately after the for loop is executed in the next step.

## Multiple Choice, 2 points each

To practice taking this section. Choose the best answer in the pop-up menu after each question to record your answer. To submit your answers for grading, click on the "submit for grading" button.

On lynx, use down arrow to move to the next question or choice. Hit return to make your selection.

1. The value of the following expression is:
```     6 - 4 * 1 + 3 % 2 / 5 - 7
```

a. 5
b. -5
c. -9
d. 9

2. Trace the execution of the following if statements and select the output from this code fragment.
```     int a = 3, b = 7 ;

if ( (a + b) % 2 != 0 ) {
printf("tetrahedron\n") ;
} else if ( a > b ) {
printf("cube\n") ;
} else if ( b - 4 > a ) {
printf("octahedron\n") ;
} else {
printf("dodecahedron\n") ;
}
```

a. tetrahedron
b. cube
c. octahedron
d. dodecahedron

3. What is the effect of the following for loop?
```     int n, k;

n = 0 ;
for (k = 2 ; k < 10 ; k = k + 2) {
n = n + k ;
}
```

a. The variable n contains the sum of the even numbers from 2 to 10 inclusive.
b. The variable k contains the sum of the even numbers from 2 to 10 inclusive.
c. The variable n contains the sum of the even numbers from 2 to 8 inclusive.
d. The variable k contains the sum of the even numbers from 2 to 8 inclusive.

4. Which of the following expressions has a side effect?

a. a < 5
b. 17 + 9 / (4 - 2 - 2)
c. n == m + 17
d. c++

5. The expression (13 / 2) + 2.0

a.has type double and value 8.5
b.has type int and value 8
c.has type double and value 8.0
d.has type int and value 8.5

6. According to DeMorgan's Law, the following Boolean expression is equivalent to which of the choices?
```     ! ( (x < 3) || (x >= 7) )
```

a. (x < 3) && (x >= 7)
b. (x >= 3) && (x < 7)
c. (x >= 3) || (x < 7)
d. (x < 3) && (x > 7)

7. To declare that the function basil takes an int parameter, a double parameter and does not return any values, we should use which of the following function prototypes?

a. void basil(int n, double x) ;
b. int basil(int, double) ;
c. basil(int, double) ;
d. (int double) basil(void) ;

8. When a break statement is executed as part of a switch statement:

a. The statements immediately after the nearest while loop is executed in the next step.
b. The statements in the default case are executed in the next step.
c. The statement immediately after the current case is executed in the next step.
d. The statement immediately after the switch statement is executed in the next step.

9. Let flag be a variable with type bool. After executing the following statement,
```     flag = (3 < 4) && (7 < 14)
```

a. The value of flag is TRUE.
b. The value of flag is FALSE.
c. The value of flag is garbage.
d. The value of flag is 7.

10. According to the rules of short circuit evaluation, when the following expression is evaluated
```     (3 < 4) || (a < b++)
```

a. The expression (3<4) is TRUE, so the expression (a < b++) is not evaluated.
b. Either the expression (3<4) or the expression (a < b++) could be evaluated first.
c. Both the expression (3<4) and the expression (a < b++) will be evaluated.
d. The effect of the expression depends on which compiler is used.

## Short Answers, 4 points each

This section cannot be graded on-line. To practice taking the exam, jot down your answer and follow the link after each question to see a sample solution.

• In the following questions, no syntax errors have put in deliberately. (So, "there is a syntax error" is not the right answer and will receive no credit.)
• In order to receive partial credit, you must show your work.

1. Write down the output of the following program fragment.
```     int i, j ;

for (i = 1 ; i <= 4 ; i++) {
for (j = 1 ; j <= 5 ; j++) {
if ( (i == 1) || (i == 4) ) {
printf("X") ;
} else if ( j == 5 ) {
printf("X") ;
} else {
printf("O") ;
}
}
printf("\n") ;
}
```
Sample Solution

2. Write a short program with a for loop that takes an integer value (call it n) from the user and prints out the first n odd numbers and the sum of the first n odd numbers. For example, if the user enters 3 as input, your program should print out:
```     1 + 3 + 5 = 9
```
If the user enters 9 as input, your program should print out:
```     1 + 3 + 5 + 7 + 9 + 11 + 13 + 15 + 17 = 81
```
Sample Solution

3. Write down the output of the following complete program. Show your work and clearly indicate your final answer.
```     #include <stdio.h>
int lennie (int, int) ;
int squiggy (int) ;

main() {
int a, b, c ;

a = 1 ;
b = 4 ;
c = squiggy(3) ;
a = lennie(b, c) ;
printf("main: a = %d, b = %d, c = %d\n", a, b, c) ;
}

int lennie (int a, int b) {
int c ;

c = squiggy(a - b) ;
printf("lennie: a = %d, b = %d, c = %d\n", a, b, c) ;
return(c) ;
}

int squiggy (int a) {
int b, c ;

b = 3 * a - 1 ;
c = b - a ;
printf("squiggy: a = %d, b = %d, c = %d\n", a, b, c) ;
return(b) ;
}
```
Sample Solution

4. Write a short program that takes an integer value (call it n) from the user and prints out a tower of numbers starting with n on the first line, then n-1 twice on the second line, then n - 2 three times on the third line, For example, if the user enters 3 as the input, your program should print out:
```     3
2 2
1 1 1
```
If the user enters 5 as the input, your program should print out:
```     5
4 4
3 3 3
2 2 2 2
1 1 1 1 1
```
Sample Solution

Last Modified: Thu Dec 7 04:44:58 EST 1995

Richard Chang, chang@gl.umbc.edu