Skip to content

Instantly share code, notes, and snippets.

@itsjohncs
Created March 13, 2014 19:05
Show Gist options
  • Save itsjohncs/9534738 to your computer and use it in GitHub Desktop.
Save itsjohncs/9534738 to your computer and use it in GitHub Desktop.

CS 10 Mock Final

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!


Question A

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;
    }
}
  1. An unsigned integer local variable
  2. An argument of print_square
  3. A parameter of print_square
  4. An iterator variable
  5. An extern variable

Question B

Which of the following are valid C++ literals. You may select multiple answers (no partial credit).

  1. True
  2. 19.3
  3. January
  4. '\n'
  5. return 0
  6. 4.523e-7

Question C

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;
    }
}
  1. FizzBuzz1FizzBuzzFizz5FizzBuzz
  2. Fizz1FizzBuzzFizz5Fizz
  3. FizzFizzBuzzFizzFizz
  4. FizzBuzzFizzBuzzFizzFizzBuzz
  5. FizzBuzzFizzBuzzzzImABeeFizz
  6. a runtime error occurs

Question D

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;

Question E

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;
}
  1. 10
  2. 0
  3. 3
  4. 6
  5. 4

Question F

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;
    }
}
  1. "ABCD"
  2. "1122"
  3. "^^^^^^"
  4. "10290"
  5. 19.2
  6. "11110"
  7. "0000"

Question F - Part 2

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;
    }
}
  1. (s.at(i) != s.at(i + 1)) && (i + 1 < s.size())
  2. (i + 1 < s.size()) && (s.at(i) != s.at(i + 1))
  3. ((i + 1 < s.size()) && (s.at(i) != s.at(i + 1))) || ((i - 1 >= 0) && (s.at(i) != s.at(i - 1)))
  4. ((s.at(i) != s.at(i + 1)) && (i + 1 < s.size())) || ((s.at(i) != s.at(i - 1)) && (i - 1 >= 0))

Question G

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;
}
  1. Foo
  2. BazFoo
  3. BazBazFoo
  4. BazBazBazFoo
  5. BazBazBazBazFoo
  6. BazBazBazBuzzBazFoo

Question H

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";
}

Question I

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";
}
  1. no output
  2. foo
  3. bar
  4. baz
  5. foobar
  6. barbaz
  7. foobaz
  8. foobarbaz

Question J

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.

  1. (r ^ 2) * PI
  2. static_cast<double>((r ^ 2) * PI)
  3. (static_cast<double>(r) * r) * PI
  4. static_cast<int>(r * r) * PI
  5. static_cast<double>(static_cast<int>(PI) * r * r)
  6. (static_cast<double>(r) ^ 2) * PI

Question K

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.

Chekerboard Picture

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';
    }
}

Question L

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;
}
  1. 8582
  2. 8591
  3. 8618
  4. 8636
  5. 8645

Question M

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;
}

Question N

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?

  1. 'a' + ((c - 'a') + 8) % ('z' - 'a')
  2. ((c - 'a') + 8) % ('z' - 'a' + 1)
  3. 'a' + ((c - 'a') + 8) % ('z' - 'a' + 1)
  4. (c + 8) % 26
  5. 'a' + (c + 8) % 26

Question O

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;
}
  1. 6 0 1 5 13
  2. 6 0 2 6 13
  3. 6 0 2 6 14
  4. 7 0 2 6 13
  5. 7 0 1 5 13

Question P

Given a string variable s containing the value "Foo Bar Baz", what would the following expression evaluate to?

s.substr(2, 4)
  1. "o B"
  2. "oo "
  3. "o Ba"
  4. "oo B"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment