Multiple Choice Questions

EECS 183
Winter 2016
Closed Book
Closed Electronic Devices
Exam 1
3”× 5” Notecard
Closed Neighbor
Turn Off Your Cell Phones
We will confiscate all electronic devices that we see - including cell phones, calculators,
smart watches, etc.
Multiple Choice Questions
(20 questions × 6 points per question = 120 points)
KEY 1
Instructions: Read Carefully!
1.) You may have one personally handwritten 3”× 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, bubble in your name and UMID. 10 pts off for incorrect UMID.
7.) On the scantron, bubble in Key 1. 10 pts off for missing or incorrect key.
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.
Signature
uniqname
1
This page is intentionally left blank. You may use it as scratch paper if needed.
2
1.) What does the following code print?
int x = 5;
x = x + 4.3;
cout << x;
A.
B.
C.
D.
E.
9.3
54.3
9
10
5
2.) What does the following code print?
char x[] = {'a','b','c'};
x[1] = 'e';
cout << x[0] << " " << x[1] << " " << x[2];
A.
B.
C.
D.
E.
a b c
ebc
e b c
a e c
Nothing. It does not compile because the array size is not declared.
3.) What is the value of snow after the following code executes?
bool precipitation = true;
int temperatureF = 40;
bool snow = (precipitation && (temperatureF < 32));
A.
B.
C.
D.
E.
true
false
32
40
None of the above.
4.) What will happen if a function’s local variable has the same name as a global variable?
A.
B.
C.
D.
E.
The name will refer to the local variable within the function.
It will not compile because two variables can’t have the same name.
The name will refer to the global variable within the function.
The local variable’s value will be assigned to the global variable.
A and D.
3
5.) What does the following code print?
string mouse = "Mickey";
string dog = "Pluto";
string duck = "Donald";
mouse = dog;
duck = mouse;
dog = duck;
cout << "mouse " << "duck " << dog;
A.
B.
C.
D.
E.
Mickey Mouse
mouse duck dog
Mickey Pluto Donald
mouse duck Pluto
Pluto Pluto Pluto
mouse duck Donald
6.) What does the following code print?
for (int i = 3; i < 19; i += 2) {
cout << i << " - ";
}
A.
B.
C.
D.
E.
3
3
3
5
5
-
4
5
5
7
7
-
5
7
7
9
9
-
6 - 7 - 8 - 9 9 - 11 - 13 - 15 - 17 9 - 11 - 13 - 15 - 17 - 19 11 - 13 - 15 - 17 11 - 13 - 15 - 17 - 19 -
7.) Which of the following statements about strings are always true?
I. A longer string is always greater than (>) a shorter string.
II. A string can be thought of as an array of type char.
III. If you include the string library, you do not need to write using namespace
std;
IV. To read input into a string, one must use getline(cin, stringname) rather than
cin >> stringname.
A.
B.
C.
D.
E.
I only
II only
I and II
II and III
I, III, and IV
4
8.) Which of the sets of values for pinocchiNose ensures that
every branch of the following code is tested?
if (pinocchiNose >= 0) {
if (pinocchiNose > 0) {
if (pinocchiNose % 2 == 0) {
cout << "Branch 1" << endl;
} else if (pinocchiNose % 3 == 0) {
cout << "Branch 2" << endl;
} else {
cout << "Branch 3" << endl;
}
} else {
cout << "Branch 4" << endl;
}
} else {
cout << "Branch 5" << endl;
}
A. -1, 0, 4, 5, 6
Pinocchio
B. -5, -1, 0, 6, 9
C. -4, -3, 1, 5, 7
D. -4, 0, 4, 5, 9
E. -3, 0, 4, 5, 7
9.) What is the output of the following code?
int wolf = 7 / 3;
if (wolf == 3) {
cout << "Third Little Pig" << endl;
} else if (wolf != 0) {
cout << "Second Little Pig" << endl;
} else {
cout << "First Little Pig" << endl;
}
A. Third Little Pig
B. Second Little Pig
C. First Little Pig
D. Second Little Pig
First Little Pig
E. None of the above.
5
10.) What is the output of the following code?
int x = 7;
int i = 2;
while (x % i > 0) {
cout << i << ", ";
++i;
}
cout << "Done!";
A.
B.
C.
D.
E.
1,
2,
2,
3,
3,
1,
3,
3,
4,
4,
3,
4,
4,
5,
5,
2,
5,
5,
6,
6,
1, Done!
6, Done!
6, 7, Done!
Done!
7, Done!
Use the following code to answer the next two questions.
1
2
3
4
5
6
7
8
9
10
string dwarfs[] = {"Doc", "Happy", "Sneezy", "Bashful",
"Sleepy", "Grumpy", "Dopey"};
int j = 0;
for (int i = 0; i < 7; i = j + 2) {
j += i;
if (i > 2) {
cout << dwarfs[i] << " " << dwarfs[j] << " ";
}
}
11.) What will print?
A.
B.
C.
D.
E.
Sleepy Dopey
Doc Doc
Sneezy Sneezy
Bashful Grumpy
Happy Happy
12.) What will be the values of i and j at line 10?
A.
B.
C.
D.
E.
i: 7, j: 6
i: 8, j: 6
i: 8, j: 14
i is out of scope, j: 2
i is out of scope, j: 6
6
13.) Which of the following loops will iterate 5 times?
A.
B.
C.
D.
E.
for (int i = 0;
for (int i = 1;
for (int i = 1;
for (int i = 1;
All of the above.
i
i
i
i
<= 5; i++) {}
< 5; i++) {}
< 6; i++) {}
<= 4; i++) {}
14.) What is the output of the following program?
#include<iostream>
using namespace std;
int doubleTrouble(int & si, int am);
int main() {
int a = 90;
int b = 80;
cout << doubleTrouble(a, b) << " " << a << " " << b;
return 0;
}
int doubleTrouble(int & si, int am) {
si *= 2;
am *= 2;
return (am + 2);
}
A. 82 90 80
B. 82 180 160
C. 162 90 80
D. 162 180 80
E. 162 180 160
15.) Which of the following is a valid array declaration?
I. int arr[5];
II. int arr[] = {1, 2, 3, 4, 5};
III. int arr[5] = {0};
A.
B.
C.
D.
E.
I only
I and II
I and III
II and III
I, II, and III
7
Siamese
Cat
16.) Which of the following are possible reasons to pass a variable by reference?
A. To change the value of the variable.
B. To save memory.
C. To shorten run time.
D. A and C.
E. A, B, and C.
17.) What statement about tweedledee and tweedledum is true?
double tweedledum(double x) {
double crow = x * x;
return crow;
}
void tweedledee(double x) {
double crow = x * x;
cout << crow << endl;
}
Tweedledee and
Tweedledum
A. If someone wants to write a program that prints the square of a number, they can
use tweedledee but not tweedledum.
B. If someone wants to write a program that prints the square of an integer, they
cannot use tweedledee nor tweedledum.
C. If someone wants to write a program that computes the square of a number, and
then uses the result in later computations, they can use either tweedledum or
tweedledee.
D. If someone wants to write a program that computes the square of a number and
then uses the result in later computations, they can use tweedledum but not
tweedledee.
E. More than one of the above is true.
18.) What values of the variables will make happilyEverAfter evaluate to false?
bool happilyEverAfter = ((trueLove && firstKiss) || glassSlipperFits);
1tabtrueLove1tabfirstKiss1tabglassSlipperFits
A. true1tab1tabfalse1tab1tabtrue
B. true1tab1tabtrue1tab1tab false
C. false1111111true1tab1tab false
D. false1111111true1tab1tab true
E. true1tab1tabtrue1tab1tab true
8
Use the following function for the next two questions. It is intended to return the square
of an integer input.
// Requires: nothing.
// Modifies: nothing.
// Effects: Computes and returns the square of num.
int square(int num) {
int result = 0;
for (int i = 0; i < num; i++) {
result = result + num;
}
return result;
}
19.) Which of the following tests could reveal an error in the function?
A. cout << square(-4);
B. cout << square(7);
C. cout << square(0);
D. A and B.
E. A and C.
20.) Which of the following tests could result in unexpected behavior?
A. cout << square(7);
B. cout << square(123456789);
C. cout << square(3000000000);
D. B and C.
E. A, B, and C.
9