LearnFrenzy
Sign in Take a free test

Arrays and Vectors · Exercise 2 of 4

Arrays and Vectors Predict the Output

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

Read each C++ program and choose what it prints. Every program was compiled and run on a 64-bit system, so the answers are the real output.

Question 11 Medium

What is the output of the following C++ program on a 64-bit system?

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

int main() {
    std::vector<int> v{5, 10, 15};
    int sum = 0;
    for (int i = 0; i <= 3; ++i) {
        try {
            sum += v.at(i);
        } catch (const std::out_of_range&) {
            std::cout << "bad index " << i << ", ";
        }
    }
    std::cout << "sum " << sum << '\n';
    return 0;
}
A bad index 3, sum 30
B sum 30
C bad index 3, sum 0
D bad index 4, sum 30
Question page & discussion
Question 12 Medium

What is the output of the following C++ program on a 64-bit system?

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

int main() {
    std::array<int, 3> a{1, 2, 3}, b{1, 3, 0}, c = a;
    c[2] = 4;
    std::cout << (a == b) << (a < b) << (c > a) << (a != c) << '\n';
    return 0;
}
A 0111
B 0011
C 1011
D 0110
Question page & discussion
Question 13 Hard

What is the output of the following C++ program on a 64-bit system?

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

int main() {
    std::vector<int> a{1, 2}, b{1, 2, 0}, c{1, 3};
    std::cout << (a < b) << (b < c) << (a == std::vector<int>{1, 2}) << (c < a) << '\n';
    return 0;
}
A 1110
B 0110
C 1100
D 1111
Question page & discussion
Question 14 Easy

What is the output of the following C++ program on a 64-bit system?

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

int main() {
    std::vector<char> stack{'a', 'b', 'c', 'd'};
    while (!stack.empty()) {
        std::cout << stack.back();
        stack.pop_back();
    }
    std::cout << ' ' << stack.size() << '\n';
    return 0;
}
A dcba 0
B abcd 0
C dcba 4
D d 3
Question page & discussion
Question 15 Medium

What is the output of the following C++ program on a 64-bit system?

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

int main() {
    std::vector<int> v{1, 2, 3, 4, 5};
    for (auto it = v.rbegin(); it != v.rend(); ++it)
        if ((it - v.rbegin()) % 2 == 0) std::cout << *it << ' ';
    std::cout << '|' << *(v.end() - 2) << '\n';
    return 0;
}
A 5 3 1 |4
B 1 3 5 |4
C 5 4 3 2 1 |4
D 5 3 1 |5
Question page & discussion
Question 16 Easy

What is the output of the following C++ program on a 64-bit system?

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

int main() {
    std::vector<int> v{40, 10, 30, 20, 50};
    std::sort(v.begin(), v.end(), std::greater<>());
    std::sort(v.begin() + 1, v.end() - 1);
    for (int x : v) std::cout << x << ' ';
    std::cout << '\n';
    return 0;
}
A 50 20 30 40 10
B 50 40 30 20 10
C 10 20 30 40 50
D 50 10 20 30 40
Question page & discussion
Question 17 Medium

What is the output of the following C++ program on a 64-bit system?

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

int main() {
    std::vector<int> v{3, 9, 4, 9, 1};
    auto it = std::max_element(v.begin(), v.end());
    double avg = std::accumulate(v.begin(), v.end(), 0.0) / v.size();
    std::cout << *it << ' ' << (it - v.begin()) << ' ' << avg << ' '
              << std::count(v.begin(), v.end(), 9) << '\n';
    return 0;
}
A 9 1 5.2 2
B 9 3 5.2 2
C 9 1 5 2
D 9 1 5.2 1
Question page & discussion
Question 18 Hard

What is the output of the following C++ program on a 64-bit system?

main.cpp
#include <iostream>

int main() {
    int m[2][3] = {{1, 2, 3}, {4, 5, 6}};
    int* p = &m[0][0];
    std::cout << p[4] << ' ' << *(m[1] + 2) << ' ' << sizeof(m) << ' ' << sizeof(m[0]) << '\n';
    return 0;
}
A 5 6 24 12
B 5 6 24 8
C 4 6 24 12
D 5 3 24 12
Question page & discussion
Question 19 Easy

What is the output of the following C++ program on a 64-bit system?

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

int main() {
    std::vector<int> a{1, 2, 3};
    std::vector<int> b = a;
    std::vector<int>& c = a;
    b[0] = 100;
    c[1] = 200;
    std::cout << a[0] << ' ' << a[1] << ' ' << b[1] << ' ' << c.size() << '\n';
    return 0;
}
A 1 200 2 3
B 100 200 2 3
C 1 2 2 3
D 1 200 200 3
Question page & discussion
Question 20 Medium

What is the output of the following C++ program on a 64-bit system?

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

int sum(std::span<const int> s) {
    int t = 0;
    for (int x : s) t += x;
    return t;
}

int main() {
    std::vector<int> v{1, 2, 3, 4, 5, 6};
    std::span<int> all(v);
    all[0] = 10;
    std::cout << sum(all) << ' ' << sum(all.first(2)) << ' ' << sum(all.subspan(2, 3)) << ' ' << v[0] << '\n';
    return 0;
}
A 30 12 12 10
B 21 3 12 1
C 30 12 9 10
D 30 12 12 1
Question page & discussion
Up next · Exercise 3Find the Error10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links