Which of the following is not a type in C++?
a) int
b) bool
c) string
d) char
e) tuple
e) tuple
What type of loop should you use for a count controlled event?
for loop
In an if / else-if / else chain, how many branches will execute?
1
List the following elements of this function declaration: double divide(double numerator, double denominator) { return numerator / denominator; }
1) return type
2) function name
3) parameters
4) function body
1) double
2) divide
3) double numerator, double denominator
4) return numerator / denominator
What day and time is exam 1 and where can you find resources to study?
Tuesday, February 17th from 6:00pm to 8:00pm & resources can be found on the EECS 183 site under resources -> exam resources
Which of the following statements are invalid?
a) bool a = 0;
b) string b = "";
c) char c = "hi";
d) double d = 1;
c
What will the output of this loop be?
for (int i = 0; i < 12; i+= 2) {
cout << i << " ";
}
0 2 4 6 8 10
What is the output of the following code segment?
bool sunny = true;
bool warm = false;
if (sunny) {
cout << "Don't forget sunblock!" << endl;
}
if (warm) {
cout << "Go touch grass" << endl;
}
else if (!warm && !sunny) {
cout << "Maybe stay inside today" << endl;
}
else {
cout << "Bundle up today!" << endl;
}
Don't forget sunblock!
Bundle up today!
Which of the following is a correct function declaration, a function call, and a function definition?
a) double divide(double numerator, double denominator);
b) double results = divide(double numerator, double denominator);
c) double divide (double numerator, double denominator) { return numerator / denominator; }
d) double results = divide (5.6, 7.4);
e) double divide (double numerator, double denominator) { cout << numerator / denominator; }
declaration: a
call: d
definition: c
What is the scope of cookie?
bool wednesday = true;
bool hungry = true;
if (wednesday) {
if (hungry) {
bool cookie = true;
}
cout << "yay i have eecs 183 lab today!";
}
}
the if (hungry) {} statement
Given the following code, what type of variables should people be stored in?
cout << "How many people are in your family?";
____ people;
cin >> people
int
Which of the follow are syntactically invalid?
a) int i = 0; for (; i < 12; ++i) {}
b) string a = "hello"; for (i = 0; i < a.length(); ++i) {}
c) for (int i = 0; i < 12; ;) {}
d) for (; i < 12; --i) {}
e) int i = 0; for (; i < 12; ;) {}
d) for (; i < 12; --i) {}
which of the following code segments would not compile?
a) if (true) {}
b) else if (false) {}
c) if (true) {}
else {}
d) if (!true || !false) {}
b) else if (false) {}
Describe the bugs and errors in the following code snippet. Must name at least 3 to receive the points
double divide(double numerator, double denominator);
int main() {
int ans = divide(double 5.4; double 6);
cout << ans;
}
double divide(double numerator, double denominator) {
if (denominator == 0) {
cout << "invalid input" << endl;
return 0;
}
else {
cout << (numerator / denominator);
return (denominator / numerator);
}
}
*These are bugs & errors I intentionally added but there may be more*
1) Checking for the requires clause in function definition
2) returning denominator / numerator instead of numerator / denominator
3) putting double in with the parameters in the function call
4) storing a double in an int
What is the output of the following code segment?
int a = 10;
int b = 3;
double ab = static_cast<double>(a) / b;
double ba = a / static_cast<double>(b);
int abab = static_cast<double>(a / b);
cout << ab << " " << ba << " " << abab;
3.33 3.33 3
Which of the following code fragments will assign the value 2.5 to y?
a) double y = 1 + 3 % 2;
b) double y = 1 + 3 / 2;
c) double y = 1.0 + 3 / 2;
d) double y = 1 + 3 / 2 * 1.0;
e) double y = 1 + 1.0 * 3 / 2;
e) double y = 1 + 1.0 * 3 / 2;
What will the output of the following loop(s) be?
for (int i = 0; i < 6; ++i) {
for (int j = 0; j < 3; ++j) {
if (i % 2 == 0) {
cout << i + j << " ";
}
}
}
0 1 2 2 3 4 4 5 6
How many branches will execute?
int age = 19;
string year = "Sophomore";
bool student = true;
bool graduated = false;
if (!graduated) {
cout << "current student" << endl;
}
else {
cout << "alumni" << endl;
}
if (student) {
cout << "eecs 183 student" << endl;
}
bool underclassman = false;
if (year == "Freshman") {
cout << "first year student";
underclassman = true;
}
else if (year == "Sophomore") {
cout << "second year student";
underclassman = true;
}
if (underclassman) {
cout << "underclassman";
}
else {
cout << "upperclassman";
}
4
Create an RME for the following function
void divide (double numerator, double denominator) {
cout << (numerator / denominator);
}
*answers will vary slightly*
Requires: Denominator != 0
Modifies: cout
Effects: Prints the value of numerator / denominator
int num;
int sum = 0;
cout << "enter numbers"
while (cin >> num) {
sum += num;
}
when the user stops entering numbers
Which of the following is not a primative type in C++?
a) int
b) float
c) string
d) char
c) string
What is the output of the following?
while(true) {
for (int i = 0; i < 12; ++i) {
cout << "i" << endl;
}
}
trick questions :p the while (true) loop has no termination, so this would cause an infinite loop
What will the output of this code segment be?
bool silly = true;
bool minion = true;
bool evil_minion = false;
if (silly) {
if (evil_minion) {
cout << "muah huah huah :p" << endl;
}
cout << "silly goose" << endl;
}
if (minion) {
if (evil_minion) {
cout << "i am purple" << endl;
}
else {
cout << "don't worry i'm a normal minion" << endl;
}
}
else if (silly) {
cout << "tee hee hee";
}
silly goose
don't worry i'm a normal minion
What will be printed to the console?
int swap(int a, int b);
int main() {
int a = 12;
int b = 7;
int c = 14;
a = swap(a, c);
b = swap(c, b);
c = swap(a, b);
cout << "a: " << a << " b: " << b << " c: " << c;
}
int swap(int a, int b) {
int temp = a;
a = b;
b = temp;
return b;
}
a: 12 b: 14 c: 12
Why is the output of this function wrong?
void good_day(bool sunny, bool warm, bool no_school);
int main() {
//expected amazing day, output is good day
good_day(true, true, true);
}
void good_day(bool sunny, bool warm, bool no_school) {
if (sunny) {
if (!warm && !sunny) {
cout << "okay day";
}
else if (warm || no_school) {
cout << "good day";
}
else if (warm && no_school) {
cout << "amazing day";
}
}
else if (warm && sunny) {
cout << "good day";
}
else if (no_school && warm) {
cout << "good day";
}
else {
cout << "bad day";
}
}
The nested else if (warm || no_school) will execute because an || branch will execute if at least one condition is met. This means the else if (warm && no_school) branch will never execute