It is unlikely you will finish this test in the allotted time. Try to do as many problems as you can.
Things to keep in mind:
- If the problem states you can select multiple answers, you don't necessarily have to select more than one (or even any at all).
- If you see a mistake please tell me.
- This may or may not be an accurrate represenation of what your final will look like. I do not know what your final looks like and am not responsible for creating it. This is a tool to help you study created by the SI leader assigned to this class (who is an undergraduate student with the same status as you).
- Each question is worth 1 point. No partial credit.
- The questions are not ordered in a reasonable way, be wary.
- The questions are lettered and the answers are numbered, don't get confused.
Because it hasn't been empathasized a lot I will remind you about short circuit evaluation. Don't forget that when the computer evaluates the expression a && b
(at runtime), if a is false, b will not even be looked at. A similar thing happens with the ||
operator.
Good luck!
What is the most approriate desciption of a as shown in the code below?
int print_square(int a) {
string stars('*', a);
for (int i = 0; i < a; ++i) {
cout << starts;
}
}
- An unsigned integer local variable
- An argument of
print_square
- A parameter of
print_square
- An iterator variable
- An extern variable
Which of the following are valid C++ literals. You may select multiple answers (no partial credit).
True
19.3
January
'\n'
return 0
4.523e-7
What is the output of the following code.
for (int i = 0; i <= 6; ++i) {
if (i % 2 == 0) {
cout << "Fizz";
} else if (i % 3 == 0) {
cout << "Buzz";
} else if (i % 2 == 0 && i % 3 == 0) {
cout << "FizzBuzz";
} else {
cout << i;
}
}
- FizzBuzz1FizzBuzzFizz5FizzBuzz
- Fizz1FizzBuzzFizz5Fizz
- FizzFizzBuzzFizzFizz
- FizzBuzzFizzBuzzFizzFizzBuzz
- FizzBuzzFizzBuzzzzImABeeFizz
- a runtime error occurs
A friend of yours made a new currency and wants you to help them write a program that determines the most efficient way to represent a given sum of money.
- A GoldBuzz coin is worth 32 SilverBuzz coins.
- A SilverBuzz coin is worth 16 CopperBuzz coins
- A CopperBuzz coin is worth 8 BitBuzz coins
int bitbuzz;
cout << "How many BitBuzz coins do you have?: ";
cin >> bitbuzz;
// YOUR CODE GOES HERE
cout << goldbuzz << " GoldBuzz coins, " << silverbuzz << " SilverBuzz coins, and "
<< bitbuzz << " BitBuzz coins" << endl;
Which of the following code snippets would correctly print out the most efficient way to represent the number of BitBuzz coins the user enters.
For example, if the user entered 100, the program should print out 3 GoldBuzz coins, 0 SilverBuzz coins and 4 BitBuzz coins.
Snippet 1
int silverbuzz = bitbuzz / 16;
bitbuzz = bitbuzz - 16 * silverbuzz;
int goldbuzz = bitbuzz / 32;
bitbuzz = bitbuzz - 32 * goldbuzz;
Snippet 2
double goldbuzz = bitbuzz / 32.0;
bitbuzz = bitbuzz % 32;
double silverbuzz = bitbuzz / 16.0;
bitbuzz = bitbuzz % 16;
Snippet 3
int goldbuzz = bitbuzz / 32;
bitbuzz = bitbuzz - 32 * 2;
int silverbuzz = bitbuzz / 16;
bitbuzz = bitbuzz - 16;
Snippet 4
int goldbuzz = bitbuzz / 32;
bitbuzz = bitbuzz % 32;
int silverbuzz = bitbuzz / 16;
bitbuzz = bitbuzz % 16;
What is the value of the variable a after the following code executes?
int a = 3, b = 4;
if (a >= 0) {
a = a * 2;
}
if (a >= 6) {
a = a + b;
}
- 10
- 0
- 3
- 6
- 4
The following code is supposed to print out Not all the same! if every character in s is not the same. It has a problem though.
Which values, when assigned to s, will cause the following program to crash with a runtime error? You may select multiple answers (no partial credit).
string s = /* SOME VALUE */;
for (int i = 0; i < s.size(); i += 2) {
if (s.at(i) != s.at(i + 1)) {
cout << "Not all the same!" << endl;
break;
}
}
"ABCD"
"1122"
"^^^^^^"
"10290"
19.2
"11110"
"0000"
You've decided to fix the code and you've determined the best way to do that is by replacing the boolean expression within the if statement. Which boolean expression would best prevent any runtime errors and produce the desired behavior described above?
string s = /* some value (not important right now though) */;
for (int i = 0; i < s.size(); i += 2) {
if (/* YOUR BOOLEAN EXPRESSION HERE */) {
cout << "Not all the same!" << endl;
break;
}
}
(s.at(i) != s.at(i + 1)) && (i + 1 < s.size())
(i + 1 < s.size()) && (s.at(i) != s.at(i + 1))
((i + 1 < s.size()) && (s.at(i) != s.at(i + 1))) || ((i - 1 >= 0) && (s.at(i) != s.at(i - 1)))
((s.at(i) != s.at(i + 1)) && (i + 1 < s.size())) || ((s.at(i) != s.at(i - 1)) && (i - 1 >= 0))
What is the output of the following program?
#include <iostream>
using namespace std;
bool is_even(int x) {
cout << "Baz";
return ((x % 2) == 0);
}
int main() {
if (is_even(8) || (is_even(2) && is_even(3) && is_even(4))) {
cout << "Foo";
}
return 0;
}
- Foo
- BazFoo
- BazBazFoo
- BazBazBazFoo
- BazBazBazBazFoo
- BazBazBazBuzzBazFoo
Which of the listed function implementations will cause the below fragment to output FoobarBaz?
string s;
cout << qux(s);
cout << s;
cout << "Baz";
Function 1
string qux(string & s) {
s = "bar";
cout << "Foo";
}
Function 2
void qux(string s) {
s = "bar";
cout << "Foo";
}
Function 3
string qux(const string & s) {
s = "bar";
return "Foo"
}
Function 4
string qux(string & s) {
s = "bar";
return "Foo";
}
What output by the following code fragment?
int a = 100, b = 200;
bool c = a < b;
const bool d = a > b;
a = a * 5;
if (c) {
cout << "foo";
}
if (d) {
cout << "bar";
}
a = a / 5;
if (d) {
cout << "baz";
}
- no output
- foo
- bar
- baz
- foobar
- barbaz
- foobaz
- foobarbaz
Given an integer variable r and a constant double variable PI containing the value of π, which expressions correctly calculate the floating point value of the area of the circle with radius r. You may select multiple answers (no partial credit).
For reference, the area of a circle is defined as πr2.
(r ^ 2) * PI
static_cast<double>((r ^ 2) * PI)
(static_cast<double>(r) * r) * PI
static_cast<int>(r * r) * PI
static_cast<double>(static_cast<int>(PI) * r * r)
(static_cast<double>(r) ^ 2) * PI
This question is very difficult/lengthy, approach with caution.
You are trying to initialize a 2D vector representing a checker board. Each tile is stored as a character where a period represents that the tile is empty, a capital R represents that a red piece is placed on it, and a capital B represents that a black tile is placed on it. The colors of the tiles themselves (ie: the classic checkerboard pattern) are not represented.
// This will initialize the vector v to be an 8x8 2D vector filled entirely
// with periods.
vector<char> inner(8, '.');
vector<vector<char> > v(8, inner);
// YOUR CODE HERE
for (int i = 0; i < v.size(); ++i) {
for (int j = 0; j < v.size(); ++j) {
cout << v.at(i).at(j) << " ";
}
cout << endl;
}
You want the code fragment above to print out:
R . R . R . R .
. R . R . R . R
R . R . R . R .
. . . . . . . .
. . . . . . . .
. B . B . B . B
B . B . B . B .
. B . B . B . B
Here is a checkerboard for reference.
Which of the following code snippets correctly initializes the 2D vector v when placed in the fragment above?
Snippet 1
for (int i = 0; i < 3; ++i) {
for (int j = i % 2; j < v.size(); j += 2) {
v.at(i).at(j) = 'R';
}
}
for (int i = 5; i < v.size(); ++i) {
for (int j = i % 2; j < v.size(); j += 2) {
v.at(i).at(j) = 'B';
}
}
Snippet 2
for (int i = 0; i < 3; ++i) {
for (int j = i % 2; j < v.size(); j += 2) {
v.at(i).at(j) = 'R';
}
}
for (int i = 5; i < v.size(); ++i) {
for (int j = (i + 1) % 2; j < v.size(); j += 2) {
v.at(i).at(j) = 'B';
}
}
Snippet 3
for (int i = 0; i < 3; ++i) {
for (int j = i % 2; j < v.size(); j += 2) {
v.at(j).at(i) = 'R';
}
}
for (int i = 5; i < v.size(); ++i) {
for (int j = i % 2; j < v.size(); j += 2) {
v.at(j).at(i) = 'B';
}
}
Snippet 4
for (int i = 0; i < 3; ++i) {
for (int j = i % 2; j < v.size(); j += 2) {
v.at(j).at(i) = 'R';
}
}
for (int i = 5; i < v.size(); ++i) {
for (int j = (i + 1) % 2; j < v.size(); j += 2) {
v.at(j).at(i) = 'B';
}
}
What is the value of n after the following code finishes executing? Note: there is no infinite loop or runtime error in the below code.
int n = 8582, m = 8645, p = 1;
while (n != m) {
n = n + (p / (m / 10));
p = n;
}
- 8582
- 8591
- 8618
- 8636
- 8645
Which of the following code fragments correctly takes in integers from the user and places them into a vector v until the user enters a negative number (which should not be added into v)? You may select multiple answers (no partial credit).
Fragment 1
int x;
while (x >= 0) {
cin >> x;
v.push_back(x);
}
Fragment 2
int x;
while (x >= 0) {
cin >> x;
if (x >= 0) {
v.push_back(x);
}
}
Fragment 3
int x;
do {
cin >> x;
} while (x >= 0);
v.push_back(x);
Fragment 4
int x;
cin >> x;
while (x >= 0) {
v.push_back(x);
cin >> x;
}
A decoder ring works by assigning each letter of the alphabet another letter, a fixed distance away on the ring. For example, a decoder ring with a distance of 5 would encode the string abc as fgh, and the string xyz as cde.
Given a lowercase letter stored in a char variable c, which expession evaluates to the corresponding character on a decoder ring with distance 8?
'a' + ((c - 'a') + 8) % ('z' - 'a')
((c - 'a') + 8) % ('z' - 'a' + 1)
'a' + ((c - 'a') + 8) % ('z' - 'a' + 1)
(c + 8) % 26
'a' + (c + 8) % 26
What is the output of the following program?
#include <iostream>
#include <vector>
using namespace std;
int foo(int & a, int b) {
a = b / 2;
b = b + 1;
return b % 4;
}
int bar(vector<int> v) {
int sum = 0;
for (int i = 0; i < v.size() - 1; ++i) {
sum += foo(v.at(i), v.at(i + 1));
}
return sum;
}
int main() {
vector<int> v;
v.push_back(0);
v.push_back(1);
v.push_back(5);
v.push_back(13);
cout << bar(v) << " ";
for (int i = 0; i < v.size(); ++i) {
cout << v.at(i) << " ";
}
cout << endl;
return 0;
}
6 0 1 5 13
6 0 2 6 13
6 0 2 6 14
7 0 2 6 13
7 0 1 5 13
Given a string variable s containing the value "Foo Bar Baz"
, what would the following expression evaluate to?
s.substr(2, 4)
"o B"
"oo "
"o Ba"
"oo B"