Sol_exam2_sum07 - The University of Alabama in Huntsville...

Info iconThis preview shows pages 1–14. 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

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

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

Unformatted text preview: The University of Alabama in Huntsville Exam ll, CPE 112, Summer 2007 / Name 30L U 73:0 I\/ July 19, 2007 90 Minute time limit —150 points Closed notes, book and neighbor. If you have any questions ask them. Notes: Segment of code — necessary C++ statements to perform the action described — not a complete program Program — a complete C++ program —- what you have been writing in lab. Write clearly and make sure the case of a letter is clear (where applicable) since C++ is case sensitive. Unless otherwise noted, assume a single sgace between all words. For this test the two-character sequence \n is to be taken to mean the newline character. There are no INTENTIONAL syntax errors. Assume that all code in this exam will compile. There may be logic errors in some of the code. 1) (2 pts) A function that does not return a function value is known as what kind of function? A) Value returning B) Reference Parameter C) Empty E) Expression less F) None of these 2) (2 pts) How many function values does a void function have? B) 1 C) As many as necessary D) 2 E) 3 F) None of these 3) (2 pts) Circle all of the following that are examples of event-controlled loops: < A) End-Of—File Controlled 1) B) Sentinel-Controlled ‘ Flag-Controlle D) Count-Controlled E) Previous-Value F) All of these 4) (2 pts) Which operations below ARE NOT ALLOWABLE aggregate operations on structures. A) Return as a functions return value B) Assignment ) Input/Output (cin, cout statements) F) Argument pass by value to a function G) Argument pass by reference to a function H) None of these Page 1 of 14 The University of Alabama in Huntsville Exam ll, CPE 112, Summer 2007 5) (2 pts) Reference parameters (passing by reference) are used if a parameters data flow is: A) one-way, into the function D A and B B) one-way, out of the function ) B and C C) two-way, into and out of the function one 0 these 6) (2 pts) Value parameters (passing by value) are used if a parameters data flow is: A one-wa into the function D) A and B B) one-way, out of the function E) B and C C) two-way, into and out of the function F) None of these 7) (3 pts) What are the three logical operators for C++? five 4% 0R ll NOT ’. 8) (6 pts) For the operators shown below, CLEARLY indicate if the operator is: Relational (use an R), Logical (use an L) or Neither (use an N). NOTE: that there are no spaces between characters even though it may appear that there is a blank in some of the operators a)>>_M_ b)n_L_ c==_lL_ d)>=r<_ d)l< N e)! L— f)=! l! 9)!= ______Q h)<= L i)&&_L_ j)=>_l\_/_ k)=_&_ Page 2 of 14 The University of Alabama in Huntsville Exam ll, CPE 112, Summer 2007 9) (15 pts) True/False questions. Select T for true and F for false.. ® F a) Reference parameters receive the memory location of an argument. T ® b) The body of a for loop executes one or more times. (B F c) Local identifiers have name precedence over global identifiers. T @ d) The use of the statement: return; is not valid in a void function. CD F e) A logical expression can consist of a single relational expression? (1) F f) Static variables in a function maintain their value from function call to function call. (D F g) A variable declared as a union holds a value for only one member at any given time? 6') F h) Members of a structure can be different DataTypes. ® F i) Value parameters receive a copy of the arguments value. T (a j) A function call can contain, more arguments than the number of parameters in the corresponding function heading. 6') F k) In sentinel-controlled loops, the sentinel is a value unexpected as normal input? T ® I) An argument corresponding to a reference parameter can be a constant or arbitrary expression? T ® m) An if statement cannot occur inside of another if statement. T @n) A compile error results when the DataType defining a functions value type is omitted. ® F o) The statement for(;;); is a valid C++ statement. Page 3 of 14 The University of Alabama in Huntsville Exam ll, CPE 112, Summer 2007 10) (12 pts) Match the words with their definitions. Choose the BEST definition for each word. a) Static Variable L 9) Event Counter b) Member Selector _§_ h) Function Call 2 c) Function Prototype A ' i) Hierarchical Structure A d) Lifetime ___l.__ j) Local Variable _/\f_ e) Structure j k) Reference Parameterg f) Iteration Counter 4— |) Scope Q A) Definition is not listed below (may be used more than once). The expression used to access components of a struct variable. I C) A parameter that receives a copy of the value of the corresponding argument. l/fiL U5: WMQM {97A parameter that receives the memory location of the corresponding argument. 2)’ A statement that transfers control to a function. i’)’ A variable for which memory remains allocated throughout the execution of the entire program. G) A structured collection of components, all of the same DataType that is given a single name. My (HYA structure in which at least one of the members is itself a structure. I) The expression whose value determines which switch label is selected. 5 W Ha“ 004” SS" 0" J) a variable or expression listed in a call to a function. fi/fldme/d- )Q’A variable that is incremented when a particular event occurs. /|:)’T he period of time during program execution when an identifier has memory allocated to it. M) A component of a structure. mambo/f )t')‘ A variable declared within a block and not accessible outside of that block. he region of a program where it is legal to reference (use) an identifier. Page 4 of 14 The University of Alabama in Huntsville Exam ll, CPE 112, Summer 2007 11) (8 pts) In the following code segment, all variables are integers. maxHeight = 20; maxWeight = 50; if ( height > maxHeight) if (weight > maxWeight) cout << “Message #l\n”; else cout << “Message #2\n"; else if (weight < maxWeight) cout << “Message #3\n”; else cout << “Message #4\n”; If the variables height and weight have the following values, what is the output of the above segment of code when it is executed? a) height = 50 , weight = 40 qumqe #2, b) height = 30 , weight = 50 Messaqu #‘L 12) (8 pts) Write the structure declaration for a structure named LogInfo containing the following members: - an integer variable representing the log entry number - a string variable representing the name of the person making the log entry - a floating-point variable indicating the cost of the entry - A Date variable indicating the day of the entry where Date is a structure already defined 6 «uu- Lo 9 fnid 2 ml- ,Qoy NW»; 54v. n3 0 a wed" {you} C054") bate, &6v‘d~d 30 Page 5 of 14 The University of Alabama in Huntsville Exam ll, CPE 112, Summer 2007 13) (10 pts) Consider the following structure declarations when answering the questions below. struct Time { int hour; int minute; int second; i; struct Appointment { string name; string type; string day; Time duration; i; a) Write a statement that declares the identifier clock as a variable of DataType Time. T) me C [0C 2.5 b) Write a cout statement that will output the value of the hour member of clock. coal. 1. < (Lucia. lam/VJ 0) Write a statement that declares the identifier date as a variable of DataType Appointment. 970m I'nrf’merv’f a d) Write a statement that assigns a value of “Dr. Fixit” to the name member of date. it . don‘t new: “W: ’31” J e) Write a statement to assign a value of 15 to the minute member of duration, which is a member of date. Jail. mun/+6, 3/53 14) (6 pts) Consider the following segment of code int loop = 0; while (loop < 7) { cout << “Hello”; loop = loop + 2; } Rewrite the above code segment as a for loop such that the same output is obtained. w loaf; fixttloop :20; MFA—7}” 1200/0 7‘"- Z) 000+ 44 "HWW'fi Page 6 of 14 The University of Alabama in Huntsville Exam II, CPE 112, Summer 2007 15) (10 pts) a) What is the output for the code segment below: int count = 10; bool notFinished = true; while (notFinished) { if (count > 5) notFinished = true; else notFinished = false; count——; } // This is the only line that outputs information cout << “count is: “ << count << endl; count is: Li b) If necessary, rewrite the code segment in part a (by correcting any possible logic errors) so that the value printed out is 5. You can modify lines that are present only — do not delete or add any more lines to the code segment. If no modifications are required, write “No modifications required". A partial copy of the code has been provided to reduce the amount of writing necessary. int count = 10; bool notFinished = true; ,, 4,90% I _ moi/L wwflwar while Cnai- Fm What?) "(F S f“ { CM+"\) if L60m+7Q m ,‘g @W 75) not Fini shed ‘1 else notFinished 3> J C00n+ -'J } // This is the only line that outputs information cout << “count is: “ << count << endl; Page 7 of 14 The University of Alabama in Huntsville Exam II, CPE 112, Summer 2007 16) (8 pts) For the following code segment, write out what is printed to the screen. Place a single character in each box, skip a box to indicate a space, and skip a row to indicate a blank line. int loop_b; int loop_a = 0; while ( loop_a <= 2) { for (loop_b = 0; loop_b <= loop_a; loop_b++) cout << loop_b; loop_a++; cout << loop_a << endl; Page 8 of 14 The University of Alabama in Huntsville Exam ll, CPE 112, Summer 2007 17) (8 pts) For the following code segment, write out what is printed to the screen. Place a single character in each box, skip a box to indicate a space, and skip a row to indicate a blank line. #include <iostream> using namespace std; void Test(); int main() void Test() { static int i = 0; static int j = 5; j = 0; i++; j++; cout << 1 << “—“<< j << endl; Page 9 of 14 The University of Alabama in Huntsville Exam II, CPE 112, Summer 2007 18) (8 pts) Finish the segment of code below by using an if-then-else-if statement to print out the following information based on the value of grade: “A” if grade has a value of 90, “B” if grade has a value of 80, “C” if grade has a value of 70, and “Error” if grade is not 90, 80' or 70. Note: The output is to be only one value - A, B, C or Error. int grade; cout << “Enter in the grade value: “; cin >> grade; // if-then-else-if statement follows this comment. {\‘F < grak 3:96) Codi» La “fiv” 44 eno’f; le [XE (3.”qu : : 90) Cad-1r 4 a "6"44'4-44) [lg ;\‘F( gradét;7o> Cow 44 "c." A‘W’ZS re 1 («e - C&U+ 44 UE'KM " ¢4M£J Page 10 of 14 The University of Alabama in Huntsville Exam II, CPE 112, Summer 2007 19) (10 pts) Finish the program below by adding a value-returning function specified below. Add only a function prototype, function call statement and function definition to the following program. -) No other information (other than what is necessam for the 3 items above) is to be added to the program (i.e. variables) (- The name of the value-returning function is InitStruct. The function has one integer parameter. The function is to initialize each member of a structure variable of data type Date with the value of the integer parameter. The function returns, as its function value, the initialized structure variable. #include <iostream> using namespace std; struct Date { int month; int day; int year; }; // Place the function prototype below this line [)cbfe. -IL‘L+5§+VUC*'<;fyth)J int main() { Date date; int value = —1; // Place the function call statement below this line // information stored in date must be available in main am: 1; pH {xv/c» C Value); return 0; } // Place the function definition below this line Dock fm'+ Sid/(:14 (/h+ V4 lye)? :- dQ‘Le. 1qu. chL/ ; VG [Mi feWrn i Page 11 of 14 The University of Alabama in Huntsville Exam ll, CPE 112, Summer 2007 20) (8 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 value returned by the value-returning function. float Calculate (float numl, float num2) { float result; result = (numl + num2)*5.0; return result; } M9368 Calculm‘ccwclocd 49 NW”) ’pwnomlj lflwrzmz) ? 3 V189 (+2 @(jm/ Mdml) 965% 21) (10 pts) Write a Boolean value-returnin function definition called QEenInEutFilo. - The function prompts the user for the name of the input file, reads the name of the input file and then tries to open the input file (use . c_str () ). - The function returns a value of true if the input file was successfully open. - A value of false is returned if the input file was not successfully opened. - The function requires two parameters. The first parameter represents the name of the input file that is entered for opening, and the name of this file must be available for use in main () . - The second parameter represents the input stream to use. l000/ open fiww Eh (Smngéna/mcj [LS-fractm 49L. F/L) E Cowl—44 “5th mflumé'lz fléorhe: '3‘ Cph>§fl4mgj ILL Pike/1m (new, C- S#L\\; return [OOCQIAFIK/e)‘; \>, Could a5!) W (LN/fl WV‘IQ) elgr - Page 12 of 14 WM" 0“[89 The University of Alabama in Huntsville Exam ll, CPE 112, Summer 2007 22) (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. There will be a total of 4 lines written to the screen from this program. #include <iostream> using namespace std; void function_A(int&); void function_B(int&); int number = 4; int main() { «2. int number = 3; ,“J(DV“MJ function_B(numb r); cout << “number in main is: “ << number << endl; return 0; } veid function_A(1nt& n%F%h) 500*. int number = 2; num = num +2; 5 Z cout << “number in function A is: “ << number << endl; } void function_B(int& s m) { T’Vii 6’6" function_A(sum);$ back. A! cout << “number in function B is: “ << nu er << endl; sum = sum + 1; cout << “sum in function B is: “ << sum << endl; } The output for this program is as indicated 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. number in main is: C0 ! number in function A is: 2, number in function B is: L/ 3 sum in function B is: d) Page 13 of 14 The University of Alabama in Huntsville Exam ll, CPE 112, Summer 2007 Bonus Question (+5 pts) The following program is executed. The user enters the integer “6” when prompted for a number. What is the output to the screen? Just fill in the blanks indicated in the output line shown Be careful on this problem. Think about what is being performed with which variables #include <iostream> using namespace std; void Summation( int num, int& result ); int main () { int number = -1; int result = —1; cout << “Enter an integer between 1 and 10: “; cin >> number;(p /\ f“) [800* Summation(nuéger, result); // first function call Summation(nu ber, result); // second function call 18m, 1% 004- // show the output for this cout statement only cout << “Summation for “ << number<< “ is: “<<result <<endl; return 0; } void Summation( int n m, int& result) { i ’1 m, 1900+ static int loop = 0,- lgml IQOU'F result = 0; —-9 5‘4 resd 2!- «lo 0 for (loop = num; num >=l; num-—) 200,0;(0 dag: “def-dam { result = result + loop; 0% +4, +6, num = num —l; } return; } Answer: Summation for (o is ReéaH-co/ mums—(o, . Va (4124 SI 2 ,fioop I’ldm r980 1+ Wm WM ('0 é (a fi’ “9"” (a Li 6+9 9) 2, Z. égzlg (9 Z (911%: ’8 O J) exert-F Page 14 of 14 ...
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 / 14

Sol_exam2_sum07 - The University of Alabama in Huntsville...

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

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