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 6 Question 104 Discussion

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

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

#include

#include

int main ()

{

int t[]={1,2,3,4,5};

std::vectorv1(t,t+5);

std::vectorv2(v1);

v1.resize(10);

v2.reserve(10);

std::vector::iterator i = v1.begin();int ii = 0;

while (i != v1.end()) { std::cout<

i = v2.begin();ii=0;

while (i != v2.end()) { std::cout<

return 0;

}

Show Suggested Answer Hide Answer
Suggested Answer: E

Contribute your Thoughts:

Jose
21 days ago
Wait, is that a typo in the loop? I think the '??' is supposed to be '++'. Gotta pay attention to the small details!
upvoted 0 times
...
Alecia
25 days ago
I'm picturing the output being a weird mix of 1s and the original elements. This should be a fun one to work through.
upvoted 0 times
Christoper
7 days ago
A) program outputs 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
upvoted 0 times
...
...
Nichelle
1 months ago
Haha, I can already see the compiler throwing a fit at this code. Time to brush up on my C++ vector knowledge!
upvoted 0 times
Rosio
16 days ago
I think it will output a compilation error.
upvoted 0 times
...
...
Trina
2 months ago
Ah, the classic vector resizing and copying conundrum. I bet the answer has something to do with the difference between resize() and reserve().
upvoted 0 times
Roosevelt
21 days ago
B) compilation error
upvoted 0 times
...
Roosevelt
1 months ago
C) program outputs 1 1 1 1 1 1 1 1 1 1 1 2 3 4 5
upvoted 0 times
...
...
Micaela
2 months ago
This looks like a tricky one! I'm going to have to think through the memory management implications carefully.
upvoted 0 times
Howard
25 days ago
I think it will result in a compilation error
upvoted 0 times
...
Howard
1 months ago
No, it will output 1 1 1 1 1 1 1 1 1 1 1 2 3 4 5
upvoted 0 times
...
Howard
1 months ago
It will output 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
upvoted 0 times
...
...
Beth
2 months ago
I'm not sure, but I think the answer might be C) program outputs 1 1 1 1 1 1 1 1 1 1 1 2 3 4 5 because of the way the vectors are being manipulated.
upvoted 0 times
...
Andrew
2 months ago
I agree with Goldie, the code looks like it will not compile correctly.
upvoted 0 times
...
Goldie
2 months ago
I think the answer is B) compilation error because there seems to be an issue with the code.
upvoted 0 times
...

Save Cancel