EECS 183 Winter 2014 Exam 1 Closed Book Closed Notes Closed Electronic Devices Closed Neighbor Turn off Your Cell Phones We will confiscate all electronic devices that we see including cell phones, calculators, etc. Multiple Choice Questions (20 questions * 5 points per question = 100 points) Key 1 Instructions: Read Carefully! 1. You may have one personally handwritten 3 x 5 notecard. 2. Some questions are not simple, therefore, read carefully. 3. Assume all code and code fragments are syntactically valid, unless otherwise specified. 4. Assume/use only the standard C++11. 5. In all the given code, if any character looks like a space, it is one. 6. On the scantron sheet, bubble in your name and UMID - - 10 pts off for incorrect UMID. 7. On the scantron sheet, bubble in Key 1 - - zero on exam if key not bubbled in. 8. Sign below and print your uniqname - - 10 pts off if we cannot read your uniqname I have neither given nor received aid on this examination, nor have I concealed any violations of the Honor Code. 1
(Intentionally left blank such that you may use if needed) 2
1 What does the following expression evaluate to? 5 / 3 * 3 A) 0 B) 3 C) 5 D) None of the above 2 Which of the following evaluates to an odd number? A) 3 * 6 / 2 / 2 B) 6 * 3 / 2 / 2 C) 6 / 2 / 3 * 2 D) 6 / 3 * 3 / 2 3 Which of the following code fragments will assign the number 4.5 to variable y? A) double y = 2.0 + 5 % 2; B) double y = 2 + 5 / 2; C) double y = 2.0 + 5 / 2; D) double y = 2.0 + 1.0 * 5 / 2; E) double y = 2.0 + 5 / 1.0 * 2; 4 What does the expression 1234 / 10 / 10 % 10 evaluate to? A) 1 B) 2 C) 3 D) 4 3
5 If the user types: 2.5 # 3.5 <enter>, what does the following code snippet output? double x = 3; int y = 2; char ch = '6'; cin >> x >> ch >> y; cout << x * y; A) 5.5 B) 6 C) 7 D) 7.5 6 If the user types: 2.5 # 3.5 <enter>, what does the following code snippet output? (Note: this is a different question than the one above - - really it is - - read closely) double x = 3; int y = 2; char ch = '6'; cin >> y >> x >> ch; cout << x * y; A) 0 B) 1 C) 2 D) 2.5 7 If the user types: Go Blue <enter>, what does the following code snippet output? string str; getline(cin, str); cout << str; A) Go B) Blue C) G D) Go Blue 4
8 Consider the following function: bool iscoronationday(int num) { if (num % 100 == 0) { if (num % 400 == 0) { return true; else { return false; else if (num % 4 == 0) { return true; else { return false; Which of the following test suites thoroughly test the above function by testing every path through the function? A) 1999, 2000, 2001 B) 1899, 1900, 1901, 1902 C) 1800, 2000, 2008 D) 1900, 1930, 1960, 2000 9 What does the following line of code print? cout << "\"//Some people are /";//"worth melting for - - Olaf"; A) \ B) " C) "//Some people are / D) None of the above E) Compilation Error 10 Consider the following C++ function: int oaken(int x) { return x + 1; What does the expression oaken(oaken(3)) evaluate to? A) 3 B) 4 C) 5 D) 6 5
11 What does the following code print? #include <iostream> using namespace std; int kristoff(int x); int anna(int x); int main() { cout << kristoff(1) + anna(2); return 0; int kristoff(int x) { ++x; return anna(x); int anna(int x) { x = x + 2; return x; A) 5 B) 6 C) 7 D) 8 E) 9 12 Which of the following calls to the function frozen are valid (i.e., will compile), if frozen has the function prototype: void frozen(int x, char y); A) frozen(1, '*'); B) cout << frozen(1, '*'); C) int x = frozen(10, '*') + 3; D) All of the above E) A and B 6
13 What does the following C++ program print? #include <iostream> using namespace std; void olaf(int x, int y); int main() { int x = 1; int y = 3; olaf(x, y); cout << x << ", " << y; return 0; void olaf(int x, int y) { int temp = x; x = y; y = temp; return; A) 1, 1 B) 1, 3 C) 3, 1 D) 3, 3 14 What does the following code fragment print if the user enters: - 5 12<enter> int temperature, amount; cin >> temperature >> amount; if (temperature > 0) { if (amount >= 10) { cout << "Build me a friend" << endl; else { cout << "Oh no! It's too cold to snow" << endl; A) Build me a friend B) Oh no! It's too cold to snow C) None of the above 7
15 What does the following C++ program print? #include <iostream> using namespace std; int main() { int x = 42; if (x >= 0) { cout << "I don't have a skull. Or bones."; else if (x <= 42) { cout << "I'm Olaf and I like warm hugs."; else { cout << "It's so cute. Like little baby unicorn"; return 0; cout << "Oh, look at that. I've been impaled."; A) I don't have a skull. Or bones. B) I'm Olaf and I like warm hugs. C) It's so cute. Like little baby unicorn D) Oh, look at that. I've been impaled. E) It will run with no errors, but print nothing. 16 Consider the following function marshmallow: int marshmallow(bool p, bool q) { int x = 0; int y = 2; return ((x > 1) p) && ((y < 3) q); What does the function marshmallow return? A) 0 B) 1 C) The return value depends on the values of both p and q D) The return value depends on the value of p but not q E) The return value depends on the value of q but not p 8
17 What does the following code snippet print? int x = 1; while (x < 5) { x *= x + 1; cout << x << " "; A) 1 B) 5 C) 6 D) 1 2 6 E) 2 6 18 Given the following function implementation, what does bulda(5) return? int bulda(int n) { int i = 0; while (i < n) { i = i + 1; n = n + 1; return i; A) 4 B) 5 C) 6 D) Behavior is undefined - - may or may not stop 9
19 If the user types 20 60 0 70 <enter> what does the following code print? int x; int sven = 0; cin >> x; while (x > 0) { if (x > sven) { sven = x; cin >> x; cout << sven ; A) 20 B) 60 C) 0 D) 70 20 Which of the following is a complement of the C++ expression (temperature < 10 temperature > 80)? A) (temperature > 10 && temperature < 80) B) (10 <= temperature <= 80) C) (10 <= temperature && temperature <= 80) D) (temperature >= 10 temperature <= 80) 10