LearnFrenzy
Sign in Take a free test

Arrays and Vectors · Predict the Output · Question 4

push_back, front, back and size Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 4 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{3};
    v.push_back(6);
    v.push_back(9);
    v[0] = 1;
    std::cout << v.size() << ' ' << v.front() << ' ' << v.back() << ' ' << v[1] << '\n';
    return 0;
}
  • A 3 1 9 6 Correct
  • B 4 1 9 6
  • C 3 3 9 6
  • D 5 1 9 6
Correct Answer: Option A

v{3} is one element; two push_backs make three. v[0] = 1 replaces the 3.

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