Score Possible Points

Total Page:16

File Type:pdf, Size:1020Kb

Score Possible Points

CSCE 206 EXAM TWO Fall 2009

SID/SSN:

Score Possible Points

Part One 65 pts _____

Part Two 30 pts _____

Part Three 20 pts _____

Part Four 20 pts _____

Part Five 20 pts _____

Total 155 pts _____ PART ONE (65 points). Each question is worth 5 points. Circle the correct answer.

1. Which of the following is legal? int i=5, *p;

(a) printf("%d", *p); (b) p = *i; (c) p = (double *) 1307; (d) (double) p = 3.0;

2. Which of the following is WRONG for array initiation? (a) int a[]={3, 4, 5, 6}; (b) int a[4] = {1, 2}; (c) int a[2][3] = {1, 2, 3, 4, 5, 6}; (d) int a[2][3] = {{1, 2}, {3, 4}, {5, 6}};

3. Which of the following is WRONG for the initialization of Strings? (a) char s[] = {‘a’, ‘b’, ‘c’, ‘\0’}; (b) char s[] = “abc”; (c) char *p; p[0]= “abc”; (d) char p[2]; p[0]= ‘A’; p[1] = ‘\0’;

4. Which of the following is TRUE between Arrays and Pointers? int a[100], *p; (a) p = &a; (b) p = a[0]; (c) p = a+1 is equivalent to p = a[1] (d) p = a+1, *p = a[1]

5. Which of the following explanations is FALSE? (a) A call-by-value mechanism means a copy of the value of the expression is made and passed to the function. (b) A call-by-reference mechanism means the actual argument list in a function reference contains the true address of a variable sent to the function (c) You can initialize and modify a variable with type static const. (d) External and static variables that are not explicitly initialized by the programmer are initialized to zero by the system. 6. Which of the statements is true about the following code?

int main() { char a[7]= {‘c’,’p’,’s’,’c’,’2’,’0’,’6’}; char *p = a+2; } 1. p contains the address of the third array element 2. p contains the base address of the array 3. p contains the value of ‘p’ 4. p contains the value of ‘s’

(a) 1 only (b) 2 only (c) both 1 and 3 (d) both 1 and 4

7. Which of the following is FALSE? enum day {sun, mon=7, tue, wed, thu=4, fri, sat};

(a) the value of wed is 3 (b) the value of fri is 5 (c) enum is used to declare enumeration types (d) typedef enum day day; is a legal statement

8. Suppose our machine stores an int in four bytes. If a[0] is stored at location 100010, which of the following is NOT TRUE?

int a[5]={1,2,3,4,5}; int *p=a;

(a) The remaining array elements are stored at locations 100410, 100810, 101210, and 101610. (b) p holds the value stored in the first element. (c) p points at base of array a (d) p is equivalent to &a[0] 9. What is the output of this program? Line 1: int main(void) Line 2: { Line 3: char s1[] = "a brown cow", Line 4: s2[] = "t\0u\0v\0"; Line 5: printf("%s%s", s1, s2); Line 6: char* c = s1; Line 7: c+=3; Line 8: }

(a) a brown cow t\0u\0v\0 (b) a brown cow t (c) a brown cow tuv (d) at

Use the following program for the next THREE problems:

Line 1: void compute(int b[], int n, int* ans) // n is the size of the array Line 2: { Line 3: int i; Line 4: for (i=0; i

10. Given Line 1: void compute(int b[], int n, int* ans), which of the following is NOT TRUE?

(a) int b[] is equivalent to int *b (b) int b[] in the function definition means when an array is passed, the array elements are copied (c) int n in the function definition uses call-by-value mechanism. (d) int* ans in the function definition uses call-by-reference mechanism. 11. Which of the following is TRUE?

(a) Array b in Line 5 and Line 11 means the same one. (b) Change line 5 to ans += b[i] will not change the program (c) Change line 13 to printf("%d", ans) will get a compilation time error (d) Line 12 compute(a, 3, &answer) pass the base address to the compute function. We can rewrite line 5 to *ans += a[i] and get the same result.

12. What is the output of this program?

(a) 3 (b) 12 (c) 4 (d) 7

13. Given an array declaration int a[25][50]. What is the linear sequential element number of a[20][35]? PART TWO (30 points).

1. Fill in the table below with the correct values for each of the statements. (Assume the variables are initialized as shown and that each statement is executed independently with the same set of initialization statements.)

int a = 2, b = 3, c = 0, d = 1 ,*p = &a, *q = &b;

int k[] = {1,2,3};

int *t = &k[1]; t++; double *n;

Expression Value *t

b + * * & p / d

*p**q

2 * c % ++d + a--

*(t-1)

2. Given the declaration below, write a sequence of statements which will sum the array a using pointers. (i.e., no indices or subscripts).

int a[5]={1, 3, 5, 7, 9}

3. What is the output of the following code? #include void swap1 (int *m, int *n) { int temp = *m; *m = *n; *n = temp; } void swap2 (int m, int n) { int temp = m; m = n; n = temp; } int main (void) { int a[4] = {1,2,3,4}; int b[4] = {5,6,7,8}; int i, j;

for (i = 0; i < 3; ++i) if (a[i] < a[i+1]) swap1 (&a[i], &a[i+1]);

for (i = 0; i < 3; ++i) if (b[i] < b[i+1]) swap2 (b[i], b[i+1]);

for (j = 0; j < 4; ++j) printf ("%d ",a[j]);

printf ("\n");

for (j = 0; j < 4; ++j) printf ("%d ",b[j]);

return 0; }

PART THREE (20 points). Write a C function which will calculate, and return to the calling function, the greatest element (in magnitude) of a two dimensional array B, and also the position of that element (the indices in the array), given that the array declaration is as follows: double B[10][15]; PART FOUR (20 points).

Write a C function to calculate and return to the calling function the first 10 terms of the infinite series below and the sum of the series. Use a separate function to calculate the factorial. Put each individual term into an array element: i.e. store the i-th term in the (i-1) location in the array. For example, store the first term in array[0], second term in array[1], and so on.

exp(x) = 1 + x + x2 / 2! + x3 / 3! + x4 / 4! ...... PART FIVE (20 points).

Given the strings s1 and s2, write a function called 'strcatlen' that combines the functionality of strcat and strlen. (Note: strcat concatenates two strings and strlen calculates the length of a string.)

Recommended publications