## 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.
• There are no intentional syntax errors in this section. So, ``Syntax Error --- no output is produced'' is not the correct answer for any of the questions. No credit will be given for such a response.
• If you need more space use the back of the pages.

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 a = 2, b = 4, c = 8 ;

int foobar(int b) {
int a ;

a = b + c ;
b = 2 * a ;
c = b + 1 ; ;
printf("foobar: a = %d, b = %d, c = %d\n", a, b, c) ;
return(b) ;
}

main() {
a = foobar(c) ;
printf("main: a = %d, b = %d, c = %d\n", a, b, c) ;
}
```

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 <= 2; i++) {
for (j = 0; j <= 2; j++) {
if ( i * i < j )
printf("X") ;
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?
```  int f(int x, int *p, int *t) {

x = x + 1 ;
*p = 2 * *p ;
*t = 3 * *t  ;
x = x + *p + *t ;
return(x) ;
}

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

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

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

}
```

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() {
int i, start, stop  ;
string str ;

str = "Our zip code is 21228." ;

for (i = 0 ; str[i] != '\0' ; i++) {
if ( str[i] > 'w' ) break ;
}
start = i ;
for (i = start ; str[i] != '\0' ; i++) {
if ( str[i] >='0' && str[i] <= '9' ) break ;
}
stop = i ;
printf("%s\n", SubString(str, start, stop) ) ;
}
```

5. Write a simple loop which prints out the following sequence of numbers:
```      4 8 16 32 64 128
```

6. Rewrite the following loop without using the break statement. (Your code should be equivalent to the given code.)
```  string str ;
int i ;

i = 0 ;
str = GetLine() ;
while ( str[i] != '\0' ) {
if (str[i] == 'a') break ;
i = i + 1 ;
}