Single answer

Two functions try to add bonus points to a score. What does this program print when compiled with g++ -std=c++17 and run?

#include <iostream>

void add_bonus(int points) {
    points += 10;
}

void add_bonus_ref(int& points) {
    points += 10;
}

int main() {
    int score = 50;
    add_bonus(score);
    std::cout << score << ' ';
    add_bonus_ref(score);
    std::cout << score << '\n';
}
Single answer

What does this program print?

#include <iostream>

int main() {
    int stock = 5;
    int& alias = stock;
    int copy = stock;
    alias = 8;
    copy = 1;
    std::cout << stock << ' ' << alias << ' ' << copy << '\n';
}
Single answer

What happens when you compile this program?

#include <iostream>
#include <string>

void shout(const std::string& name) {
    name += "!";
    std::cout << name << '\n';
}

int main() {
    std::string guest = "Ana";
    shout(guest);
}
Single answer

True or false: like a pointer, a reference can be declared first, as in int& r;, and bound to a variable on a later line.

Single answer

What does this program print?

#include <iostream>

int main() {
    int a = 1;
    int b = 2;
    int& r = a;
    r = b;
    b = 9;
    std::cout << a << ' ' << b << ' ' << r << '\n';
}
Single answer

What does this program print?

#include <iostream>
#include <string>

int main() {
    std::string city = "Iasi";
    city += " Nord";
    std::cout << city.size() << ' ' << city[0] << '\n';
}
Single answer

What does this program print?

#include <iostream>
#include <string>

int main() {
    std::string a = "OK";
    std::string b = "O";
    b += "K";
    std::cout << (a == b) << '\n';
}
Single answer

A price list is doubled in one loop and increased by 1 in another. What does this program print?

#include <iostream>
#include <vector>

int main() {
    std::vector<int> prices = {10, 20, 30};
    for (int p : prices) {
        p *= 2;
    }
    for (int& p : prices) {
        p += 1;
    }
    for (int p : prices) {
        std::cout << p << ' ';
    }
    std::cout << '\n';
}
Multiple answers

The function greet below takes a non-const reference. Which of these lines compile when placed in main in place of the comment? Select all that apply.

#include <iostream>
#include <string>
#include <vector>

void greet(std::string& name) {
    std::cout << "Hello, " << name << '\n';
}

int main() {
    std::string guest = "Dan";
    std::vector<std::string> names = {"Eva", "Ion"};
    // call goes here
}
Single answer

A program splits an e-mail address with find and substr. What does it print?

#include <iostream>
#include <string>

int main() {
    std::string email = "ana@bytestark.com";
    auto at = email.find('@');
    std::cout << email.substr(0, at) << ' ' << email.substr(at + 1, 3) << '\n';
}