CS 1044 Homework 2 Key Spring 1999 Q Ans Reason 1 B 12.0 / 5.0 + 2 / 5 == 2.4 + 0 == 2.4 Note: 2 / 5 is done as int arithmetic. 2 D 10 / 3 == 3 (int arithmetic), then multiply that by 5 3 C 10 % 5 == 0 since 5 divides into 10 without a remainder; 2 % 5 == 2 since 5 goes into 2 zero times, with a remainder of 2 4 E 15 / 1.2 == 12.5 5 B 5 / 2 == 0 (int division); assigning to a double makes it 2.0 6 A 7.0 / 4.0 == 1.75; assigning to an int truncates the value to 1 7 A 8 A The caret symbol (^) actually IS a valid arithmetic operator in C++, representing the bitwise exclusive or. But we haven't covered that yet, so you may have said it's invalid syntax. Either A or B will be counted correct here. 9 A 10 B You can't extract from the output stream. 11 D cin >> anInt // This extracts 1 into anInt >> aChar; // This extracts the next character, '.' into aChar 12 B cin >> aDble // This extracts 1.2 into aDble >> aChar; // This extracts '4' into aChar 13 B cin >> aDble // This extracts 1.2 into aDble >> anInt; // This extracts 4 into anInt 14 B cin >> anInt; // This extracts 1 into anInt cin.get(aChar); // This gets '.' and places it into aChar cin >> anInt; // This extracts 2 into anInt 15 C The expression 30 + 12 is evaluated, and its value is printed in a field that's 3 columns wide. 16 C This is the only one that correctly groups a+b and d+e. 17 C (4 + 8 + 5 + 4) / 4 == 21 / 4 == 5 (int division); assigning that to a double converts it to 5.0 18 D 19 A Direct from slide on operator precedence. 20 B From class discussion, int and double values are stored in a different format, even if the values are (mathematically) the same.