LearnFrenzy
Sign in Take a free test

Functions and Default Arguments · Exercise 2 of 4

Functions and Default Arguments Predict the Output

  • 20 questions
  • Page 1 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Read each C++ program and choose what it prints. Every program was compiled and run, so the answers are the real output.

Question 1 Easy

What is the output of the following C++ program?

main.cpp
#include <iostream>

void swap_copy(int a, int b) { int t = a; a = b; b = t; }
void swap_ref(int& a, int& b) { int t = a; a = b; b = t; }

int main() {
    int x = 1, y = 2;
    swap_copy(x, y);
    std::cout << x << y << ' ';
    swap_ref(x, y);
    std::cout << x << y << '\n';
    return 0;
}
A 12 21
B 21 21
C 21 12
D 12 12
Question page & discussion
Question 2 Easy

What is the output of the following C++ program?

main.cpp
#include <iostream>

int volume(int l, int w = 2, int h = 3) { return l * w * h; }

int main() {
    std::cout << volume(5) << ' ' << volume(5, 4) << ' ' << volume(5, 4, 1) << '\n';
    return 0;
}
A 30 60 20
B 5 20 20
C 30 40 20
D 60 60 20
Question page & discussion
Question 3 Medium

What is the output of the following C++ program?

main.cpp
#include <iostream>

void f(int)    { std::cout << 'I'; }
void f(double) { std::cout << 'D'; }

int main() {
    f(5);
    f(5.0);
    f(5.0f);
    f('5');
    f(true);
    f(5L * 1.0);
    std::cout << '\n';
    return 0;
}
A IDDIID
B IDIIID
C IDDDID
D IDDIDD
Question page & discussion
Question 4 Easy

What is the output of the following C++ program?

main.cpp
#include <iostream>

int count_calls() {
    static int n = 0;
    int m = 0;
    ++m;
    return ++n * 10 + m;
}

int main() {
    std::cout << count_calls() << ' ' << count_calls() << ' ' << count_calls() << '\n';
    return 0;
}
A 11 21 31
B 11 11 11
C 10 20 30
D 11 22 33
Question page & discussion
Question 5 Medium

What is the output of the following C++ program?

main.cpp
#include <iostream>

void show(int n) {
    if (n == 0) return;
    std::cout << n;
    show(n - 1);
    std::cout << n;
}

int main() {
    show(3);
    std::cout << '\n';
    return 0;
}
A 321123
B 123321
C 321321
D 333111
Question page & discussion
Question 6 Easy

What is the output of the following C++ program?

main.cpp
#include <iostream>

int digit_sum(int n) { return n < 10 ? n : n % 10 + digit_sum(n / 10); }
long long power(int b, int e) { return e == 0 ? 1 : b * power(b, e - 1); }

int main() {
    std::cout << digit_sum(98765) << ' ' << power(3, 5) << '\n';
    return 0;
}
A 35 243
B 35 15
C 9 243
D 45 125
Question page & discussion
Question 7 Hard

What is the output of the following C++ program?

main.cpp
#include <iostream>

int calls = 0;

int fib(int n) {
    ++calls;
    return n < 2 ? n : fib(n - 1) + fib(n - 2);
}

int main() {
    std::cout << fib(10) << ' ' << calls << '\n';
    return 0;
}
A 55 177
B 55 10
C 89 177
D 55 109
Question page & discussion
Question 8 Medium

What is the output of the following C++ program?

main.cpp
#include <iostream>
#include <vector>

int& largest(std::vector<int>& v) {
    int i_max = 0;
    for (int i = 1; i < static_cast<int>(v.size()); ++i)
        if (v[i] > v[i_max]) i_max = i;
    return v[i_max];
}

int main() {
    std::vector<int> v{4, 9, 2, 7};
    largest(v) = 0;
    largest(v) += 100;
    for (int x : v) std::cout << x << ' ';
    std::cout << '\n';
    return 0;
}
A 4 0 2 107
B 4 109 2 7
C 4 100 2 7
D 104 0 2 7
Question page & discussion
Question 9 Medium

What is the output of the following C++ program?

main.cpp
#include <iostream>
#include <string>
#include <tuple>

std::tuple<std::string, int, double> student() {
    return {"Asha", 21, 8.75};
}

int main() {
    auto [name, age, cgpa] = student();
    auto t = student();
    std::cout << name << ' ' << age + 1 << ' ' << cgpa << ' ' << std::get<0>(t).size() << '\n';
    return 0;
}
A Asha 22 8.75 4
B Asha 21 8.75 4
C Asha 22 8.8 4
D Asha 22 8.75 5
Question page & discussion
Question 10 Medium

What is the output of the following C++ program?

main.cpp
#include <iostream>

int twice(int x) { return 2 * x; }
int square(int x) { return x * x; }

int apply_n(int (*f)(int), int x, int n) {
    while (n-- > 0) x = f(x);
    return x;
}

int main() {
    std::cout << apply_n(twice, 3, 3) << ' ' << apply_n(square, 3, 2) << '\n';
    return 0;
}
A 24 81
B 18 81
C 24 9
D 9 81
Question page & discussion

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links