# Final Exam A

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

## True or False Questions, 1 point each

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

1. The value of the expression n%2 is always 0 when n is a multiple of 14.

2. Because - associates left to right, the expression 4 - 2 - 2 is equivalent to (4 - 2) - 2.

3. Let flag be a Boolean variable, then the following statement assigns TRUE to flag:
``` flag = (17 > 9) && (2 < 1) ;
```

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

5. A function with return type void does not return any values.

6. The constants "x" and 'x' may be used interchangeably.

7. The string "Milano" uses 6 bytes of memory.

8. The function call IthChar("Palermo", 6) will return the character 'm'.

9. The keyword typedef is used to define new types.

10. Suppose that C is an array of 7 integers, then the expression C[7] refers to the last element of the array.

11. If p is a character pointer, then the following statement will add 1 to the address stored in p
` p = p + 1 ; `

12. You need to have #include "strlib.h" when you use functions like Concat because the compiler needs to know that Concat returns a string.

13. The expression *&*&p is equivalent to the expression *p.

## Multiple Choice, 2 points each

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:
```     10 - 9 - 8 * 7 / 6 + 5 + 4
```

a. -1
b. 0
c. 1
d. 2

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

if ( a > b ) {
printf("Juventus\n") ;
} else if ( 4 * a > b ) {
printf("Roma\n") ;
} else if ( b - 4 <= a ) {
printf("Napoli\n") ;
} else {
printf("Lazio\n") ;
}
```

a. Juventus
b. Roma
c. Napoli
d. Lazio

3. What is the effect of the following for loop?
```     int i, x;

x = 0 ;
for (i = 0 ; i <= 10 ; i += 1) {
printf("%d\n", i) ;
x = x + i ;
}
```

a. It prints out the numbers 0 through 10 inclusive..
b. It prints out the numbers 1 through 10 inclusive.
c. It prints out the numbers 0 through 9 inclusive.
d. It prints out the sum of the numbers from 0 through 10 inclusive.

4. The expression (7 + 4) / 4 + 1

a. has type double and value 3.75
b. has type int and value 3
c. has type double and value 3.0
d. has type int and value 4

5. According to DeMorgan's Law, the following Boolean expression is equivalent to which of the choices?
```     ( (x >= 3) && (x <= 27) )
```

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

6. To declare that the function Torino takes a string parameter str and a pointer to integer parameter ptr and returns an integer, we use which of the following function prototypes?

a. int Torino(string str, int * ptr) ;
b. int Torino(string str, int ptr) ;
c. Torino(string str, int * ptr, int return) ;
d. void Torino(string str, int * ptr) ;

7. According to the rules of short circuit evaluation, when the following expression is evaluated
```     (4 > 7) && (x++ < 2)
```

a. Either the expression (4 > 7) or the expression (x++ < 2) could be evaluated first.
b. Both the expression (4 > 7) and the expression (x++ < 2) will be evaluated..
c. The expression (4 > 7) is FALSE, so the expression (x++ < 2) is not evaluated.
d. The expression (4 > 7) is TRUE, so the expression (x++ < 2) will be evaluated.

8. The function call SubString("Dino_Zoff", 3, 4) returns

a. the string "o_Zo"
b. the string "no_Z"
c. the string "no"
d. the string "o_"

9. Suppose that sizeof(int) is 4 and sizeof(double) is 8. Then the function call malloc(40) accomplishes which of the following, assuming that enough system memory is available?

a. reserves enough space for an array of 40 integers
b. reserves enough space for an array of 40 characters
c. reserves enough space for an array of 9 integers
d. reserves enough space for an array of 4 doubles

10. Suppose that A and B are "honest to goodness" arrays of 10 integers, then the statement
```    A = B ;
```

a. will cause a syntax error when compiled.
b. checks whether A and B have the same value.
c. stores the address of B in A.
d. copies each element of B into the corresponding element of A.

11. Suppose that ptr1 and ptr2 are integer pointers, then the statement
```     ptr2 = ptr1 ;
```

a. will cause a syntax error when compiled.
b. copy the integer value that ptr1 points to into the location that ptr2 points to.
c. will cause a segmentation fault when the statement is executed.
d. copy the address in ptr1 into ptr2.

12. Let str be a string variable, then after the assignment
```     str = "Rogers and Hammerstein" ;
```

a. the expression str[9] refers to the character 'n'.
b. the expression str[9] refers to the character 'd'.
c. the expression str[9] refers to the string "nd Hammerstein".
d. the expression str[9] refers to the string "d Hammerstein".

## Short Answers, 4 points each

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

1. Nested loops: write down the output of the following program fragment.
```   int i, j ;
for (i = 1 ; i < 5 ; i++) {
for (j = 0 ; j <= 5 ; j++) {
if ( (i + j) % 2 == 0 ) {
printf("X") ;
} else {
printf("O") ;
}
}
printf("\n") ;
}
```
Sample Solution

2. Pointer manipulations: write down the output of the following program.
```   #include <stdio.h>

main() {
int a = 1, b = 2, c = 3 ;
int *ptr1, *ptr2, *ptr3;

ptr1 = &a ;
ptr2 = &b ;
ptr3 = &c ;

*ptr1 = *ptr2 + *ptr3 ;
printf("a = %d, b = %d, c = %d\n", a, b, c) ;

*ptr2 = *ptr1 + b ;
printf("a = %d, b = %d, c = %d\n", a, b, c) ;

ptr3 = ptr1 ;
printf("a = %d, b = %d, c = %d\n", a, b, c) ;

*ptr1 = *ptr2 + *ptr3 ;
printf("a = %d, b = %d, c = %d\n", a, b, c) ;
}
```
Sample Solution

3. Parameter passing by reference: write down the output of the following program.
```   #include <stdio.h>

int palermo (int *, int *) ;

int palermo (int *p, int *q) {
int b ;

b = *p ;
*p = *q + 5 ;
*q = b + 5 ;
printf("palermo: b = %d, *p = %d, *q = %d\n", b, *p, *q) ;
return(b) ;
}

main() {
int a = 1, b = 7, c = 4 ;
a = palermo(&b, &c) ;
printf("main: a = %d, b = %d, c = %d\n", a, b, c) ;
}
```
Sample Solution

4. The following program contains several global variables. Trace through the program and write down the output of the program. It is important that you write down the output in the order that they would appear when the program is executed.
```   #include <stdio.h>

int bologna(int) ;

int a = 1, b = 2, c = 3, d = 4, e = 5 ;

int bologna(int c) {
int e ;

a = c + 1 ;
b = c + 2 ;
c = c + 3 ;
d = c + 4 ;
e = c + 5 ;
printf("bologna: a=%d, b=%d, c=%d, d=%d, e=%d\n", a, b, c, d, e) ;
return(e) ;
}

main() {
int b = 10, c = 11 ;

printf("main: a=%d, b=%d, c=%d, d=%d, e=%d\n", a, b, c, d, e) ;
c = bologna(a) ;
printf("main: a=%d, b=%d, c=%d, d=%d, e=%d\n", a, b, c, d, e) ;
}
```
Sample Solution

5. Write a short program that takes an integer value (call it n) from the user and prints out a square of numbers starting with 1 in the top left corner, 2 in a ring around the 1, 3 in a ring around the 2's, For example, if the user enters 3 as the input, your program should print out:
```   1 2 3
2 2 3
3 3 3
```
If the user enters 5 as the input, your program should print out:
```   1 2 3 4 5
2 2 3 4 5
3 3 3 4 5
4 4 4 4 5
5 5 5 5 5
```
Sample Solution

6. A perfect number is a positive integer which is equal to the sum of its divisors (not counting the number itself). For example, the number 6 is a perfect number because 1, 2 and 3 are the divisors of 6 and 1+2+3 = 6. Similarly, the number 28 is a perfect number because 1+2+4+7+14 = 28.

Write a function PerfectNumber which takes an integer parameter and returns TRUE if the parameter is a perfect number. If the parameter is not a perfect number, the function returns FALSE.

7. Write a function called DoubleLetter which takes a string parameter and returns an integer value. The value returned by the function is the index of the first character that appears consecutively in the string. If no such character is found, then DoubleLetter returns -1. For example, DoubleLetter("abcdefgghi") should return the value 6 and DoubleLetter("Attila the Hun") should return the value 1.