Instructions: Read Carefully! 1

Instructions: Read Carefully! 1

EECS 183 Winter 2012 Exam 2 Part 1 17 questions @ 5 pts each max 80 points (so you get one question on us) Closed Book Closed Notes Closed Electronic Devices Closed Neighbor Turn off Your Cell Phones Multiple Choice Questions Form 1 Instructions: Read Carefully! 1. On the scantron sheet, bubble & enter your name and UMID and Form number and sign your name to indicate the honor pledge below: I have neither given nor received aid on this examination, nor have I concealed any violations of the Honor Code. 2. This is a closed book exam. You may not refer to any materials during the exam. 3. Please turn off your cell phones before the start of the exam. 4. Some questions are not simple, therefore, read carefully. 5. Assume all code and code fragments compile, unless otherwise specified. 6. Assume/use only the standard ISO/ANSI C++. 1 1) Ponder the following code fragment int level; for (level = 10; level < 20; level += 2) { // do nothing } cout << level; What does the above code print? A) 2 B) 10 C) 20 D) 18 E) the code does not compile 2) Ponder the following code fragment for (int level = 10; level < 20; level += 2) { // do nothing } cout << level; What does the above code print? A) 2 B) 10 C) 20 D) 18 E) the code does not compile 2 3) Given the following function definition: void missingNo(int& item) { item *= 10; } Ponder the following code fragment int rareCandies = 4; while (rareCandies != 100) { missingNo(rareCandies); } cout << rareCandies; What does the above code print? A) 4 B) 100 C) 40 D) 400 E) the code enters an infinite loop 4) Ponder the following C++ function (calculate carefully!) int adjustLife(int& health, int damage) { health = health + damage; return health; } If a variable life of type int is initialized to 100 what does the C++ expression adjustLife(life, -20) + adjustLife(life, -40) evaluate to? A) 100 B) 120 C) 140 D) 40 E) -60 3 5) What does the following code fragment print? void levelUp(int level) { level++; } int main() { int levelOfMeowth = 27; cout << "Current Level : " << levelOfMeowth << ". "; levelUp(levelOfMeowth); if (levelOfMeowth > 27) { cout << "Meowth evolved into Persian!"; } return 0; } A) Current Level : 27. B) Current Level : 28. Meowth evolved into Persian! C) Current Level : 27. Meowth evolved into Persian! D) Current Level : 0. E) the code does not compile 6) What does the following code fragment print? void levelUp(int& level) { level++; } int main() { int levelOfSquirtle = 15; cout << "Current Level : " << levelOfSquirtle << ". "; levelUp(levelOfSquirtle); if (levelOfSquirtle > 15) { cout << "Squirtle evolved into Wartortle!"; } return 0; } A) Current Level : 15. B) Current Level : 16. Squirtle evolved into Wartortle! C) Current Level : 15. Squirtle evolved into Wartortle! D) Current Level : 0. E) the code does not compile 4 7) What does the following code print? int main() { int grass[3][3] = { { 0, 0, 1 }, { 0, 0, 0 }, { 1, 0, 0 } }; for (int row = 0; row < 3; row++) { for (int col = 0; col < 3; col++) { if (grass[row][col]) { cout << "Wild Rattata appeared at " << row << ", " << col << "! "; } } } return 0; } A) [Nothing prints] B) Wild Rattata appeared at 3, 1! Wild Rattata appeared at 1, 3! C) Wild Rattata appeared at 1, 3! Wild Rattata appeared at 3, 1! D) Wild Rattata appeared at 2, 0! Wild Rattata appeared at 0, 2! E) Wild Rattata appeared at 0, 2! Wild Rattata appeared at 2, 0! 8) Which of the following conditions should you never use for reading a file inFile in a loop? A) while (!inFile.eof()) B) while (inFile) C) while (inFile.good()) D) while (!inFile.fail()) E) All of the above are good 5 9) Consider the following code (read carefully!): string getPokemonName(int pokemon) { string pokemonName = "???"; switch (pokemon) { case 1: pokemonName = "Bulbasaur"; case 25: pokemonName = "Pikachu"; case 382: pokemonName = "Kyogre"; default: pokemonName = "Unknown (Not Unown!)"; } return pokemonName; } What does the following line print? cout << getPokemonName(25) << endl; A) Pikachu B) ??? C) Unknown (Not Unown!) D) [Nothing Prints] E) Kyogre 10) Consider the following code (read carefully!): // REQUIRES: The size of the array is length; length >= 1 int maxLevel(const int levels[], int length) { int maximum = levels[0]; for (int i = 1; i < length; i += 1) { if (levels[i] > maximum) { return levels[i]; } else { return maximum; } } return maximum; } const int SIZE = 5; int herp[SIZE] = { 1, 6, 18, 3, 9001 }; What does maxLevel(herp, SIZE) return? A) 9001 B) 1 C) 6 D) 0 E) 18 6 11) Consider the following code fragment: int grade = 70; switch ( grade ) { case 100 :cout << "Grade A "; break; case 80 :cout << "Grade B "; break; case 60 :cout << "Grade C "; break; default :cout << "This one is easy!!"; } What prints? A) Grade A B) Grade B C) Grade C D) This one is easy!! E) the code does not compile 12) Which of the following are valid prototypes? A) void catchThemAll(int pokemon[ ][9001]); B) void catchThemAll(int pokemon[9001][-100]); C) void catchThemAll(int pokemon[ ][ ]); D) void catchThemAll(int pokemon[1337][ ]); E) All of the above are valid prototypes. 13) Think critically about which test case exposes the potentially buggy swap implementation (read carefully!): void swappySwap(int& a, int& b) { a = a + b; b = a - b; a = a - b; } A) int x = 5; int y = 6; swappySwap(x, y); B) int x = 6; int y = 5; swappySwap(x, y); C) int x = 5; int y = 5; swappySwap(x, y); D) int x = 5; int y = 5; swappySwap(x, x); E) This implementation of swappySwap is correct 7 14) Consider the following code fragment: ifstream in; in.open("file.txt"); int x, count = 0; char c; while (in >> x) { } // this loop has an empty body in >> c; while (in >> x) { count++; } cout << count; What does the above code fragment print, if file.txt contains 2 4 a 3 4 5 4 ? Please note the whitespaces between the characters. A) 0 B) 1 C) 2 D) 3 E) 4 8 15) Consider the following code fragment: // REQUIRES: The size of the array is n; n>= 1 bool harpDarp(const int array[], int n) { int prev = array[0]; for (int i = 1; i < n; ++i) { if (array[i] < prev) { return false; } prev = array[i]; } return true; } What would misty be initialized to? int first[3] = {3, 6, 6}; int second[4] = {2, 4, 5, 3}; bool misty = harpDarp(first, 3) && harpDarp(second, 4); A) false B) true C) {3, 5, 7} D) {2, 4, 5, 3} E) None of the above 16) Consider the following code fragment: const int SIZE = 1; int arr[SIZE]; for(int i = 1; i <= SIZE; i++ ) { arr[i] = i + 1; } What are the contents of the array arr? A) {1, 2} B) {1} C) {2} D) indeterminate (depends on the compiler) E) the code does not compile 9 17) Consider the following code fragment: const int SIZE = 5; int arr[SIZE] = {0, 1, 2}; Which of the following best represents the contents of the array arr? A) 0 1 2 ??? ??? B) 0 1 2 2 2 C) 0 0 0 1 2 D) ??? ??? 0 1 2 E) the code does not compile 10 .

View Full Text

Details

  • File Type
    pdf
  • Upload Time
    -
  • Content Languages
    English
  • Upload User
    Anonymous/Not logged-in
  • File Pages
    10 Page
  • File Size
    -

Download

Channel Download Status
Express Download Enable

Copyright

We respect the copyrights and intellectual property rights of all users. All uploaded documents are either original works of the uploader or authorized works of the rightful owners.

  • Not to be reproduced or distributed without explicit permission.
  • Not used for commercial purposes outside of approved use cases.
  • Not used to infringe on the rights of the original creators.
  • If you believe any content infringes your copyright, please contact us immediately.

Support

For help with questions, suggestions, or problems, please contact us