LearnFrenzy
Sign in Take a free test

Arrays and Vectors · Exercise 3 of 4

Arrays and Vectors Find the Error

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

Each C++ program contains an array or vector mistake that the compiler rejects. Choose the option that names the real problem.

Question 1 Medium

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

int main() {
    int n;
    std::cin >> n;
    int scores[n];
    std::cout << sizeof(scores) << '\n';
    return 0;
}
A n must be unsigned
B Standard C++ requires an array size that is a compile-time constant; variable-length arrays are only a compiler extension – use std::vector<int> scores(n)
C sizeof cannot be used on arrays
D cin cannot read the size
Question page & discussion
Question 2 Easy

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

int main() {
    int a[3] = {1, 2, 3};
    int b[3];
    b = a;
    std::cout << b[0] << '\n';
    return 0;
}
A b must be initialised
B C arrays cannot be assigned as a whole; copy element by element, use std::copy, or use std::array, which supports =
C a must be const
D Arrays of size 3 are not allowed
Question page & discussion
Question 3 Easy

The program below does not compile. Which statement correctly describes the problem?

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

int main() {
    std::array<int, 3> days{31, 28, 31, 30};
    std::cout << days[0] << '\n';
    return 0;
}
A std::array cannot be brace-initialised
B The list has 4 values for an array of 3 elements – too many initializers
C days must be global
D int cannot hold 31
Question page & discussion
Question 4 Medium

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

int[3] make() {
    int a[3] = {1, 2, 3};
    return a;
}

int main() {
    std::cout << make()[0] << '\n';
    return 0;
}
A make must be static
B Functions cannot return C arrays; return std::array<int, 3> or std::vector<int> instead
C The array must be initialised with ()
D main cannot index a call
Question page & discussion
Question 5 Easy

The program below does not compile. Which statement correctly describes the problem?

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

int main() {
    std::array<int> primes{2, 3, 5, 7};
    std::cout << primes.size() << '\n';
    return 0;
}
A primes must be a vector
B std::array needs two template arguments – the element type and the size: std::array<int, 4> (or std::array primes{…} with deduction)
C size() does not exist for std::array
D The values must be in parentheses
Question page & discussion
Question 6 Hard

The program below does not compile. Which statement correctly describes the problem?

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

int main() {
    int a = 1, b = 2;
    std::vector<int&> refs{a, b};
    refs[0] = 10;
    std::cout << a << '\n';
    return 0;
}
A The vector needs reserve
B Containers cannot hold references, because references are not objects that can be assigned or reseated; use pointers or std::reference_wrapper<int>
C a and b must be const
D refs[0] needs at()
Question page & discussion
Question 7 Easy

The program below does not compile. Which statement correctly describes the problem?

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

void add_item(const std::vector<int>& items, int x) {
    items.push_back(x);
}

int main() {
    std::vector<int> v;
    add_item(v, 5);
    std::cout << v.size() << '\n';
    return 0;
}
A x must be a reference
B items is a reference to const, so the non-const member push_back cannot be called; take std::vector<int>& to modify it
C push_back needs two arguments
D Vectors cannot be passed to functions
Question page & discussion
Question 8 Easy

The program below does not compile. Which statement correctly describes the problem?

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

int main() {
    std::vector<int> prices{100, 250, 80};
    for (const auto& p : prices)
        p = p * 2;
    std::cout << prices[0] << '\n';
    return 0;
}
A prices must be an array
B const auto& makes each element read-only; use auto& to double the prices in place
C The loop needs an index
D p * 2 overflows
Question page & discussion
Question 9 Easy

The program below does not compile. Which statement correctly describes the problem?

main.cpp
#include <iostream>

int main() {
    char code[4] = "PUNE1";
    std::cout << code << '\n';
    return 0;
}
A char arrays cannot hold letters
B "PUNE1" needs 6 chars including the terminating '\0', but the array has only 4
C cout cannot print char arrays
D Strings need single quotes
Question page & discussion
Question 10 Medium

The program below does not compile. Which statement correctly describes the problem?

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

int total(const std::array<int, 4>& a) {
    int t = 0;
    for (int x : a) t += x;
    return t;
}

int main() {
    std::array<int, 3> q{1, 2, 3};
    std::cout << total(q) << '\n';
    return 0;
}
A total must take a vector
B The size is part of the type: std::array<int, 3> and std::array<int, 4> are different types, so q cannot bind to the parameter
C q must be non-const
D Range-for does not work on std::array
Question page & discussion
Up next · Exercise 4Array and Vector Programs10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links