What happens when you attempt to compile and run the following code?
#include
#include
int main ()
{
std::vector
for(int i = 10; i>0; i??)
{
v1.push_back(i);
}
std::vector
int sum = 0;
while(it != v1.end())
{
sum+=it++;
}
std::cout<<*v1.erase(v1.begin(),v1.end()?3)<<" "< return 0; }
Suggested Answer: E
Get Premium CPP Questions
as Interactive Web-Based Practice Test or PDF
Real Exam Format
Detailed Explanations
Instant Access
Get Your Practice Test
30 comments · newest first
KA
Karan
11 months ago
I thought it would just sum up the numbers, not sure about the erase part.
AL
Alverta
11 months ago
I think it outputs 3 55, but I might be wrong.
FR
Francine
11 months ago
Wait, what does `v1.erase(v1.begin(),v1.end()?3)` even do? Sounds fishy.
LI
Ligia
11 months ago
Totally agree, that part is definitely wrong.
LU
Lucille
11 months ago
Looks like a compilation error due to the `i??` syntax.
MA
Maryrose
12 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.
AL
Alaine
12 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.
CA
Cammy
12 months ago
I remember something about iterators and the erase function, but I’m not sure if it’s being used correctly here.
AS
Ashlyn
1 year ago
I think there’s a syntax error in the for loop with the "i??" part. That doesn’t look right.
MI
Miriam
1 year 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.
JO
Joesph
1 year 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).
SH
Shawnee
1 year 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.
KE
Kenny
1 year ago
Hmm, this looks like a tricky one. I'll need to carefully step through the code to understand what's happening.
PA
Paris
1 year 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!
LE
Leonor
1 year ago
C) program outputs 3 45
LU
Luis
1 year ago
B) compilation error
KA
Katy
1 year ago
A) program outputs 3 55
MA
Margarett
1 year 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.
AN
Annice
1 year ago
C) program outputs 3 45
KE
Keith
1 year ago
B) compilation error
JO
Jolanda
1 year ago
A) program outputs 3 55
MI
Micheal
1 year 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!
EU
Eun
1 year 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.
LE
Lelia
1 year ago
B) compilation error
HO
Hollis
1 year ago
The code has a compilation error.
SH
Sheridan
1 year ago
A) program outputs 3 55
MA
Margret
1 year 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.
LA
Lavina
2 years ago
I believe the program will output B) compilation error because of the syntax error in the code.
DO
Dorinda
2 years ago
I agree with Telma, there seems to be an issue with the code.
TE
Telma
2 years ago
I think the answer is B) compilation error.