AlgoMaster Logo

Functions - Quiz

Last Updated: January 3, 2026

Functions Quiz

30 quizzes

1
Multiple Choice

Which part of a function specifies the type of value it produces?

2
Multiple Choice

Where is it most appropriate to place function declarations for reuse across multiple .cpp files?

3
Multiple Choice

Which parameter type is best to pass a large std::string that should NOT be modified?

4
Multiple Choice

What is a key characteristic of pass-by-value parameters?

5
Multiple Choice

Which of these is a valid reason for using function overloading?

6
Multiple Choice

Where should you typically put default argument values for a function used across translation units?

7
Multiple Choice

Which statement about inline functions in modern C++ is most accurate?

8
Multiple Choice

In a well-formed recursive function, what must always be present?

9
Multiple Choice

Which lambda correctly captures all automatic variables by reference?

10
Multiple Choice

Which is a practical reason to pass a std::vector<T> by non-const reference to a function?

11
Code Completion

Declare a function that takes a const reference to std::string and returns its length

cpp
1
std::size_t lengthOf(const std::string& );

Click an option to fill the blank:

12
Code Completion

Overload a function named logValue taking a double parameter

cpp
1
void logValue( value);

Click an option to fill the blank:

13
Code Completion

Declare a lambda that captures all variables by value and takes an int x

cpp
1
auto func = [=]( x) { return x * 2; };

Click an option to fill the blank:

14
Output Prediction

What is the output of this code using pass-by-value vs pass-by-reference?

1#include <iostream>
2void incByValue(int x) { x++; }
3void incByRef(int& x) { x++; }
4int main() {
5    int n = 5;
6    incByValue(n);
7    incByRef(n);
8    std::cout << n << std::endl;
9    return 0;
10}
15
Output Prediction

What does this overloaded function call print?

1#include <iostream>
2void print(int x) { std::cout << "int" << std::endl; }
3void print(double x) { std::cout << "double" << std::endl; }
4int main() {
5    auto x = 3.0f;
6    print(x);
7    return 0;
8}
16
Output Prediction

What is the output of this recursive function?

1#include <iostream>
2int sumDown(int n) {
3    if (n <= 0) return 0;
4    return n + sumDown(n - 2);
5}
6int main() {
7    std::cout << sumDown(5) << std::endl;
8    return 0;
9}
17
Output Prediction

What does this lambda-based code output?

1#include <iostream>
2int main() {
3    int a = 1;
4    int b = 2;
5    auto f = [a, &b]() mutable {
6        a += 10;
7        b += 10;
8        std::cout << a << " " << b << "\n";
9    };
10    f();
11    std::cout << a << " " << b << std::endl;
12    return 0;
13}
18
Output Prediction

What will this inline function print when called?

1#include <iostream>
2inline int square(int x) { return x * x; }
3int main() {
4    int n = 3;
5    std::cout << square(++n) << " " << n << std::endl;
6    return 0;
7}
19
Bug Spotting

Find the bug in this recursive factorial implementation

Click on the line(s) that contain the bug.

cpp
1
#include <iostream>
2
int factorial(int n) {
3
    if (n == 0) {
4
        return 0;
5
    }
6
    return n * factorial(n - 1);
7
}
8
int main() {
9
    std::cout << factorial(3) << std::endl;
10
    return 0;
11
}
20
Bug Spotting

Find the bug related to default arguments and overloading

Click on the line(s) that contain the bug.

cpp
1
#include <iostream>
2
void log(const std::string& msg, int level = 1) {
3
    std::cout << level << ": " << msg << std::endl;
4
}
5
void log(const std::string& msg, int level = 2); // declaration
6
int main() {
7
    log("Hello");
8
    return 0;
9
}
21
Hotspot Selection

Click the line that causes unintended modification due to pass-by-reference

Click on the line to select.

cpp
1
#include <vector>
2
void appendZero(std::vector<int>& v) {
3
    v.push_back(0);
4
}
5
int main() {
6
    std::vector<int> data{1,2,3};
7
    appendZero(data);
8
    return 0;
9
}
22
Hotspot Selection

Click the line where the lambda captures by value, preventing external modification

Click on the line to select.

cpp
1
#include <iostream>
2
int main() {
3
    int counter = 0;
4
    auto f = [counter]() mutable {
5
        counter++;
6
        std::cout << counter << std::endl;
7
    };
8
    f();
9
    std::cout << counter << std::endl;
10
    return 0;
11
}
23
Fill in the Blanks

Complete the function that prints elements of a const std::vector<int> without copying it

cpp
1
#include <vector>
2
#include <iostream>
3
void printVec( std::vector<int> v) {
4
for ( x : v) {
5
std::cout << x << " ";
6
}
7
std::cout << std::endl;
8
}
9

Click an option to fill blank 1:

24
Fill in the Blanks

Complete the overloaded add functions with default arguments

cpp
1
int add(int a, int b = ) {
2
return a + b;
3
}
4
double add(double a, double b = ) {
5
return a + b;
6
}
7
int main() {
8
std::cout << add(5) << " " << add(2.5) << std::endl;
9
}
10

Click an option to fill blank 1:

25
Fill in the Blanks

Complete the recursive function to compute power(base, exp) for non-negative exp

cpp
1
int power(int base, int exp) {
2
if (exp == 0) return ;
3
return base * ;
4
}
5

Click an option to fill blank 1:

26
Fill in the Blanks

Complete the lambda to sort a vector of ints in descending order using std::sort

cpp
1
#include <algorithm>
2
#include <vector>
3
#include <iostream>
4
int main() {
5
std::vector<int> v{1,4,2,3};
6
std::sort(v.begin(), v.end(), (int a, int b) { return ; });
7
for (int x : v) std::cout << x << " ";
8
}
9

Click an option to fill blank 1:

27
Matching

Match each parameter passing style with its typical use case

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

28
Matching

Match each lambda capture form with its description

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

29
Sequencing

Order the steps to design and use a new function in an existing C++ project

Drag and drop to reorder, or use the arrows.

30
Sequencing

Order the steps performed during a single recursive call to solve a problem

Drag and drop to reorder, or use the arrows.