Quiz 3

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. If p is a pointer variable, then the following statement will always add 4 to the address stored in p
```	     p = p + 1 ;
```

2. To declare an array called A with 10 integer elements, we use the declaration:
```	int A[11] ;
```

3. The type string defined in the course library is equivalent to an array of characters.

4. When an array is passed as a parameter, a separate copy of the array is given to the function.

5. 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 main disadvantage of using IthChar from the course library is:

a. The IthChar function does not check out-of-bounds errors.
b. The IthChar function is slower than using array notation.
c. The IthChar function is not compatible with standard C.
d. All of the above.

2. Suppose that sizeof(int) is 4, then the function call malloc(24) accomplishes which of the following, assuming that enough system memory is available?

a. reserves enough space for an array of 6 integers
b. reserves enough space for an array of 5 integers
c. reserves enough space for an array of 24 integers
d. reserves enough space for an array of 23 integers

3. Suppose that p is an integer pointer and A is an "honest to goodness" array of 10 integers, then
```	   p = A ;
```

a. will cause a syntax error when compiled.
b. checks whether p and A have the same value.
c. stores the address of A in p.
d. allocates memory so that p can be used as an array.

4. If ptr1 is a pointer to a double and ptr2 is a pointer to an integer, then the statement
```	   ptr2 = ptr1 ;
```

a. will cause a syntax error when compiled.
b. will truncate the value in ptr1 and store it in ptr2.
c. will cause a segmentation fault when the statement is executed.
d. none of the above

5. Let str be a string variable, then after the assignment
```	   str = "Gilbert and Sullivan" ;
```

a. the expression str[5] refers to the string "ert and Sullivan".
b. the expression str[5] refers to the string "rt and Sullivan".
c. the expression str[5] refers to the character 'e'.
d. the expression str[5] refers to the character 'r'.

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.) Please mark your final answer clearly. In order to receive partial credit, you must show your work.

In each of the following questions, 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.

1. Parameter passing by reference.
```#include <stdio.h>
int dragoon (int, int *) ;

int dragoon (int a, int *p) {
int b ;

b = *p + a ;
*p = b / a ;
printf("dragoon: a = %d, b = %d, *p = %d\n", a, b, *p) ;
return(b) ;
}

main() {
int a = 13, b = 2, c = 9 ;

a = dragoon(b, &c) ;
printf("main: a = %d, b = %d, c = %d\n", a, b, c) ;
}

```

2. Using an array parameter.
```#include <stdio.h>
int yeoman(int []) ;

int yeoman(int A[]) {

A[1] = 9 ;
A[2] = 18 ;
return(-1) ;
}

main() {
int X[4] ;

X[3] = yeoman(X) ;

printf("main: %d, %d, %d\n", X[1], X[2], X[3]) ;
}

```

3. Pointer manipulations.
```#include <stdio.h>

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

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

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

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

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