Name Definition Matching (8 Points) 1. (8 pts) Match the words with their definitions. Choose the best definition for each word. Relational Expression Iteration Counter Count-controlled loop Loop Flow of Control Event Counter Event-controlled loop Logical Expression A) Definition is not listed below (can be used more than once if necessary) B) A variable that is incremented with each iteration of the loop C) A loop that executes a specified number of times D) A variable that is incremented each time a particular event occurs E) An expression followed by a relational operator followed by an expression that evaluates to a logical value of true or false F) A step for which the implementation details are fully specified G) A statement that transfers control to a function. H) The order in which the computer executes statements in a program I) A statement used to alter the normally sequential flow of control J) A control structure that causes a statement or block to be executed repeatedly K) A step for which some implementation details remain unspecified L) A loop that terminates when something happens inside the loop body to signal that the loop should be exited Multiple choice (12 points) Questions 2 13 For these problems circle all correct answers For example if answers A, C and E are all valid then circle A, C and E. 2. How many times does the body of the following while loop execute? int sum = 0; int count = 1; while (count < 6) sum = sum + count; count = count + 1; A) 3 B) 4 C) 5 D) 6 E) 7 F) None of these Page 1 of 11
3. When used with an input file stream variable, which statements below (A through D) are false about the open function? A) It creates a new, empty file if the file does not exist. B) It sets the reading marker at the first character in the file. C) It puts the stream into the fail state if the file does not exist. D) It associates the name of a stream variable with the name of a physical disk file. E) All of the above are true statements 4. What happens when a C++ input stream enters the fail state? A) The system displays an error message, and program execution is terminated. B) The system does not display an error message; the program continues running, and further input operations with that stream are ignored. C) The system does not display an error message, and program execution is terminated. D) The system displays an error message; the program continues running, and further input operations with that stream are ignored. E) None of the above are valid explanations 5. What is the output for the following segment of code(be careful on this problem)? int sum = 0; for (int j = 1 ; j > 7; j++) sum = sum +j; cout << sum; A) 0 B) 2 C) 6 D) 12 E) 20 F) 30 G) None of These 6. What is the output of the following code segment? int score = 85; if (score <= 90) score = score - 5; if (score <= 80) score = score - 5; if (score <= 75) score = score - 5; cout << score << endl; A) 85 B) 80 C) 75 D) 70 E) None of These 7. In the following code segment, what is the best data type for the variable named mystery????? mystery; mystery.open( file.txt ); int x = 25; mystery << X is << x << endl; A) ofstream B) string C) ifstream D) bool E) Can t determine from given info Page 2 of 11
8. The What is the output for the following code if the user enters: 325 PAST A indicates a space int number; char letter; cin >> letter >> number >> letter; cout << letter; A) 2 B) 5 C) P D) A E) S F) T G) (A space) H) Answer is not shown 9. What is the output for the following code if the user enters: 325 PAST A indicates a space int number; char letter; cin >> letter >> number; cin.get(letter); cout << letter; A) 2 B) 5 C) P D) A E) S F) T G) (A space) H) Answer is not shown 10. Assume that the Boolean variable X has the value false and that the Boolean variable Y has the value true. What is the value of the following expression?!( (Y!Y)&& (X!X) ) A) true B) false C) maybe D) I don t know 11. What is the output for the following segment of code? int sum = 0; int j; for (j = 0; j < 5; j++) sum += j; cout << j; // what is being output? A) 0 B) 5 C) 10 D) 15 E) Nothing, it is an infinite loop 12. What are the two main categories of loops discussed in class: A) Sentinel-Controlled B) Count-Controlled C) Flag-Controlled D) Summing-Controlled E) End-Of-File Controlled F) Event-Controlled Page 3 of 11
13. What is the output for the following code if the user enters: 55 B6A 33.\n ABC A indicates a space string line; char letter; getline(cin, line, \n ); cin >> letter; cin.get(letter); cout << Start- << letter; A) Start-A B)Start-B C)Start-C D)Start- E)Answer is not shown True or False (12 Points) 14. Circle T for true and F for false: T F a) Logical operators can take relational expressions as operands? T F b) A logical expression can consist of a single Boolean variable? T F c) Relational expressions are made up of logical values and logical operators T F d) The state of an input stream can be tested as a true or false value? T F e) Placing an if statement inside a block associated with another if statement results in a syntax error? T F f) The statement cin.get(ch); reads the first non-whitespace character from cin into ch. T F g) The number of times an event-controlled loop executes is known ahead of time. T F h) While loops can be nested (one loop inside of another loop)? T F i) The body of a while loop is always executed at least one time. T F j) The extraction operator can be used to read a newline character. T F k) In sentinel-controlled loops, the best sentinel is a value that is not expected as normal input. T F l) The body of a for loop executes zero or more times. Page 4 of 11
Short Answer (68 points) Questions 15 31 15. (3 pts) What are the three logical operators for C++ (show the symbols)? 16. (6 pts) For the operators shown below, CLEARLY indicate if the operator is relational (use an R) or other (use an O). NOTE: there are no spaces between characters a)> b) < c) => d) =< d) e) & f)!= g) == h)>= i) <= j) =! k) = 17. (4pts) Given the following values for the Boolean variables w, x, y and z: w = false, x = false, y = true, z = true Clearly indicate whether each logical expression has a Boolean value of true or false. a) (!w!y &&!z) && x b) w && x y &&!x c) (w && x && y && z) y d) z x && y 18. (5 pts) Place the following statements in the proper order for correctly using file stream variables in a program (there are 4 required and one optional step). Place the numbers 1 through 5 on the line provided with 1 being the first step. Use the file stream variable in a statement to write to a file or read from a file. Associate a file on the hard drive with a file stream variable using the open function. Declare an identifier to be an input file stream or output file stream variable Close the file stream variable Include the header file fstream Page 5 of 11
19. (4 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 < 4) for (loop_b = 0; loop_b < 4; loop_b++) cout << loop_b; cout << - << loop_a << endl; // line terminate the output here loop_a++; 20. (4 pts) Consider the following segment of code int count = 10; // declare and initialize the variables int sum = 0; while (count > 0) sum += count; cout << sum << endl; count--; Rewrite the entire code segment above as a for loop such that the same output is obtained. Page 6 of 11
21. (4 pts) Show the output for the following segment of code. Remember \n indicates the new line character. The contents of the standard input stream (cin) are shown at the right. Reading marker is on the A. string str; int num1; char ch; cin.get(ch); cin >> num1; getline(cin, str, : ); cin.get(ch); cin.ignore(3, A ); cin >> num1; Contents of cin A1234BCD:E12345 cout << num1 << - << ch << - << str; Place a single character in each box, skip a box to indicate a space, skip a row to indicate a blank line. 22. (4 pts) For the following code segment, what is the output printed to the terminal? int num = 15; if (num <= 10) cout << less than 10 << endl; if (num == 10) cout << equal to 10\n ; if (num > 5) cout << greater than 5 << endl; else cout << greater than 10\n ; cout << num is << num << endl; Spaces above are represented as. Place a single character in each box, skip a box to indicate a space, skip a row to indicate a blank line. Page 7 of 11
23. (8 pts) Write a segment of code that performs the following: - prompts for the name of an input file to open, reads the file name and opens the file, - Prints out a message stating a successful or unsuccessful open of the file - For this segment of code, use the following declared variables only. string file; ifstream infile; 24. (6 pts) Finish the segment of code using an if-then-else-if statement such that the following phrases are printed out for the temperatures indicated: Ice if temperature entered is less than 32 Water if temperature entered is less than 212 and greater than or equal to 32 Steam if temperature entered is greater than or equal to 212 int temp; cout << Enter in a temperature (integers only): ; cin >> temp; // Place if-then-else-if testing structure below this statement Page 8 of 11
25. (8 pts) Using a priming read, write a while loop segment of code that keeps reading integers until the user enters in an integer that is greater than or equal to 1000. The loop will sum the integers entered, and the program prints out this sum when the loop exits. Declare all variables required by your code segment. Assume user enters integers only Page 9 of 11
26. (12 pts) Write a complete program that uses a loop to read an unknown number of words separated by spaces from a single line of data entered by the user. The final word on the input line is the sentinel word EOL. The program is to print out how many words were present on the input line excluding the sentinel character word. Do not forget any header files that may be necessary and remember to declare all variables used in the program. Input is from the standard input stream (cin), and output is to the standard output stream (cout) Sample input line #1: Hello World Today is Thursday EOL Sample output #1: There were 5 words on the line Sample input line #2: Hello World EOL Sample output #2: There were 2 words on the line Page 10 of 11
Extra Credit #1(3 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 = 4; while ( loop_a > 0) for (loop_b = 0; loop_b < loop_a; loop_b++) cout << loop_b; cout << - << loop_a << endl; loop_a--; Extra Credit #2 (3 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 = 1; int remain; // remainder of integer division while( loop <= 12) remain = loop%4; if (remain!= 0) cout << remain; // note: no endl else cout << - << loop << endl; loop++; Page 11 of 11