Strings
Functions
vectors
100

if string s = "I like apples"; what does s.length(); return?

13
100
What should be the type of a function that returns nothing?

void

100

Declare a vector of integers V of size n (n is an integer variable previously declared and initialized)

vector <int> V(n);

200

if string s = "We are in the endgame now D:". How do I use substr to get "engame"?

s.substr(14, 7);

200

Given a function sum of the form int sum(int, int). Give an example of an incorrect way to call sum.

One possible answer: sum(1,"2");

200

How you access the last element of a vector V of size n where n is an integer variable previously declared and initialized

v.at(n - 1);

300

if string s = "I don't wanna go". What does s.find('i') return?

string::npos
300

Given a function of the form int f(int x, int &y). Give an example of an invalid way to call f.

f(3, 4); // Note: the second parameter is expecting an actual integer variable, not an integer literal


300

vector <string>V(3);

v.at(1) = "hi";

What are the contents of V after those lines of code execute?

["", "hi", ""]

400

what are the parameters to the replace function from the string library?

Starting index

Number of characters to replace

String to replace with

400

Explain the main benefit of using pass by reference parameters.

Using pass by reference parameters, we avoid copying large amounts of data.

400
V is size 15. How can you assign the last element the value of the first element tripled?

v.at(14) = v.at(0) * 3

500

string s = "1.2.3.4.5";

s.replace(s.find(".2") + 1, 2, ".3");

What is stored in s?

1..33.4.5

500

Explain why sometimes pass by reference parameters are also constants.

Because we want to avoid mistakenly updating pass by reference parameters during the execution of a function
500

vector <bool> v(2);

v.push_back(true);

v.at(0) = !v.at(0);

What are the contents of the vector?

[true, false, true]

or

[1, 0, 1]

M
e
n
u