Name Closed notes and book. If you have any questions ask them. Write clearly and make sure the case of a letter is clear (where applicable) since C++ is case sensitive. You can assume that there is one space between words unless otherwise noted. If necessary, the symbol indicates a space, and it is used when two or more spaces are together. For this test the two-character sequence \n is to be taken to mean the newline character. 1) (12 pts) True/False questions. Circle T or F T F a) The function main() is required for every C++ program. T F b) The extraction operator skips whitespace characters before reading a single characte T F c) A value returning function can be invoked (called) within an expression.. T F d) The getline function reads in an entire line skipping any white spaces. T F e) The C++ compiler finds syntax errors? T F f) Block statements are not terminated with a semi-colon. T F g) The bool data type has a domain consisting of just two values. T F h) Type coercion is the implicit conversion of one data type to another. T F i) Algorithms are a step-by-step procedure for solving a problem in a finite amount of time.. T F j) average = (float)(average) + sum; is a valid assignment statement. T F k) Relational expressions consist of expressions and logical operators T F l) A relational expression can be a logical expression. 2) (3pts) Identifiers can consist of what characters (three distinct answers)? 3) (3pts) An expression is an arrangement of, and that can be evaluated to compute a value of a given type. Page 1 of 11
4) (2 pts) What are two methods used to indicate comments in a C++ program? 5) (3 pts) What are the three logical operators for C++?(show the symbols used) 6) (6 pts) For the operators shown below, CLEARLY indicate if the operator is: Relational (use an R), or Other (use an O). NOTE: that there are no spaces between characters even though it may appear that there is a blank in some of the two character sequences. a) = b) c) > d) >! d) >= e)! f)!= g) =! h) <= i) && j) < k) == Multiple choice Circle all correct answers 7) (2 pts) Which of the following are control structures that can be used to structure statements in a program? A) Subprograms B) Indexing C) Looping D) Compiling E) Execution F) Forwarding G) Coupling H) Selection 8) (2 pts) C++ is an example of A) An assembler B) A Machine language C) Assembly Language D) A Compiler E) A Low Level Language F) None of the above 9) (2 pts) When used with an input file stream variable, which statements below are true about the open function? (circle all that are true) A) If the file does not exist, it creates a new, empty file. B) It sets the reading marker at the first character in the file. C) If the file does not exist, it puts the stream into the fail state. D) It associates the name of a stream variable with the name of a physical disk file. E) None of the above are true Page 2 of 11
10) (2 pts) What happens when a C++ input stream enters the fail state? A) The system does not display an error message, and program execution is terminated. B) The system displays an error message, and program execution is terminated. C) The system does not display an error message; the program continues running, and further input operations with that stream are ignored. D) None of the above 11) (2 pts) Assume that the Boolean variable X has a logical value true and that the Boolean variable Y has the logical value false. What is the logical value of the following logical expression? (Y && (X!X)) A) true B) false C) true D) false E) None of the above 12) (2 pts) Which of the following are required in order to use files in a C++ program A) Use a preprocessor directive to include the header file fstream B) Perform a priming read on each input file C) Erase the contents of each output file D) Declare a file stream variable for each file stream using a variable declaration statement E) Specify the name of the file stream in each input or output statement F) All of the above G) None of the above 13) (10 pts) For each identifier below, indicate which are valid and which are not. For those identifiers that are not valid, give the reason why. a) 5_speed Valid Invalid b) MyWay Valid Invalid c) ab&c Valid Invalid d) clock2 Valid Invalid e) Money$ Valid Invalid Page 3 of 11
14) (2 pts) In the following code segment, what is the best possible data type of the variable named mystery? (be careful on this one)???? mystery; A) int int x ; B) float mystery >> x; x = x*5; C) ofstream D) ifstream E) Can t determine from given info 15) (2 pts) In the following code segment, what is the best possilbe data type of the variable named mystery????? mystery; A) int int x ; string line; B) string getline(cin, line, mystery); C) char 16) (6 pts) Assignment Statements D) bool E) Can t determine from given info a) Write a statement to assign the word Hello to the string variable str1. b) Write a statement to assign the value 75 to the int variable num. c) Write a statement to assign the letter A to the char variable grade. 17) (4 pts) Write a code segment that uses one or more output statements that writes (to the standard output stream cout) the value in the variable money in eight spaces (left justified). Page 4 of 11
18) (8 pts) Declaration Statements: Declare the following identifiers as constants of the data type specified with the values provided a) Provide an integer constant declaration for the identifier SIZE with a value of 5. b) Provide a char constant declaration for the identifier GRADE with a value of A. c) Provide an int variable declaration for the identifier number. d) Provide a double variable declaration for the identifier average. 19) (3 pts) What is the output for the following segment of code? int num = 10; cout << num << endl; --num; cout << num*2 << endl; num++; cout << num + num << endl; 20) (8 pts) Given the following values for the Boolean variables w, x, y and z: w = false, x = true, y = false, z = true Clearly indicate whether each logical expression is true or false. a) w &&!y z && x b)!w!x && y c) (!w && y) (z && x) d) (y w) && z x Page 5 of 11
21) (10 pts) Match the words with their definitions. Choose the best definition for each word. a) Semantics b) Syntax c) Variable d) Identifier e) Expression f) Type Coercion g) Literal Value h) Data Type i) Declaration j) void Function A) Definition is not listed below (This answer can be used more than once if necessary) B) A specific set of values along with a set of operations on those values. C) The set of rules that determines the meaning of instructions written in a programming language. D) Arrangement of identifiers, literals and operators that can be evaluated to compute a value of a given type. E) A statement that associates an identifier with a data object, a function or a data type. F) A Name associated with a function or data object and used to refer to that function or data object. G) A location in memory, reference by an identifier, which contains a data value that cannot be changed H) The formal rules governing how valid instructions are written in a programming language. I) The implicit conversion of a value from one data type to another. J) A function that returns a single function value to its caller K) A function that does not return a function value to its caller 22) (6 pts) Finish the segment of code using an if-then-else-if structure such that the following phrases are printed out for the speeds indicated: Too Slow if speed is less than 30 Just Right if speed is less than 60 and greater than or equal to 30 Too Fast if speed is greater than or equal to 60 int speed; cout << Enter in a speed (integers only): ; cin >> speed; // Place if-then-else-if testing structure below this statement Page 6 of 11
23) (6 pts) Show the output for the segment of code below. Remember \n indicates the new line character, and the character indicates a space The input file (Input.txt) for the code below is shown on the right: string phrase; Input.txt int num1; 100\n char ch; 50ABC6 DEF Hi There\n ifstream InFile; Hello World\n InFile.open( Input.txt ); InFile >> num1; // read in an integer InFile >> num1; // read in another integer InFile.get(ch); InFile.ignore(7, \n ); InFile >> phrase; InFile.get(ch); getline(infile,phrase, \n ); cout << num1 << endl; cout << - << ch << - << endl; cout << phrase << endl; 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
24) (8 pts) Given the following declarations, show the output for each separate code segment. In the code, a indicates a space string str1 = First string to analyze ; string str2 = Second STRING ; string str3; string::size_type length, position; a) length = str2.size(); cout << length; b) position = str1.find( Str ); cout << position; c) position = str2.find( T ); cout << position; d) str3 = str1.substr(str1.find( r ),6); cout << str3; 25) (8 pts) For the two code segments shown, add code to open, in the manner specified, an input file named input.txt. a) For this code segment use the literal value of the file name. ifstream infile; // place open statement using a literal value below this line b) For this code segment the file name is stored in a string variable, and the string variable is used for opening the input file. ifstream infile; string filename = input.txt ; // place open statement using a string variable below this line Page 8 of 11
26) (6 pts) Consider the standard input stream cin containing the values shown. The reading marker starts on the 0 (zero), the character - - indicates a space and \n represents the new-line character. Contents of Input Stream for cin: 0 1 2 3\n4 5 6\n7 8 9\n Given the input stream above, what values are output by the cout statements when the code segment is executed? Complete the partially supplied output. int num1; char ch1, ch2; cin.ignore(5, \n ); cin >> ch1; cin.ignore(200, 5 ); cin >> num1; cin.ignore(200, \n ); cin >> ch2 >> ch2; cout << num1 = << num1 << endl; cout << ch1 = << ch1 << endl; cout << ch2 = << ch2 << endl; num1 = ch1 = ch2 = 27) (6pts) Show PRECISELY the output of the following statements. Place a single character in each box, skip a box to indicate a space, and skip a row to indicate a blank line. In the code, a indicates a space. cout << \nblank Line First << endl; cout << \nblank line? << endl; cout << the next line << endl; cout << setw(2) << is last << endl; Page 9 of 11
28) (8 pts) Show the output of each statement below. Place a single character in each box, Skip a box to indicate a space. a) cout << setw(3) << right << Hello << setw(7) << World ; b) cout << setw(8) << left << Number: << right << setw(6) << Five ; 29) (8 pts) Write a segment of code that prompts the user for the price of an item and the quantity of that item purchased. These values are read in and the total cost of the purchase(quantity times item price) is calculated and output. Assume that all header files have been declared, and use the following variable declarations. int number; float cost; float total; // number of items purchased // cost for one item // total cost of the order Page 10 of 11
Extra Credit #1) (3 pts) After execution of the following code segment what logical value (true or false) is contained in the Boolean variable result. Explain your answer. If the segment will not compile, state that fact and explain why it won t compile. bool result; int num1, num2; num1 = 5; num2 = 4; result = num1 = num2; Extra Credit #2) (2 pts) The following C++ statements are to be included in a program. What is the correct data type needed for the variable len? (only one possible answer for this question) string firstname= Reginald ;????? len; len = firstname.substr(5,5); A) float B) string::size_type C) string D) bool E) None of these Page 11 of 11