# Final Exam, Short Answer Section

## Short Answers, 4 points each

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

1. This question tests your knowledge of global variables and lexical scoping. What is the output of the following program?
```#include <stdio.h>

/* Global Variables */
int w = 100, x = 101, y = 102, z = 103 ;

int towers(int) ;

main() {
int w = 1 , x = 3 ;

printf("before: w = %d, x = %d, y = %d, z = %d\n", w, x, y, z) ;
w = towers(y) ;
printf("after: w = %d, x = %d, y = %d, z = %d\n", w, x, y, z) ;
}

int towers(int x) {
int y ;

y = x + 5 ;
z = z + y ;
w = z - x ;
printf("during: w = %d, x = %d, y = %d, z = %d\n", w, x, y, z) ;
return(y) ;
}
```

2. This question tests your knowledge of nested for loops. Please put carriage returns in the proper places. What is the output of the following code?
```      int i, j;

for (i = 0; i <= 3; i++) {
for (j = 1; j <= 4; j++) {
if ( (i + 1 == j) || (j + i == 4) ) {
printf("+") ;
} else {
printf("o") ;
}
}
printf("\n") ;
}
```

3. This question tests your knowledge of pointers and parameter passing by reference. What is the output of the following program?
```#include <stdio.h>

int ents(int *, int *) ;

main() {
int a = 1, b = 2, c = 3 ;

a = ents(&b, &c) ;
printf("First Call: a = %d, b = %d, c = %d\n", a, b, c) ;

c = ents(&a, &b) ;
printf("Second Call: a = %d, b = %d, c = %d\n", a, b, c) ;
}

int ents(int *p1, int *p2) {
int result, *q ;

q = p1 ;
*p1 = *p2 * 2 ;
p1 = p2 ;
*p1 = *q + 5 ;
result = *p1 + *p2 ;
return(result) ;
}
```

4. This question tests your ability to work with characters and strings. What is the output of the following program?
```#include <stdio.h>
#include "genlib.h"
#include "simpio.h"
#include "strlib.h"

main() {
string str, new ;
int i, length ;
char c ;

str = "Smaug the Worm" ;
new = "" ;
length = StringLength(str) ;
for(i = 0 ; i < length ; i++) {
c = IthChar(str,i) ;
if (isalpha(c)) {
c = tolower(c) ;
c = c + 7 ;
if (c > 'z') c = c - 26 ;
}
new = Concat(new, CharToString(c)) ;
}
printf("new = %s\n", new) ;
}
```

5. Write a simple loop which prints out the following sequence of numbers:
```      18 23 28 33 38 43 48
```