HW1 Answer Key 1) 4 The first part of the expression evaluated is (3.0 + 2). The 2 is converted to 2.0, and the addition is calculated giving 5.0. The expression to evaluate is 4.0 / 5.0 * 2. Both / and * have the same precedence, so 4.0 / 5.0 is evaluated first (left to right rule), giving 0.8 * 2 as the remaining expression. The 2 is converted to 2.0 and the result is 1.6. 2) 3 The 7 / 3 is evaluated first, because / and * have the same precedence. This is integer division, so the result is 2. Now evaluate 2 * 3, which is 6. 3) 1 The 4 + 3 is evaluated first, because it's in parenthesis. The expression to evaluate becomes 14 % 7 + 1 % 4. The remainder operator % has higher precedence than +. So, 14 % 7 is evaluated giving 0 and 1 % 4 is evaluated giving 1. Compute 0 + 1 to get 1. 4) 5 The 8 / 5 is evaluated first using integer division to give 1. 1 + 1.0 is then evaluated, converting 1 to 1.0 first, to get 2.0. Because we're storing into a floating point variable, the answer is not 2, but is instead "None of these". 5) 5 The 8 / 5.0 is evaluated first, converting 8 to 8.0 and then performing floating point division to get 1.6. Then 1.6 + 1 is evaluated, first converting 1 to 1.0, to get 2.6. We're assigning the result to an integer, so the value is truncated to 2, giving the answer "None of these". 6) 3 The evaluation happens just like problem 5, but we're storing the result into a double variable, so the correct answer is that 2.6 is assigned. 7) 8 The text cout is an identifier because it follows the identifier rules (starts with an _ or letter and has _, letters, or digits following). It is not a keyword because you can declare your own variables named cout (although you wouldn't want to do this). The textbook contains a complete list of keywords. You can also tell because cout is not printed in blue when using Visual C++. Since we're sending information to the screen or out of the program, cout is not an input stream, but it an example of an output stream. So the correct answer is 8, both 1 and 4. 8) 4 When the statement cin >> anInt >> aChar; is executed, the value 2 is read into anInt, and the read marker is placed on the tab just after the 2. We're using the extraction operator >> to read in a character, so white space, that is, the tab, is skipped and the next character is stored in aChar. So, '-' is read and stored in aChar. 9) 1 When the statement cin >> aDble >> anInt >> aChar; is executed, the value 2 is read into aDble, white space is then skipped and the value -3 is read into anInt. Since the decimal point is not a valid character for integers, the read marker stops on the decimal point, and then when a character is read, the decimal point is placed into aChar and the read marker is left on the 4. So '.' is stored in aChar. 10) 3 This is reading just the first two values as described in problem 9. Beware that '-3' is not correct for two reasons. First, it's denoted as a character constant and we're storing in an integer. Second, '-3' isn't a valid character constant anyway. 11) 5 The 2 is read into anInt and the read marker is placed on the tab just following the 2. Using get to read in a character, nothing is skipped, so the tab, '\t' is placed into aChar and the read marker ends up on the - sign. When the second integer is read, the - sign and the 3 are read because those are valid for integers, so anInt ends up with the value -3, which isn't listed as an answer. 12) 3 Using setw(3), sets aside space to print into which is 3 characters wide. The expression 30 + 12 evaluates to 42, which is only two characters wide. This leaves a space between "is" and 42 when printed, because the default is to right justify output into the space declared by setw(...) 13) 4 Clearly the first two answers are not correct because of operator precedence. The third one is not correct because / and * have the same precedence, so we evaluate the expression from left to right. So (c * c * a - b) / d will be calculated and then multiplied by e. 14) 1 Because we're initializing to double variables, all of the integer literal constants are converted to double values. Summing up all of the values is 21.0 which is then divided by 4. Since we're mixing floating point and integer, the 4 is converted to 4.0. Floating point division is performed, resulting in 5.25. 15) 4 All of these work fine, because setw(4) sets aside 4 spaces for printing into, and the value 2 is printed right justified into that space. Most people chose only 1. 16) 3 Look at the precedence tables in the notes and in the book. 17) 8 Answer 1 doesn't work because first the value 23 is read into A, then the colon is ignored, placing the read marker at the 5 starting 59. So when a value for B is extracted, the number 59 will be read and the read marker will be left on the period. Answer 2 works fine. First, everything up to and including the colon is ignored, placing the read marker on the 5 starting 59. The value 59 is extracted for A, and the read marker is left on the period. The period is read into ch using the get function, leaving the read marker on the 5 in 58. Finally, the value extracted for B will be the 58. Answer 3 works fine. First, everything up to and including the period is ignored, placing the read marker on the 5 starting 58. So the value extracted for B is 58. Answer 4 does not work, for several reasons. First, the get function expects a character variable not an integer variable as its parameter. Second, if A and B were character variables, the 5 and 9 would be read for A and B respectively, which is not what we wanted. 18) 3 Answer 1 fails to work, because if the label has spaces, the extraction operator won't read in the entire label, and if there were no spaces, the extraction operator would read in the entire line. Answer 2 fails to work, because the entire line is read into Label using getline. Answer 3 works. The whole label is read into Label by getline, which stops at the vertical bar since we passed that in as the third parameter. The vertical bar is read, but not stored, leaving the read marker on the 6. Then, when Speed is extracted, the value 698 (or any integer value following the |) is placed into Speed. Answer 4 does not work because the second parameter for getline must be a string variable, and Speed is an int. 19) 8 Everything up to and including the first 7 in Data.txt is ignored, leaving the read marker on the first 8. The next character is extracted, so '8' gets stored in Value, which is then printed. 20) 4 The '\0' character is the null character and it matches nothing except the end of the file. So, we will ignore the first 14 characters of the file. The first 10 characters are the digits 1234567890 on the first line. The next character is the newline character, leaving the 123 on the second line to be ignored. So the read marker is left on the 4 in the second line. That is read in using get, storing '4' in Value, which is then printed. 21) 10 The using namespace std; directive is always needed, so for any answer to be valid, 4 must be part of it. Answers 1 and 2 are used to include support for strings and file input/output. Answer 3 is what is used to include support for keyboard/screen input/output. 22) 6 If a program runs to completion, that means its syntax must be correct, or the compiler wouldn't compile it. It also means the program linked correctly, or an executable file would not be generated. The program loads properly, because the executable file must be loaded into memory for a program to run. The program does not have an execution error though, because it runs to completion and is just printing out incorrect results. 23) 1 This is an example of a syntax error. Forgetting a comma when declaring multiple variables of the same type breaks the syntax rules for variable declaration. 24) 5 The contents of an executable file are placed into memory. The rest of the answers are not placed in memory when you run your program. For example, none of those other things are placed into memory when you run Internet Explorer. 25) 8 When you build your program in Visual C++, your program is compiled and linked. Editing happens before compiling and loading happens when you run your program, after your program has been linked to generate an executable file.