Important points to Understand for Chapters 5 - 7 exam Chapter 5 - loops while statement, for statement, and do-while statements how to convert from one form to the other sentinel-controlled loops flag-controlled loops counting loops how to trace loop operation Chapter 6 - functions call by value versus call by reference - table 6.1 know the difference between input, output, and input/output arguments Know how to indicate the kind of an argument in function headings and prototypes. Table 6.1. Be able to draw and work with diagrams like Fig. 6.4 and 6.5. Know definition of driver and stub. Understand nested function calls and recursive function calls. Be able to analyze simple recursive functions. Chapter 7 - data types integer division casting character values and conversions back and forth from type char to type int. Know how to write simple character functions enumeration types /* For next exam Chapter 8 - streams difference between cin >> ch and cin.get (ch) Understand how data is read from a stream and how whitespace is handled. Know the effect of non-numeric characters when reading a numeric value. Know functions open, get, put, eof, fail, close and how to use them in a program Know how to read data from an external input stream, process it, and write it to an external output stream. Know difference between internal and external (directory) names for a function. Understand string input and use of function getline. */ CIS-067, Second Exam, Instructor: Elliot Koffman - Page 1 Course CIS-067, (Chapters 5 to 8) (1) What is a cohesive function? (2) What function call type is used when the symbol & immediately follows an argument in a function header? (3) Once a function is defined in a program, how many times can it be called within the main() function? Consider the following program segment when answering the next three questions: void main() { int a = 1, b, c; b = sum(a, a); c = sum(sum(a, a), sum(a, a)); } int sum (int a, int b) { return a+b; } (4) What is the value of b? (5) What is the value of c? (6) Is the following statement correct? Why? sum(a, a) = b; Consider the following (incorrect) factorial function when answering the next three questions: int factorial (int n) { if (n <= 1) return 0; else return n * factorial (n-1); } (7) What should be changed in the above function for it to correctly return the factorial of a positive integer? (8) What would happen during the execution of the corrected function (i.e. the function obtained after the changes in question 7 were made) if the third line was replaced with the following? else return n * factorial (n); } (9) Would the corrected function (i.e. the function obtained after the changes in question 7 were made) return a different factorial value if the first line was replaced with the following? int factorial (int& n) { Consider the following program segment when answering the next three questions: #define kappa = 5.66 const float pi = 3.14159; float a = pi + 1; (10) Is there any syntax error in the statements above? If yes, what is it? (11) Would the following statement generate an error message if it was added to the end of the program segment? Why? pi = pi + 2; (12) What is the value of a? Consider the following program segment when answering the next three questions: char b, c, d, a = ÔBÕ; b = --a; c = ++a; d = a / 10000; (13) What is the character stored in b? (14) What is the character stored in c? (15) What is the value of d? (20) Trace the function calls and indicate what is displayed when the program below executes with the proper libraries included? // void dummy (char&, char, int&) void main () { char aa, bb; int cc; aa = 'z'; bb = '5'; dummy (aa, bb, cc); cout << aa << bb << endl; aa = '5'; bb = 'z'; dummy (aa, bb, cc); cout << aa << bb << cc << endl; } void dummy (char& a, char b, int& c); { toupper (a); toupper (b); if (isdigit (b)) c = int (b) - int ('0'); else if (isletter (b)) c = int (b) - int ('A'); else c = 99; } (21) What librarys need to be included? (22) Indicate the kind (input, output, input/output) of each formal argument. The next four questions refer to the following program segment. (Assume that all variables are of type int.) z = 0; g = 0; s = 0; i = 0; while (i < 50) { cin >> t; s += t; if (t >= 0) g++; else z++; i++; } 5. How many times is the while statement executed? a. Once. b. Never. c. 49 times. d. 50 times. e. Until a number of 50 or larger is entered. 6. The value stored in variable s at the end of the execution of the loop could best be described as the ____________. 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 terminating the loop 7. The value stored in variable z at the end of the execution of the loop could best be described as the ____________. 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 terminating the loop 8. The loop can best be categorized as a ____________. a. counter-controlled loop b. sentinel-controlled loop c. general conditional loop d. do-while loop e. None of the above. 9. What does this program segment do? s = 0; for (i=n; i>=1; i--) { if ((i % 2) == 0) s += i; } a. Adds all the integers from 1 to n. b. Adds all the integers from 1 to n-1. c. Adds the even integers from n down to 1. d. Adds the odd integers from n down to 1. e. None of the above. 10. What does this program segment do? s = 0; i = 1; do { s += i; i++; } while (i < n); a. Adds all the integers from 1 to n. b. Adds all the integers from 1 to n-1. c. Adds the even integers from 1 to n. d. Adds the odd integers from 1 to n. e. None of the above. 11. Rewrite the code segment below using a while loop to produce a code segment that is exactly equivalent. // for loop // while loop product = 1.0; for (next=1; next <= m; next++) product *= next; 12. Show the output for the fragment: for (k = 5; k > 0; k--) { for (i = 0; i < 5-k; i++) cout << '.'; for (j=0; j < 2*k-1; j++) cout << 'B'; cout << endl; } For the next three questions, consider the following function prototype: void compute_ave (float, int, float&); 13. The function call compute_ave (12.0, 5, 22.0) is valid. ` True/False 14. The function header void compute_ave (float sum, int num, float ave) is valid. True/False 15. The protoype indicates the function has _________ input arguments and ___________ output arguments. 16.Given the function header void compute_ave (float sum, int num, float& ave) The statement sum++; within the function would change the value of s in the calling program, assuming the function is called by compute_ave (s, n, a). 17. What is the output from the following program? #include void main () { void do_something (int&, int); int first, second; first = 1; second = 2; do_something (second, first); cout << first << second; return; } void do_something (int& thisp, int that) { int the_other; the_other = 5; that = the_other + 2; thisp = the_other * that; return; } // end do_something a. 35 2 b. 1 35 c. 35 7 d. 1 2 e. 1 0 Assume the following program declarations for the next two questions. float x, y, z; int m, n; void w // function prototype (float&, float&, int); 18. Is the function call w (z, y, m); correct? If not, state why not. 19.Is the function call w (z*2, y, m); correct? If not, state why not. 20. An enumeration type variable cannot be used in the switch statement. True/False 21. Given the declaration enum day {mon, tues, wed, thurs, fri}; the condition mon < fri is true. True/False 22.Given the declarations enum weekend {sat, sun}; enum party {today, tomorrow}; what is the value of the expression int(sat) + int(tomorrow)? Answer: ________ 23. If a computer's collating sequence places uppercase letters in consecutive ordinal positions, then int('F') - int('A') is ____________. a. not defined b. 5 c. 4 d. 'B' e. 'D'