CIS 71 3rd Exam: Spring, 96 Name:__________________________ Indicate where each operation in questions 1 - 15 would appear in a program that contained a loop. Use the answers below: a. before the loop header b. in the loop header c. in the loop body d. after the loop 1. The statement that prints the heading for a table of information a 2. The statement that prints each line of a table with several similar lines. c 3. The statement that tests whether a data item is the sentinel b 4. The statement that reads the first data item for a sentinel loop a 5. The statement that reads the second data item for a sentinel loop c 6. The statement that reads the third data item for a sentinel loop c 7. The statement that adds the next data item to an accumulating sum c 8. The statement that computes the average of a collection of data items. d 9. The statement that initializes the variable that stores a sum. a 10. The statement that displays a line of dashes under a table d 11. The statement that adds 1 to the counter for a counting loop written as a while loop c 12. The statement that initializes the counter for a counting loop written as a for loop b 13. The statement that specifies the limit value for a for loop. b 14. The statement that reads in the number of times a counting loop should be repeated. a 15. The statement that initializes the counter for a counting loop written as a while loop. a 16. Every for loop can be implemented using a while loop (T/F) t 17. Every while loop can be implemented using a for loop (T/F) t - not true in most languages, but true in C. while loop is generally more readable, except for counting loops. 18. The increment for a for loop is always +1. (T/F). F 19. A for loop always executes at least one time (T/F). F 20. A do-while loop always executes at least one time (T/F) T Use the answers below for questions 21 - 26 a. a syntax error b. a run-time error c. a non-terminating loop d. a loop that executes zero times e. a loop that executes one time 21. Omitting the update step for the loop control variable in a while loop causes ___________ c 22. Omitting the repetition condition in a while loop causes _________ a 23 Using the loop header while (x == x) causes _________ c 24. Using the loop header while (x != x) causes _________ d 25. Using the loop header for (i = 10; i <= 10; i++) causes _________ e 26 Using the loop header for (i = -10; i <= 10; i--) causes __________ d For questions 27-28 choose an the answer from the list a. 5 b. 6 c. 20 d. 25 e. 30 27. What value is stored in SUM after loop exit? SUM = 0; NEXT = 0; while (NEXT < 25) { NEXT = NEXT + 5; SUM = SUM + 1; } a 28. What value is stored in NEXT after loop exit for the above loop. d Use the for statement below for questions 29 to 33. X = 1; for (I = 2; I <= N; I++) X = X * I; 29. How many times does the loop execute? a. N d. 1 b. N - 1 e. N - 2 c. 0 b 30. If N is 5, what would the final value of X be? a. 5 d. 24 b. 14 e. 120 c. 15 e 31. Answer question 30 if N is 5 and the * is replaced by +. c 32. If we replace the for statement with a while loop, which statement below should be added to the loop body? a. N = N + 1; b. I = I + 1; c. printf("%d\n", X); d. X = X + 1; e. N = N - 1; b 33. If we replace the for statement with a while loop and initialize I to 2 before loop entry, which condition below should be the loop repetition condition? a. I <> N b. I >= N c. I > N d. I < N e. I <= N e For questions 34 to 39, select the best answer from the list: a. counting loop b. while loop c. sentinel loop d. do-while loop e. flag-controlled loop 34. Which always uses an input variable for loop control? c 35. Which would you use when loop exit is based on the occurrence of a particular event? e 36. Which would you use when the end of the data is indicated by a special value? c 37. Which is always executed at least once? d 38. Which always uses a logical variable for loop control? e 39. Which always uses an update step that increments the loop control variable by 1? a The next 3 questions refer to the following program segment. Assume that all variables are type int. Z = 0; G = 0; S = 0; I = 1; while (I < 50) { scanf("%d", &T); S += T; if (T >= 0) G++; else Z++; I++; } 40. How many times is the loop body executed? c a. once b. never c. 49 times d. 50 times e. until a number 50 or larger is entered. 41. The value stored in variable S at the end of execution of the loop could best be described as the c a. average of the numbers read b. largest of all numbers read c. sum of all numbers read d. number of numbers read e. sentinel value 42. The value stored in variable Z at the end of execution of the loop could best be described as the c a. number of positive items read b. sum of all positive items read c. number of negative items read d. sum of all negative items read e. sentinel value ----------------------------------------------------------- Not on loops - don't answer these. A function f is supposed to return the sum (through argument s) and product (through argument p) of two numbers passed as arguments a, b. 43. What could be a correct formal argument list for f? a. (int a, int b, int &s, int &p) b. (int a, b, int *s, p) c. (int *s, int *p, int a, int b) d. (int *sum, int *product, int a, int b) e. (int a, int b, int s, int p) 44. What statement would you use to return the value of the sum a. return sum; b. return a + b; c. sum = a + b; d. *s = *a + *b e. *s = a + b 45. If you needed to return a third formal argument, funnyave, which was the mean of the sum and product, what statement would you use? a. *funnyave = (*s + *p) / (double) 2; b. funnyave = (s + p) / (double) 2; c. return (s + p) / (double 2); d. &funnyave = (&s + &p) / (double) 2; e. return 0.5; 46. How would you call the original function from another function if you wanted to find the sum (s1) and product (p1) of two numbers n1 and n2 declared in that function. Assume that the function inputs are provided as the first 2 arguments. a. s1 = f(n1, n2, p1); b. p1 = f(n1, n2, s1); c. f(n1, n2, *s1, *p1); d. f(&n1, &n2, &s1, &p1); e. f(n1, n2, &s1, &p1); A function g has 2 arguments. It returns the absolute value of its first argument through its first argument, and it returns the value of its second argument increased by 1 through its second argument. 47. What is a valid prototype for function g? a. void g(int a, int b); b. int g(int a, int b); c. void g(int *a, int *b); d. void g(int &a, int &b); e. void g(int abs(a), int b++); 48. What kind of arguments does function g have? a. input b. output c. input/output d. input or output 49. What statement would you use to return the first argument value? a. return abs(a); b. a = abs(a); c. *a = abs(a); d. *a = abs(*a); e. a = abs(*a); 50. What statement would you use to return the second argument value? a. return b++; b. (*b)++; c. (*b) = b++; d. b = *b + 1; e. *b = b + 1;