You are on page 1of 11

EECS

183 Fall 2012 Exam 3


Part 1 (90 points 6 pts each
15 questions with 2 freebees)
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

Multiple Choice Questions

Form 1
Instructions: Read Carefully!
1. On the scantron sheet, bubble & enter
your name and UMID and Form number
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++.
7. All code and input is written in a fixed width font. If it looks like a space, it is a space.
8. This course operates under the rules of the College of Engineering Honor Code. Your
signature endorses the pledge below. After you finish your exam, print your uniqname and
sign below:

Print clearly:

UM unique name: ________________________ @umich.edu


Sign:

I have neither given nor received aid on this examination, nor have I concealed any
violations of the Honor Code. __________________________________________________________________

(Intentionally left blank)

1. Consider the following code fragment.



struct Cat {
string name;
};
struct Dog {
string name;
};
struct CatDog {
Cat cats[4];
Dog dogs[5];
int age;
};

CatDog catDog;

What is the compile-time type of catDog.cats[3]?

A) Array of Cat
B) Cat
C) int
D) string
E) CatDog


2. Which of the following is a correct function prototype?
A) void angry(int& beavers[2][2]);
B) void angry(int beavers[2][ ]);
C) void angry(int beavers[2][2]);
D) All of the above
E) A and C


3. What does the following code print?

char theme[2];
theme[0] = 'A';
cout << theme[0] << ' ' << theme[1] << endl;

A) A <Enter>
B) A<Enter>
C) Nothing, there is a compile error.
D) Results are undefined.




3

4. Consider the following code.



void daria(ifstream& sick_sad_world) {
char c;
sick_sad_world.get(c);
cout << c;
}

Which of the following statements is false?
A) ifstream is a class
B) sick_sad_world is a reference to an object
C) get is a member function of the ifstream class
D) c is passed by value to get

5. Given the following code, what happens?

class Person {
private:
string name;
string favoriteThing;
public:
Person(string inName, string inFavoriteThing) {
name = inName;
favoriteThing = inFavoriteThing;
}

void print() {
cout << name << " likes " << favoriteThing << endl;
}
};

Person ed("Ed", "Chickens Eddy!");
Person jonny("Jonny 2x4", "Plank");
ed.print();
jonny.print();

A) Ed likes Chickens Eddy!
Jonny 2x4 likes Plank
B) Ed Chickens Eddy!
Jonny 2x4 Plank
C) Ed "likes" Chickens Eddy!
Jonny 2x4 "likes" Plank
D) Ed likes Chickens
Jonny likes Plank
E) Jonny 2x4 likes Plank
Ed likes Chickens Eddy!

4

6. Consider the following code. What is the output? Read Carefully!



struct Ingredient {

string name;
};

void set(Ingredient i1, Ingredient i2, Ingredient i3, Ingredient i4) {

i1.name = "sugar";

i2.name = "spice";

i3.name = "everything nice";

i4.name = "ingredient X";
}

int main() {

Ingredient secretIngredients[4];

for (int i = 0; i < 4; ++i) {
secretIngredients[i].name = "?";
}


set(secretIngredients[0], secretIngredients[1],
secretIngredients[2], secretIngredients[3]);

for (int i = 0; i < 4; ++i) {
cout << secretIngredients[i].name << ' ';
}

return 0;
}

A) sugar spice everything nice ingredient X
B) sugarspiceeverything niceingredient X
C) ? ? ? ?
D) ????


7. Consider the following code.

bool tomCatchesJerry = tomFasterThanJerry || jerry > 5;

Oh no! Currently Tom catches Jerry! How would you write the complement of the conditional
expression? Only you can save Jerry!

A) static_cast<bool>(nigelThornberry)
B) !tomFasterThanJerry && jerry <= 5
C) !tomFasterThanJerry || jerry > 5
D) tomFasterThanJerry || jerry <= 5
E) tomFasterThanJerry && jerry > 5

5

8. Consider the following code.



enum EnterLab { YES, NO, FILENOTFOUND };
enum Character { DEEDEE, MANDARK, DEXTER, DERPDERP };

int main() {
EnterLab permission = YES;

int charVal = 0;

cin >> charVal;

Character c = static_cast<Character>(charVal);

if (c == DEEDEE || c == MANDARK) permission = NO;

else if (c == DEXTER) permission = YES;


cout << permission << endl;

return 0;
}

What does the code print if the user enters : 2
A) YES
B) NO
C) 0
D) 1


9. Consider the following code.

enum Direction { LEFT = -1, NONE = 0, RIGHT = 1 };
const string days[7] = {
"Monday", "Tuesday", "Wednesday", "Thursday",
"Friday", "Saturday", "Sunday"
};

int day;
for (day = 0; days[day] != "Saturday"; day += RIGHT) {
cout << day << ' ';
}
cout << "One Saturday Morning!";

What prints?

A) 0 1 2 3 4 5 One Saturday Morning!
B) One Saturday Morning!
C) 0 1 2 3 4 One Saturday Morning!
D) 0 RIGHT 2 3 4 5 One Saturday Morning!
E) The code accesses out of bounds.

6

10. Given the following code, what prints on a call to brain_status()?



enum Pinky {
EGADS = 50, NARF, POIT, TROZ
};

void brain_status () {
Pinky pinky = NARF;

switch (pinky) {
case EGADS:
cout << "Are you pondering what I'm pondering?" << endl;
break;
case NARF:
cout << NARF << endl;
break;
case POIT:
cout << "Try to take over the world!" << endl;
break;
default:
cout << "Same thing we do every night - " << endl;
}
}

A) Are you pondering what Im pondering?
B) NARF
C) 51
D) Same thing we do every night -
E) Compile error

11. Consider the following code.

class ButteredToast {
public:
void haHawHawHa();
};

ButteredToast toast;

Which of the following is false:

A) toast is an object.
B) haHawHawHa is a method (member function) on ButteredToast objects.
C) toast is a class.
D) toast is a variable.

7


12. Which of the following is a correct implementation of isFull given the following RME:
(Project 5 based - Connect4 game board)

// Requires: gameBoard has been initialized
// Effects: Returns true if the gameBoard is full, false otherwise
bool isFull(const char gameBoard[NUM_ROWS][NUM_COLS]);

Remember: const char EMPTY = ' ';

A) for (int i = 0; i < NUM_ROWS; ++i) {

for (int j = 0; j < NUM_COLS; ++j) {

if (gameBoard[i][j] != EMPTY) return true;

}
}
return false;

B) for (int i = 0; i < NUM_COLS; ++i) {
if (gameBoard[NUM_ROWS - 1][i] == EMPTY) return false;
}
return true;

C) for (int i = 0; i < NUM_ROWS; ++i) {

for (int j = 0; j < NUM_COLS; ++j) {

if (gameBoard[i][j] == EMPTY) return false;

}
}
return true;

D) for (int i = 0; i < NUM_COLS; ++i) {
if (gameBoard[0][i] != EMPTY) return true;
}
return false;

E) B and C

13. What is the effect of the following code? (Be careful on this one.)

void digimon(string& digitalMonsters) {

char c = digitalMonsters[0];
for (int i = 1; i < digitalMonsters.length(); ++i) {

digitalMonsters[i-1] = digitalMonsters[i];
}
digitalMonsters[digitalMonsters.length() - 1] = c;
}

A) Rotates all of the letters in digitalMonsters to the left.
B) Rotates all of the letters in digitalMonsters to the right.
C) The code goes out of bounds under some conditions.
D) The code goes into an infinite loop.


14. Given the following code:
int rocket_power() {
ifstream ins;
ins.open("fish_taco_count.txt");

int count = 0;
string woogidi;
ins >> woogidi;

while (ins.good()) {
ins >> woogidi;
count++;
}

return count;
}

What is returned by rocket_power() if the input file contains the following?
Twister ate 10 fish tacos.
Otto ate 3 fish tacos.
Reggie ate 5 fish tacos.

A) 0
B) 15
C) 2
D) The code does not compile



15. Given the following code:


#include <iostream>
using namespace std;

class StoopKid {
public:
void status(bool isOnStoop, int daysOnStoop);
private:
bool onStoop;
int daysOnStoop;
};

void StoopKid::status(bool location, int days) {
onStoop = location;
daysOnStoop = days;
}

int main() {
StoopKid monthOfStoopKid[31];
for (int i = 0; i < 30; ++i) {
monthOfStoopKid[i].setStoopKidStatus(true, i+1);
}
monthOfStoopKid[30].setStoopKidStatus(false, 0);

if (!monthOfStoopKid[30].onStoop) {
cout << "Stoop Kid left his stoop!" << endl;
} else {
cout << "Stoop Kid afraid to leave stoop." << endl;
}

return 0;
}

What does the above program print?
A) Stoop Kid left his stoop!
B) unpredictable due to uninitialized variables
C) the above code causes a compile error
D) Stoop Kid afraid to leave stoop.
E) the above code causes a runtime error


16. Which of the following correctly declares and initializes a 2D array
A) int DeeDee[][3] = { {1, 2, 3}, {1, 2, 3}, {1, 2, 3} };
B) int Laboooratory[][] = { {1, 2, 3}, {1, 2, 3}, {1, 2, 3} };
C) int Mandark[3][3];
D) All of the above


10


17. Consider the following code. (Time consuming)

struct Looney {

string season;
};

int main() {
Looney toon;
bool duck = true;
bool rabbit = false;
int i = 0;

while (i < 3) {
if (duck) {
toon.season = "DUCK SEASON";
rabbit = !rabbit;
}
if (rabbit) {
toon.season = "RABBIT SEASON";
duck = !duck;
}
cout << toon.season << "! ";
i++;
}
}

What prints!!!? (after all it is Looney Toons)
A) RABBIT SEASON! RABBIT SEASON! DUCK SEASON!
B) DUCK SEASON! RABBIT SEASON! DUCK SEASON!
C) RABBIT SEASON! DUCK SEASON! RABBIT SEASON!
D) RABBIT SEASON! RABBIT SEASON! RABBIT SEASON!
E) This enters an infinite loop

11

You might also like