This preview shows page 1. Sign up to view the full content.
Unformatted text preview: CS410 Spring 2008 Sample Questions Exam 3 The following questions are similar to those that will appear on exam 3. 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. 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 3. What value is returned by function result? int result( int a, int n) { int i, r; r = 0; for (i = 1; i < n; 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 ++i) 4. What is printed by the following code segment if the input is as shown on the right?
April 8, 2008 1 of 7 samplee3noansf08.tex CS410 Spring 2008 Sample Questions Exam 3 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 INPUT DATA: Yes, I can 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) (e) 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. None of the above. 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. 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 {
April 8, 2008 ( j = 0; j < c ; j++ ) 2 of 7 samplee3noansf08.tex CS410 Spring 2008 Sample Questions
for { } } (a) (b) (c) (d) (e) Exam 3 ( i = 0; i < r; i++ ) m[ i ][ j ] = s[ j ]; 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. 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) (f) 14641 1 4 6 8 12 12345 10000 12358 None of the above what is the value of "s" after execution of this 6 9 7 9. If "sq" is an array representing the following matrix code fragment? 4 5 2 1 3 4 s = 0; for ( i = 0; i < 3; i++ ) s += sq[ 2  i ][ i ]; (a) (b) (c) (d) (e) 41 15 12 10 None of these 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; }
April 8, 2008 INPUT DATA: 2 0 1 4 5 6 3 3 of 7 samplee3noansf08.tex CS410 Spring 2008 Sample Questions Exam 3 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'; } } 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 ); i++) (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 ] (b) x[ j ] + 1 (c) x[ j / 2 ] April 8, 2008 4 of 7 samplee3noansf08.tex CS410 Spring 2008 Sample Questions Exam 3 (d) x[ (int)x[ j  1 ] ] (e) x[ 1 ] + x[ j  1 ] 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]; 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 ); 16. What is the output from this program? #include <stdio.h> void f( int * p, int q, int * r ) { printf( "F1: %d %d \n", *p, q ); *p = 2;
April 8, 2008 5 of 7 samplee3noansf08.tex CS410 Spring 2008 Sample Questions
q = 4; *r = 6; printf( "F2: return; } int main( void ) { int a = 1 , b = printf( "M1: %d f( &b , a, &b ) printf( "M2: %d return ( 0 ); } Exam 3 %d %d \n", *p, q ); 3; %d\n", a, b ); ; %d\n", a, b ); 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. 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. 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. 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. 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. 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
April 8, 2008 6 of 7 samplee3noansf08.tex CS410 Spring 2008 Sample Questions Exam 3 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. 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. 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. 25. Write a function fill mat that fills an N x N matrix of type double values with data from the keyboard. 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. 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. April 8, 2008 7 of 7 samplee3noansf08.tex ...
View Full
Document
 Spring '08
 BOCHERT

Click to edit the document details