Deal of The Day! Hurry Up, Grab the Special Discount - Save 25% - Ends In 00:00:00 Coupon code: SAVE25
Welcome to Pass4Success

- Free Preparation Discussions

C++ Institute Exam CPP Topic 2 Question 99 Discussion

Actual exam question for C++ Institute's CPP exam
Question #: 99
Topic #: 2
[All CPP Questions]

What happens when you attempt to compile and run the following code?

#include

#include

int main ()

{

std::vectorv1;

for(int i = 10; i>0; i??)

{

v1.push_back(i);

}

std::vector::iterator it = v1.begin();

int sum = 0;

while(it != v1.end())

{

sum+=it++;

}

std::cout<<*v1.erase(v1.begin(),v1.end()?3)<<" "<

return 0;

}

Show Suggested Answer Hide Answer
Suggested Answer: E

Contribute your Thoughts:

Margarett
1 days ago
The program will output 7 55. The sum of the elements in the vector is 55, and the erase function will remove all but the last 3 elements, leaving 7 as the final element.
upvoted 0 times
...
Micheal
3 days ago
Haha, this code is a real brain-teaser! I bet the correct answer is going to be something totally unexpected. That's how they get you in these certification exams!
upvoted 0 times
...
Eun
10 days ago
The program will output 3 55. The erase function returns an iterator to the element after the last element removed, and we're dereference that iterator to get the value 3.
upvoted 0 times
...
Margret
12 days ago
The code will have a compilation error. You can't iterate over a vector and erase elements at the same time, it will cause undefined behavior.
upvoted 0 times
...
Lavina
23 days ago
I believe the program will output B) compilation error because of the syntax error in the code.
upvoted 0 times
...
Dorinda
26 days ago
I agree with Telma, there seems to be an issue with the code.
upvoted 0 times
...
Telma
1 months ago
I think the answer is B) compilation error.
upvoted 0 times
...

Save Cancel