LearnFrenzy
Sign in Take a free test

Arrays and Vectors · Predict the Output · Question 7

insert and erase at positions Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 7 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{10, 20, 30, 40};
    v.insert(v.begin() + 1, 15);
    v.erase(v.begin() + 3);
    v.insert(v.end(), 2, 99);
    v.erase(v.begin());
    for (int x : v) std::cout << x << ' ';
    std::cout << '\n';
    return 0;
}
  • A 15 20 40 99 99 Correct
  • B 15 20 30 99 99
  • C 10 15 20 40 99
  • D 15 30 40 99 99
Correct Answer: Option A

{10 15 20 30 40} → erase index 3 (30) → {10 15 20 40} → append two 99s → erase the first element.

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