Unformatted text preview: CS410 Spring 2008 Sample Questions ( with answers ) Exam 3 The following questions are similar to those that will appear on exam 3. ANS: The following answers are believed to be correct, but there could be typo's, check in your recitation for any corrections. 1. Refer to the declarations and initializations below. Indicate whether each of the following is valid. If the statement is valid, indicate what value is displayed or assigned. If the statement is invalid, explain why. double x[ 8 ] = { 16.0, 12.0, 6.0, 8.0, 2.5, 12.0, 14.0, 54.5 }; int j = 5; (a) (b) (c) (d) (e) (f) (g) printf("%.2f\n", x[j] + 1); printf("%.2f\n", x[j + 1]); printf("%.2f\n", x[j * j]); printf("%.2f\n", x[(int)x[j  1]]); x[2 * j  3] = x[j / 6]; What is the data type of one element of array x? List all the values that are valid subscripts of array x. ANS: a) Valid; 13.00 is displayed. b) Valid; 14.00 is displayed. c) Invalid; subscript out of range. d) Valid; 6.00 is displayed. e) Valid; x[7] is assigned 16.0. f ) double g) 0, 1, 2, 3, 4, 5, 6, 7 2. If m is an N x N matrix of type double elements, what is the effect of the following code fragment for (a) (b) (c) (d) (e) (i = 0; i < N; ++i) m[i][N  1] *= 2; subtracts one from each element of m after doubling it doubles each element of the nexttolast row of m doubles each element of the nexttolast column of m doubles each element of the last row of m doubles each element of the last column of m ANS: e 3. What value is returned by function result? int result( int a, int n) { int i, r; r = 0; for (i = 1;
April 8, 2008 i < n; ++i)
samplee3ansf08.tex 1 of 11 CS410 Spring 2008 Sample Questions ( with answers ) Exam 3 if (a[i] > a[r]) r = i; return (r); } (a) (b) (c) (d) (e) The The The The The a. subscript of the largest of the first n elements of array a. value of the largest of the first n elements of array a. subscript of the smallest of the first n elements of array a. value of the smallest of the first n elements of array a. subscript of the last element greater than its predecessor within the first n elements of array ANS: a 4. What is printed by the following code segment if the input is as shown on the right? char c[20]; scanf("%s",c); printf("%d", strlen(c)); (a) (b) (c) (d) (e) (f) 10 3 4 5 11 None of the above ANS: c 5. What is the effect of this function, assume that "a" is an array containing "n" values. void f (int a, int n ) { int temp, i; for ( i = 0; i < n  1; i++ ) { if( a[ i ] < a[ i + 1 ] ) { temp = a[ i ]; a[ i ] = a[ i + 1 ]; a[ i + 1 ] = temp; } } } (a) (b) (c) (d)
April 8, 2008 INPUT DATA: Yes, I can Arranges the elements of the array in ascending order. Counts the number of elements of the array greater than its first element. Reverses the numbers stored in the array. Puts the smallest value at the end of the array data. 2 of 11
samplee3ansf08.tex CS410 Spring 2008 Sample Questions ( with answers )
(e) None of the above. ANS: d Exam 3 6. If m is a 9 x 9 2dimensional array of integers, what is displayed by this code fragment? for ( i = 0; i < 9; ++i ) printf( "%d ", m[ i ][ i ] ); printf( "\n" ); (a) (b) (c) (d) (e) The middle row of m. The middle column of m. The elements on the diagonal of m. All the elements of m. None of the above. ANS: c 7. What is accomplished by this code fragment if "m" is a twodimensional array with "r" rows and "c" columns and "s" is a onedimensional array with "c" elements? for { ( j = 0; for { } } (a) (b) (c) (d) (e) It stores in the array s a list of the row sums of m. It stores in the array s a list of the column sums of m. It assigns the array s to each row of m. It assigns the array s to each column of m. None of the above. ANS: d 8. What is the contents of the array "v" after execution of the program segment below? int v[ 5 ] = { 1, 0, 0, 0, 0 }; int k, j; for ( j = 0; j < 4; j++ ) for ( k = j ; k >= 0; k ) v[ k + 1 ] += v[ k ]; (a) (b) (c) (d) (e) 1 1 1 1 1 4 4 2 0 2 6 6 3 0 3 4 8 4 0 5 1 12 5 0 8 j < c ; j++ ) i++ ) ( i = 0; i < r; m[ i ][ j ] = s[ j ]; April 8, 2008 3 of 11 samplee3ansf08.tex CS410 Spring 2008 Sample Questions ( with answers )
(f) None of the above ANS: a 9. If "sq" is an array representing the following matrix code fragment? 4 5 2 1 3 4 s = 0; for (a) (b) (c) (d) (e) 41 15 12 10 None of these ANS: d ( i = 0; i < 3; i++ ) s += sq[ 2  i ][ i ]; Exam 3 what is the value of "s" after execution of this 6 9 7 10. What will be the contents of the array "k" after the execution of the code segment below using the input data shown? int k[6] = {0,0,0,0,0,0}; int i, n; for(i = 1 ; i < 5 ; i++){ scanf("%d",&n); k[n] = i; } ANS: 2 3 1 0 4 0 11. Draw a picture of the 2D array and its values after the following code executes. char matrix[ 6 ] [ 4 ]; for( i = 0; i < 6; i++ ) { for( j = 0; j < 4; j++ ) { if ( i %2 == 0  j % 2 == 0 ) matrix[ i ][ j ] = 'X'; else matrix[ i ][ j ] = '0'; } } INPUT DATA: 2 0 1 4 5 6 3 April 8, 2008 4 of 11 samplee3ansf08.tex CS410 Spring 2008 Sample Questions ( with answers )
ANS: X X X X 0 X X X X X 0 X X X X X 0 X Exam 3 X 0 X 0 X 0 12. Which of the following code segments will copy the string "s2" into the string "s1". (Be careful and circle all correct answers). (a) (b) (c) (d) for ( i = 0; s2[ i ] != '\0'; s1[ i ] = s2[ i ]; strcpy( s2, s1 ); s1 = s2; for ( i = 0; s2[ i ] != '\0'; i++ ) s1[ i ] = s2[ i ]; s1[ i ] = '\0'; s1[ 0 ] = s2[ 0 ]; for ( i = 0; s1[ i ] != '\0'; i++ ) s1[ i + 1 ] = s2[ i + 1 ]; strcpy ( s1, s2 ); ANS: d, e, f 13. Given the declarations and initializations below. Give the value of each expression. double x[ 9 ] = { 2.0, 9.0, 1.0, 7.0, 2.9, 4.0, 4.0, 4.2, 3.1 }; int j = 5; (a) x[ j + 1 ] i++) (e) (f) (b) x[ j ] + 1 (c) x[ j / 2 ] (d) x[ (int)x[ j  1 ] ] (e) x[ 1 ] + x[ j  1 ] April 8, 2008 5 of 11 samplee3ansf08.tex CS410 Spring 2008 Sample Questions ( with answers )
ANS: a) 4.0, b) 5.0, c) 1.0, d) 1.0, e) 11.9 Exam 3 14. Given the following program segment and assuming that all the variables have been initialized and the functions have been defined, indicate which of the following statements would be valid at this point inside the function main. Valid means that the statement will compile and execute properly. Mark valid statements with a and invalid statements with an X . int area(int a, int b); int main(void) { int p, s[10], t[7][8]; (a) p = area(p, s[9]); (b) p = area(p, s); (c) p = area(0,s); (d) p = area(s[9],s); (e) t[7][8] = 0; (f) p = area(t[0][0], t[1]); (g) s = t[0]; ANS: a) , b) ,c) ,d) ,e) ,f ) g) 15. Below is the beginning of a program that calls five functions: f1, f2, f3, f4, f5. Write the most appropriate forward declaration for each of these functions so that they are being used correctly. You do not need to write the function definition . int main( void ) { double x = 0 , list[ 8 ]; int y = 0.0 , arr[ 9 ][ 6 ]; double *p = &x; f1( list, arr ); list[ 0 ] = f2( y ); y = f3( arr[ 2 ] , x ); f4( arr[ 2 ][ 1 ] , y ); arr[ 0 ][ 0 ] = f5( &list[ 2 ] , p ); ANS: void f1( double x, int a[ ][ 6 ]); double f2( int x ); int f3( int x, double p ); void f4( int x, int y); int f5( double*p, double * q ); 16. What is the output from this program? April 8, 2008 6 of 11 samplee3ansf08.tex CS410 Spring 2008 Sample Questions ( with answers )
#include <stdio.h> void f( int * p, int q, int * r ) { printf( "F1: %d %d \n", *p, q ); *p = 2; q = 4; *r = 6; printf( "F2: %d %d \n", *p, q ); return; } int main( void ) { int a = 1 , b = printf( "M1: %d f( &b , a, &b ) printf( "M2: %d return ( 0 ); } ANS: M1: 1 F1: 3 F2: 6 M2: 1 Exam 3 3; %d\n", a, b ); ; %d\n", a, b ); 3 1 4 6 17. Write a function that takes an integer array "arr" and an integer "length" for parameters and returns a double. The function should return the average of the first "length" integers in the array. You can assume that "length" is positive. ANS: double f( int arr, int length ) { int i, sum = 0; for( i = 0; i < length; i++) sum += arr[ i ]; return (double) sum / length ; } 18. Write a void function that takes three parameter, an integer input parameter "value", an integer output (pointer) parameter "magnitude" and a character output (pointer) parameter "sign". The function should set "magnitude" to the absolute value of "value", set "sign" to '' if the "value" is negative and set "sign" to '+' if the "value" is nonnegative. ANS:
April 8, 2008 7 of 11 samplee3ansf08.tex CS410 Spring 2008 Sample Questions ( with answers )
double f( int value, int * mag, char * sign ) { if( value < 0 ) { *sign = ''; *mag =  value; } else { *sign = '+'; *mag = value; } } Exam 3 19. In this problem you need to write a function that will input a list of names into an array: Edwin Esther Eugene . . . Frank Assume that MAXNUM and MAXNAME are predefined constants and all strings have length less than MAXNAME. Write a complete C function of type int that will read names into an array of strings with MAXNUM rows and MAXNAME columns, until end of file (EOF) is reached or the array is full. The function should have the array of strings as its only parameter. The function should return the number of strings readin as the return value. You can assume that the names do not contain any spaces. ANS: int read_names( int names[ ] [ MAXNAME ] ) { int i; char temp[ MAXNAME ]; while( i < MAXNUM && scanf( "%s", temp ) != EOF ) { strcpy( names[ i ], temp ); i ++; } return i ; } 20. Suppose ROWS and COLS are globally defined integer constants. Write a complete C function of type int with one parameter, a twodimensional array of integers with ROWS rows and COLS columns. The function should find the maximum value for each row, add them together and return the sum. You can assume ROW and COL are greater than 0 and that the 2D array is full of values.
April 8, 2008 8 of 11 samplee3ansf08.tex CS410 Spring 2008 Sample Questions ( with answers )
ANS: int f( int arr[ COLS ] ) { int i, max , j, sum = 0; for( i = 0; i < ROWS; i++) { max = arr[i][0]; for( j = 0; j < COLS; j++ ) if( arr[ i ][ j ] > max ) max = arr[ i ][ j ]; sum += max; } return } sum ; Exam 3 21. Write a complete C function of type int with two parameters, a onedimensional array of integers and an integer "n". The function should scan in integers from input until a 99 is read or until "n" elements have been read. The function should return the number of elements read. ANS: int f( int arr, int n ) { int i, temp; scanf( "%d", while( i < n { arr[ i i ++; scanf( } return i; } 22. Write a integer function with one parameter, a string representing a DNA sequence. A DNA sequence is a string containing only the characters '"A"', 'G', 'C', 'T', for example: "AGACTTTCACGTT". The function should return True(1) if the number of the number of A's equals the number of T's otherwise it should return False(0). For example, the function would return False on the above sequence since 'A' occurs 3 times and 'T' occurs 5 times. ANS: int f ( char dna[ ] ) { int i, a_count = 0, t_count = 0;
April 8, 2008 &temp ); && temp != 99 ) ] = temp; "%d", &temp ); 9 of 11 samplee3ansf08.tex CS410 Spring 2008 Sample Questions ( with answers ) Exam 3 for { (i = 0; dna[ i ] != '\0'; ++i) if( dna[ i ] == 'A' ) a_count++; if( dna[ i ] == 'T' ) t_count++; } return a_count == t_count; } 23. Write a C function that takes a single M x N integer matrix argument, and finds and returns the largest value in the matrix. ANS: int largest (int matrix[M][N]) { int large, i, j; /* M could be omitted */ large = matrix[0][0]; for (i = 0; i < M; ++i) for (j = 0; j < N; ++j) if (matrix[i][j] > large) large = matrix[i][j]; return (large); } 24. Write a function that has two parametersan M x N integer matrix, and an integer target value. The function should display the row and column subscripts of all occurrences of the target value. ANS: void find_all(int matrix[M][N], int target) { int i, j; printf("Target %d is found in matrix elements:\n", target); for (i = 0; i < M; ++i) for (j = 0; j < N; ++j) if (matrix[i][j] == target) printf("[%2d][%2d]\n", i, j); }
April 8, 2008 10 of 11 samplee3ansf08.tex CS410 Spring 2008 Sample Questions ( with answers ) Exam 3 25. Write a function fill mat that fills an N x N matrix of type double values with data from the keyboard. ANS: void fill_mat (double matrix[N][N]) { int i, j; printf("Enter matrix elements a row at a time>\n"); for (i = 0; i < N; ++i) for (j = 0; j < N; ++j) scanf("%lf", &matrix[i][j]); } 26. Assume that v is a 5element array, m is a 5 x 4 matrix, and r is a 4element array. All three arrays contain type double values. Write a code fragment that multiplies v by matrix m producing result r. ANS: for (i = 0; i < 4; ++i) { r[i] = 0; for (j = 0; j < 5; ++j) r[i] += v[j] * m[j][i]; } 27. Write an integer function that has two string parameters. When the function is called the second parameter contains a string that may have blanks in it. The function should copy the second string into the first but remove all the blanks. The function should return the number of blanks removed as the return value. For example, if the second string is " A string with blanks" then after the function call the first string should be "Astringwithblanks" and the function would return 6. ANS: int f( char b[ ], char a [ ] ) { int i= 0, j = 0; for( i = 0; a[ i ] !='\0'; i++ ) { if( a[ i ] != ' ' ) { b[ j ] = a[ i ]; j++; } } b[j] = '\0'; return } i  j; April 8, 2008 11 of 11 samplee3ansf08.tex ...
View
Full
Document
This test prep was uploaded on 04/22/2008 for the course CS 410 taught by Professor Bochert during the Spring '08 term at New Hampshire.
 Spring '08
 BOCHERT

Click to edit the document details