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 CPP Exam - 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:

0/2000 characters
Karan
4 months ago
I thought it would just sum up the numbers, not sure about the erase part.
upvoted 0 times
...
Alverta
4 months ago
I think it outputs 3 55, but I might be wrong.
upvoted 0 times
...
Francine
4 months ago
Wait, what does `v1.erase(v1.begin(),v1.end()?3)` even do? Sounds fishy.
upvoted 0 times
...
Ligia
4 months ago
Totally agree, that part is definitely wrong.
upvoted 0 times
...
Lucille
5 months ago
Looks like a compilation error due to the `i??` syntax.
upvoted 0 times
...
Maryrose
5 months ago
I feel like this might compile but throw an error at runtime due to the erase function. I need to double-check that.
upvoted 0 times
...
Alaine
5 months ago
The sum should be 55 if it adds up all the numbers from 10 to 1, but I’m confused about the erase part affecting the output.
upvoted 0 times
...
Cammy
5 months ago
I remember something about iterators and the erase function, but I’m not sure if it’s being used correctly here.
upvoted 0 times
...
Ashlyn
5 months ago
I think there’s a syntax error in the for loop with the "i??" part. That doesn’t look right.
upvoted 0 times
...
Miriam
5 months ago
I'm a bit confused by the `it++` in the `while` loop. Is that incrementing the iterator or the value it's pointing to? I'll need to double-check that.
upvoted 0 times
...
Joesph
5 months ago
I think I know the answer to this one. The `erase` function is removing the first 3 elements of the vector, so the output should be the 4th element (which is 7) and the sum of the remaining elements (which is 55).
upvoted 0 times
...
Shawnee
5 months ago
Okay, let me think this through. The vector is being filled with the numbers 10 down to 1, and then the sum of the elements is being calculated. But I'm not sure what the `erase` function is doing there.
upvoted 0 times
...
Kenny
6 months ago
Hmm, this looks like a tricky one. I'll need to carefully step through the code to understand what's happening.
upvoted 0 times
...
Paris
11 months ago
As a seasoned C++ programmer, I can tell you that this code is a dumpster fire waiting to happen. Trying to erase elements while iterating over the same vector? That's a recipe for disaster!
upvoted 0 times
Leonor
9 months ago
C) program outputs 3 45
upvoted 0 times
...
Luis
9 months ago
B) compilation error
upvoted 0 times
...
Katy
9 months ago
A) program outputs 3 55
upvoted 0 times
...
...
Margarett
11 months 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
Annice
9 months ago
C) program outputs 3 45
upvoted 0 times
...
Keith
9 months ago
B) compilation error
upvoted 0 times
...
Jolanda
10 months ago
A) program outputs 3 55
upvoted 0 times
...
...
Micheal
11 months 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
11 months 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
Lelia
10 months ago
B) compilation error
upvoted 0 times
...
Hollis
10 months ago
The code has a compilation error.
upvoted 0 times
...
Sheridan
11 months ago
A) program outputs 3 55
upvoted 0 times
...
...
Margret
11 months 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
12 months ago
I believe the program will output B) compilation error because of the syntax error in the code.
upvoted 0 times
...
Dorinda
12 months ago
I agree with Telma, there seems to be an issue with the code.
upvoted 0 times
...
Telma
12 months ago
I think the answer is B) compilation error.
upvoted 0 times
...

Save Cancel