UNIVERSITY OF WINDSOR 60-106-01 Fall 2007 QUIZ # 2 Solution Examiner : Ritu Chaturvedi Dated :November 27th, 2007. Student Name: Student Number: INSTRUCTIONS (Please Read Carefully) No calculators allowed. Examination Period is : 1 hour Answer all questions in the bubble sheet provided to you. You must use a pencil to mark the answer. There are 5 True/False questions and 30 multiple choice questions. Select only one answer for each question. Write your name and student number on the examination paper as well as the bubble sheet and submit both the bubble sheet and the examination paper before you leave the exam hall. Total Marks : 70 Total number of pages : 11 There may be more than one possible answer. Choose the best possible answer. Good Luck!!! 1
Section I: Multiple-Choice Questions(30X2=60 marks) 1. What is the output of the following program? #include<stdio.h> void swap(int *, int ); void main(void) int x = 3, y = 4; swap(&x, y); printf( In main, x= %d, y = %d\n, x, y); void swap(int* a, int b) int temp; temp = *a; *a = b; b = temp; a. Will not compile because the & is missing for the function parameter b. b. 4 3 c. 3 4 d. 3 3 e. 4 4 2. For the function definition of swap given above, which one of the 2 parameters (a and b) is called by reference? a. Both a and b b. Only a c. Only b d. Neither a nor b 2
3. Which of the types listed below can be the type of a function output parameter? a. int * b. float * c. char * d. all of the above e. none of the above 4. If the input to the program segment at the right is 85, what is its output? a. A scanf("%d", &s); if (s >= 90) b. B printf("a\n"); else if (s >= 70) c. C printf("c\n"); else if (s >= 80) d. D printf("b\n"); else e. C printf("d\n"); B 5. In function apart defined below, how many of the parameters are considered output parameters? void apart(float x, int *wholep, float *fracp) *wholep = (int)x; *fracp = x - *wholep; a. 0 b. 1 c. 2 d. 3 e. all 3
6. What gets stored in variable x after the given if-else is executed. int x = 15; if (x = 5) x = x + 5; else x = x - 5; a. 5 b. 10 c. 15 d. 20 Questions 7 and 8 are based on the following for loop. Assume x is an integer. for (x=5; x<=22; x=x+7) printf( %d,x); 7. What gets printed by the for loop? a. 5 12 19 b. 5 7 12 19 c. 7 14 21 d. 1 8 15 22 8. What value of x gets printed after the loop ends? a. 22 b. 23 c. 5 d. 5 12 19 4
Use the following declaration to answer questions 9,10,11 and 12. int a[50]; 9. Which of the following is a valid instruction that reads a value from the keyboard into array element 9 of array a? a. scanf( %d, &a[i]); b. scanf( %d, &a); c. scanf( %d, a[9]); d. scanf( %d, &a[9]); 10. What is the maximum valid subscript value for array a? a. 0 b. 49 c. 50 d. a[50] e. none of the above 11. Which of the following is a valid instruction that assigns a value of 45 to array element 9 of array a? a. a[i] = 45; b. a[45] = 9; c. a[9] = 45; d. a[] = 45; a. none of the above 12. What is the effect of the following program segment? int i, temp = 0; for (i = 1; i < 50; ++i) if (a[i] > a[0]) temp = temp + 1; a. Arranges the elements of array a in ascending order. b. Counts the number of elements of array a greater than its initial element a[0]. c. Reverses the numbers stored in the array. d. Puts the largest value in the last array position.. 5
13. How long does the following code run? for(i=10;i>20;i=i+5) printf( %d,i); a. Never b. Three times c. Forever d. Two times 14.What is printed by the following program fragment? if(3==3 &&!(2==3)) printf( *** ); else printf(??? ); printf(! ); a. *** b. ***! c.??? d.???! 15.What is the value of k after the following statement is executed, given that i and k are integers and i=3 and k = 0. k = ++i; a. 4 b. b. c. 5 d. 3 e. none of the above 6
Next four questions will be based on the following program segment. int i, a, j = 5, b = 11; for ( i = 1; i <= 10 ; i = i+ 5) a = j % i ; for ( j = 0; j < 10; j = j + 10) b = b + j; j = j + 1; 16. What is the value of i after the execution of the above code? a. 10 b. 5 c.17 d. 11 e. None of above 17 What is the value of j after the execution of the above code? a. 10 b. 0 c. 5 d. 11 e. None of above 18 What is the value of a after the execution of the above code? a. 10 b. 5 c. 11 d. 2 e. None of above 19. at is the value of b after the execution of the above code? a. 0 b. 26 c. 5 d. 11 e. None of above 20. What value is assigned to x when y is 15.0? if (y< 15.0) if ( y >= 0.0) x = 5 * y; else x = 2 * y; else x = 3 * y; a. 45.0 b. 75.0 c. 30.0 d. 15.0 7
21. How many times does the following while loop run? float n; int i = 0; printf( Watch out! Here come a bunch of fractions \n ); while (i < 30) n = 1 / i; printf( %f \n,n); printf( That s all, folks!\n ); a. 30 times b. 31 times c. infinite times d. 0 times e. none of the above 22. Which of the following statements can be used to assign values 1,2,3,4,5,6,7 to the array a using for loop. a. for(i=0; i<7; i++) a[i] = i; b. for(i=0; i<7; i++) a[i] = i + 1; c. for(i=1; i<=7; i++) a[i] = i; d. for(i=1; i<=7; i++) a[i] = i + 1; b. none of the above 23. Which of the following is a valid declaration for the array a used in question 22. a. int a[7]; b. int a[1,2,3,4,5,6,7]; c. int a[6]; d. int a[0][7]; 8
24. Assuming that a 3-by-4 array is declared as int a[3][4], Which of the following statements will assign a value of 6 to the element at 1 st row and 2 nd column of a. a. a[0,1] = 6; b. a[0][1] = 6; c. a[1,0]=6; d. a[1][0] = 6; e. a[3][4]=6; 25. Given x = 4, y = 3, and operator = 2, what is the final value of x after the following switch statement is executed? switch (operator) case 1: x = x + y; case 2: x = x - y; case 3: x = x * y; case 4: x = x / y; a. 1 b. 3 c. 1.33 d. 7 9
Question 26 30 are based on the program given below : #include <stdio.h> int a=10; int b=4; int c=4; int fun(int,int *,int *); void main(void) int a=9; int b=1; a=fun(a, &b, &c); //end of main int fun(int u, int * v, int * w) *v = a / b; c = *v + a; *w = u + c; a = c + b; b = a + *v; return u + *w; Answer questions 26-30 after execution of the above program. 26. What is the value of a in main? a. 25 b. 2 c.30 d. 27 e. None of above 27. What is the value of b in main? a. 25 b. 2 c.30 d. 27 e. None of above 28. What is the value of a in Global Area? a. 25 b. 27 c. 21 d. 30 29. What is the value of b in Global Area? a. 25 b. 2 c.30 d. 27 e. None of above 30. What is the value of c in Global Area? a. 25 b. 2 c.30 d. 27 e. None of above 10
Section II: State True / False for questions 31-35 (5 X 2 = 10 marks). (TRUE = A, FALSE = B) 31. The default case is required in a switch statement.false 32. The body of a for loop might not get executed at all.true 33. Sentinel-controlled loops do not know upfront the number of iterations to execute. True 34. Functions cannot have more than one output parameters. False 35. The data-type of an integer array declared as int a[10] is an int. False 11