Review Material (CPSC 120)
Pass by value/reference
Classes
Objects and references
Templates and Maps
100

Define and initialize a variable to store the value 4.2

float a{4.2};

double a{4.2};

100

The symbol appended to a data type in a function parameter list to declare a parameter as pass-by-reference.

What is & (ampersand)

100

Members marked with this access specifier can be directly accessed from anywhere in the program, including outside the class implementation (such as inside main()).

What is public?

100

An instance of a class created in memory that holds both state (member variables) and behavior (member functions).

What is an object?

100

This C++ keyword is placed before a function or class declaration to indicate that it operates with generic types.

What is template?

200

How do you negate a boolean value?

Using the not ('!') operator

200

What is printed from the following code:
void Triple(int& num) { num *= 3; } 

void Run() {  

    int val = 10;  

    Triple(val);  

    std::cout << val; 

}

30

200

Public member functions designed specifically to safely inspect or update the values of private member variables are commonly referred to by these two terms.

What are getters and setters (or accessors and mutators)?

200

When a stack-allocated object goes out of scope, this special member function is automatically executed to clean up its resources.

What is a destructor?

200

This standard library container stores elements as unique key-value pairs sorted automatically in ascending order by key.

What is std::map?

300

Add 42 to the vector my_ints

my_ints.push_back(42);

300

Passing large standard library objects (such as std::vector or std::string) by this parameter type avoids expensive dynamic memory allocations and copies while keeping the original object read-only.

What is const reference (or const std::vector<T>&)?

300

This constructor is provided for you even if you don't explicitly define one.

What is the default constructor?

300

Adding this keyword to a function definition prevents the modification of data passed by reference.

What is 'const'?

300

When iterating over a std::map using a range-based for loop, this member name of the element pair accesses the key.

What is first (e.g., pair.first)?

400

Fix the following for loop so that it iterates 10 times total:

for(int i = 0; i <= 10; i++){

    sum += 1;

}

'<=' becomes '<'

i = 1

400

Unlike pointers, a reference in C++ cannot be set to nullptr and cannot undergo this operation after it is initialized.

What is re-binding (or re-assigning to reference a different variable)?

400

According to the Google C++ Style Guide, all private data members of a class should end with this specific character to distinguish them from local variables and parameters.

What is a trailing underscore (_)?

400

If a class contains a reference variable as a member (T& member_), it must be initialized in this constructor section because references cannot be default-declared.

What is the member initializer list

400

What is printed by the following code:
std::map<std::string, int> scores = {{"Alice", 90}, {"Bob", 85}};

scores["Alice"] = 95; 

scores["Charlie"] = 100;

std::cout << scores.size();

3

500

Write a function that takes two integers and returns their average.

double avg_calc(int first, int second){

    return (first + second) / 2;

}

500

Returning a non-const reference to a variable declared inside that same function creates this dangerous runtime bug.

What is a dangling reference (or undefined behavior)?

500

In standard C++ project architecture, class declarations and public interfaces are placed in header files (.h), while member function implementations are written in files with this extension.

What is .cpp?

500

What problem would occur if an object with a reference member variable was copied?

There would be more than one object that has a member variable pointing to the same data.

500

Calling Max(5, 3.14) on template <typename T> T Max(T a, T b) causes a compilation error for this reason.

Template expects the same type; to pass different types, only one would be templatized