LearnFrenzy
Sign in Take a free test

Arrays and Vectors · Predict the Output · Question 6

Growing and shrinking a vector with resize Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 6 Back to Exercise

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};
    v.resize(5, 9);
    v.resize(4);
    for (int x : v) std::cout << x;
    std::cout << ' ' << v.size() << '\n';
    return 0;
}
  • A 1239 4 Correct
  • B 12399 5
  • C 1230 4
  • D 1299 4
Correct Answer: Option A

resize(5, 9) appends two 9s; resize(4) drops the last one.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links