Sol_exam2_spr07 - CPE 112 — Spring 2007 Exam II(125 pts...

Info iconThis preview shows pages 1–13. Sign up to view the full content.

View Full Document Right Arrow Icon
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 2
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 4
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 6
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 8
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 10
Background image of page 11

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Background image of page 12
Background image of page 13
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CPE 112 — Spring 2007 Exam II (125 pts) April 4, 2007 Name 5 0L UTfia/r/ Bonus Question (+4 pts): The following bonus question is for students present in class the day the code was given. Possible answers for the 4 questions are: A) 123ABC B) EFG456 C) xvz123 D) 456A/BC\ Bonus #1 (+2 pts) Code #1 for Test2 is 754 445C Definition Matching — (12 Points) 1. (12 pts) Match the words with their definitions. Choose the best definition for each word. Function Prototype I Loop H Event Counter d Local Variable :3— lteration Counter E Name Precedence IV Switch Expression M Reference Parameter Function Call Argument 3]: Scope (S Event-Controlled Loop A) Definition is not listed below (may be used more than once) m statement that transfers control to a function. C) Computes a new value by performing a specified set of operations on given values. D).A parameter that receives a copy of the value of the corresponding argument .E)’ A counter variable that is incremented with each iteration of the loop /P)’.A Function declaration without the body of the function G) The order in which the computer executes the statements in a program A control structure that causes a statement or group of statements to be executed repeatedly. A variable or expression listed in a call to a function ,J).A variable declared within a block and not accessible outside of that block )6 The region of program code where it is legal to reference (use) an identifier. L) The period of time during program execution when an identifier has memory allocated to it. )k)’ The expression whose value determines which switch label is selected. )0) The precedence that a local identifier in a function has over a global identifier with the same n e [32‘ loop that terminates when something inside the loop body signals that the loop should be exited. Page 1 of 13 CPE 112 — Spring 2007 Exam II (125 pts) April 4, 2007 True or False - (15 Points) 2. (15 pts) Circle T for true and F for false: ® F a) A while loop is executed zero or more times. T ® b) A compile error occurs if the Data Type defining a function type is omitted. 6') F c) When a break statement is executed, the innermost loop in which it appears is exited. T d) Arguments corresponding to reference parameters can be literal values. T (B e) The body of a for loop executes at least once T f) Event-Controlled loops are exeCuted a are-specified number of times. ® F 9) Value parameters receive a copy of an arguments value T G) h) A logical expression consists of logical operators and Boolean variables only. T (9 i) A function call can contain more arguments than the number of parameters in the corresponding function heading. T E) j) A switch statement MUST have a default switch label. (1) F k) void functions cannot use return expression; @ F I) Local identifiers have name precedence over global identifiers. T 6 m) When a function exits, the contents of any static variables are destroyed (lost). CD F n) While loops can be nested (one loop inside of another loop)? T ® 0) Local variables can be accessed outside of the block in which they are declared? Page 2 of 13 CPE 112 — Spring 2007 Exam II (125 pts) April 4, 2007 Multiple choice (20 points) — Questions 3 — 12 For these problems circle all correct answers. For example if answers A, C and E are all valid then circle A, C and E. 3. Which of the following loop types is not an example of an Event-Controlled loop? A) Sentinel-Controlled B) End-of-File-Controlled @Count-Controlled D) F lag-controlled E) None of these 4. Assume that the Boolean variable X has the value true and that the Boolean variable Yhas the value false. What is the value of the following logical expression? (Y ll (X H IX» B) false C) “true” D) “false” E) None ofthe above 5. What is the output for the following segment of code, if the numbers entered are 1 3 5 7 9? Assume all variables are declared as integers. sum = 0; cin >> number; i do { sum = sum + number; T5 cin >> number; 3 }while (number <= 5); cout << sum; A) 8 C) 15 D) 16 E) None of These 6. The void function named GetNums has two parameters a pass-by-value parameter named x of type float a pass-by—reference parameter named num of type int. Which of the following choices is a valid function heading for the function GetNums? A) void GetNums( float , int& ) (a void GetNums( float x , int num ) void GetNums( float x , int& num ) é— D) both A and C E) none of the above Page 3 of 13 CPE 112 — Spring 2007 Exam II (125 pts) April 4, 2007 7. The void function named GetNums has two parameters a pass-by—value parameter named x of type float a pass-by—reference parameter named num of type int. Which of the following choices is a valid function prototype for the function GetNums? A) void GetNums( float, int& ): B) void GetNums( float x , int num ); C) void GetNums( float x , int& num ); both A and C none of the above 8. What is the output of the following code segment if num has a value of 2? Assume all variables are integers. switch(num) { case 1: cout << “a”; case 2: cout << “b”; break; case 3: cout << “c”; default: cout “end”; 1 A) bcend B) be C) a ® E) None of These 9. What is the output of the following code segment if value has a value of 2? Assume all variables are integers. h an] switch(num) { case 1: cout << “a”; case 2: cout << “b”; case 3: cout << “c”; default: cout “end”; } A) bcend B) c C) abcend D) end E) None of These Page 4 of 13 CPE 112 — Spring 2007 Exam II (125 pts) April 4, 2007 10.Assume that the Boolean variable X has the value false and that the Boolean variable Yhas the value true. What is the Boolean value of the following logical expression? a 1’ ,5 “(MI IY)&&( &&P(Yll IY)))II£ A) true C) “true” D) “false” E) None of These 11..What is the output value of score for the following code segment? int score = 95; if (score > 90) score = score -5;(1 0 else if (score > 80) score = score -5; else if (score > 70) score = score — 5; score = score - 5; 95 cout << score << endl; A) 70 B) 75 C) so E) None of These 12. What is the output for the following segment of code? Assume all variables are declared as integers - sum = 0; for (j = 1 ; j <= 6; j++) { if(j==2||j==4) continue; - - sum = sum +j; \ +“3'+'5>fé>"'55 } cout << sum; Page 5 of 13 CPE 112 — Spring 2007 Exam || (125 pts) April 4, 2007 Short Answer (78 points) - Questions 13 — 23 13.(2pts) Provide the names of th0 types of event-controlled loops 561415 We] "' 7(WV/"'3 , Pica; C(er “Ind/d Em) — of“ 979— C0“ ire/l?) 14. (6 pts) What is the output of the following program? #include <iostream> using namespace std; void Sum(int , int& ); int main() { int sum=—1,num=5; Lg ' ‘ A Sum (num, sum); 50m (Jamie; '1 Um dd 0+ COUt << “Sum Of integers from 1 to “<<num<<” is: “ << sum<< endl; return 0; } 5 (a, void Sum(int n, int& sum) { while(n>=l) { —-— sum=sum+n; --) +S+Lf+3tz+i ’Il/ n=n—l; } } Sum of integers from 1 to is: I ’2 15. (6 pts) Given the following constant and variable definitions/declarations. const int MAX = 100; int sum; float average; string name; int square(int); // function prototype and the following list of expressions to be used as arguments in a function call: a) sum b) sum-25 0) “Hello” d) 22.5 e) square(sum) f) name 9) sum i'mkh) MAX A) Which expressions are valid for use as arguments with value parameters? till cw; Va lici B) Which expressions are valid for use as arguments with reference parameters? 6i, 9,? Page 6 of 13 CPE 112 - Spring 2007 Exam II (125 pts) April 4, 2007 16. (10 pts) Finish the code segment below so that the following actions are perfonned: a) Prompt the user for the name of an input file and read it. b) Open the file. c) If the file did not open successfully, print out a message stating as such, reset the input stream van'able (code to do this is inFile.cIose(),' inFile.cIear();) d) Repeat steps a,b,c until a file is successfully opened or the user enters ctrl-c. ifstream inFile; string filename; cout << “Enter the name of the input file (or ctrl-c to exit): “; cin >> filename; ’ inFile .open (filename. c_str () ); // rest of the code segment goes below this line Wit/KC! m F063 2 (boat/44 £(-3:r|/d+«?11€,o‘rd ndtapen\”'y 12143116 KIOSK ()J‘ ) Isnfi'm. Q/equ-X‘)‘ $ V, \CWI’C I ’4’)- COUkzéll'Enl—e/ We. fiamqe 0/ +113 Mflu‘t'qqlLL a 3“ J ’y‘ P")9‘OP%C«P,/lenam2. CNSWOL Page 7 of 13 CPE 112 - Spring 2007 Exam II (125 pts) April 4, 2007 17.(4 pts) What is the output of the following code segment if num has a value of 50? if (num > 20) if (num < 40) cout << “mum is less than 40—”; else cout << “num is unknown—“; cout << “end\n”; {lU/Vi [S Unknown ~£fl& 18. (8 pts) For the following code segment, show precisely the displayed output. 0 Write one character per box. 0 Skip a box to indicate the presence of a blank space in the output. 0 Skip a row to indicate the presence of a blank line in the output. int loop_b; int loop_a = 0; while ( loop_a <= 3) { for (loop_b = loop_a; loop_b <=4; loop_b++) cout << loop_b; cout << loop_a << endl; loop_a++; Page 8 of 13 CPE 112 — Spring 2007 Exam II (125 pts) April 4, 2007 19. (8 pts) When the program shown below is executed, what is the output to the screen? This problem deals with the scope of a variable in a program, and the order of execution of statements. #include <iostream> using namespace std; void function_A(int&); void function_B(int); int number = 1; int main() { int number = 2; cout << “number in main is: “ << number << endl; function_B(number); return 0; } void function_A(int& num) { int number = 3; cout << “number in function A is: “ << number << endl; num = num — 1; } void function_B(int sum) { cout << “number in function B is: “ << number << endl; function_A(sum); cout << “sum in function B is: “ << sum << endl; sum = sum — 1; The identifying phrases written by the cout statements in this program are shown below. In the blank to the left of the lines, place 1,2,3 or 4 to indicate the order the statements are printed (1 for first, 4 for last). In The blank at the end of the line, put in the output value. 1 number in main is: 51 3 number in function A is: :3 1 sum in function B is: I é; number in function B is: ‘ Page 9 of 13 CPE 112 -— Spring 2007 Exam II (125 pts) April 4, 2007 20. (6 pts) Rewrite the value returning function definition below as a void function definition such that the caller of the function still has access to the result (contained in the function variable average) that is being returned by the value returning function - Use three parameters (one reference and two value) with the void function. float FindAverage(float sum, int num_values) { float average; average = sum/num_values; return average; } Vow FMflvefage’ (floflfgqvemgoj ’VWSJMI awl’ VlUwu-VGIU‘CS) E 3 average :: S'um/ n army; M S; 21. (8 pts) Finish the segment of code using an if-then-else-if structure such that a letter grade is printed out for the grade entered. Letter grade assignments are based on the following information: A for grade > 90, B for grade >76, but <= 90, C for grade > 60, but <=76 and F for grade <=60. int grade; cout << “Enter a students grade (integers only): “; cin >> grade; // Place if—then—else—if testing structure below this statement it (aware) \ Cod+4<uflHJ ngz ,2? (firm/Q. 77%) C004” 44 ((5),; Q/ge fl; ( grade 76$ ll“ COU‘I’C'L NC J Q 4c a]: “J: Page 10 of 13 CPE 112 - Spring 2007 Exam II (125 pts) April 4, 2007 22. (10 pts) Consider the following segment of code int count = 10; int sum = 1; while (count > O) { sum += sum; . cout << sum << endl; count——; } a) Rewrite the above code segment as a for loop such that the same output is obtained. Rm LCOdn+¢lDJ COUA+7OJ Cad/“L'- % 2L f3£fiv\+'7'§‘J’V1; Couk4¢ Swim éé b) Rewrite the above code segment as a do-while loop such that the same output is obtained. Mr (cam =IOJ' ‘fhr-QMq9U QC) 2 SUM+;'§</m; C 0m» 44' $0,,“ 44 two/it"J 3 While (WM 70%" Page 11 of 13 CPE 112 — Spring 2007 Exam || (125 pts) April 4, 2007 23. (10 pts) Add a function prototype, function call statement and function definition only to the following program. No other information is to be added (i. 9. variables). . The name of the value—retuming function is Average. . The function has two parameters of data type float. . The function is to divide the first parameter by the second parameter and return the result. . #include <iostream> using namespace std; // Place the function prototype for Average below this line ’Pi out fivefagac P to“, ,p/Ocd)’. int main() { float avg; float sum = 225; float num = 10; // Place the function call statement for Average below this line // use sum as the first argument and num as the second argument // store the return value in the variable avg ¢§V§IZ [4vertlf€<LS(Jan‘f1¢h~1); cout << “Average: “ << avg << endl; return 0; } // Place the function definition for Average below this line “Plioch Hue ray'aC/Pioqt Sum} .FIOCnL/ldm) (kfiflh Swm/ndflr J 3‘ Page 12 of 13 CPE 112 — Spring 2007 Exam II (125 pts) April 4, 2007 EXTRA CREDIT) (8 pts) Consider the following function definition: int product(int& numl, int& num2, int num3) { return numl *num2 *num3; } Write a complete program that (you do not have to rewrite the above function definition): 1) in maino prompts the user for three integers and places them in variables, 2) in main0 calls the product function using the three integers entered as arguments, and 3) in maino prints the three integers and the return value of the function. 4) Assume that the function definition is to appear below main. NOTE: This is an entire program. so show all #include directives, function prototypes, variable declarations, etc. You do not have to rewrite the function definition. #,anUJ¢ (' £05+r€am> USm9 nawsflécée— 3+3; ill/X1” deJC-frb‘n‘i flJ m'l’ é lh—F) )M1‘M4.M(\ (l\ 2 Q0u+4£ "6 I7¢9\‘)1\f¢e "Jag/’3: J rn’l' fir/mil name, I’M/m3“, cm>7 nUml‘>7 rem/,2 >7m/m3/' V) T Prod; {0/06} ' :: /0.r0c/dc+(/?Um’/ l70m gnarl/13>); 5 uuf 44 H/lL/ryxEQ/J 044/ [Wag/«‘7‘ NEW: '5' ‘ (amL <<nu4nl z< ” u 4‘ ’7le (U, ad CO~¢/+ 44NUm 3 x<wgd F0u+4< “Wart/aha ” Z< flrad 4C GAO/Z; 3 Our // [012$m/7‘7‘0n 5065 AP/{ Page 13 of 13 ...
View Full Document

This note was uploaded on 05/17/2008 for the course CPE 112 taught by Professor Bowman during the Spring '08 term at University of Alabama in Huntsville.

Page1 / 13

Sol_exam2_spr07 - CPE 112 — Spring 2007 Exam II(125 pts...

This preview shows document pages 1 - 13. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online