New Year Sale 2026! 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 8 Question 101 Discussion

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

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

#include

#include

using namespace std;

template

void print(T start, T end) {

while (start != end) {

std::cout << *start << " "; start++;

}

}

int main()

{

int t1[] ={ 1, 7, 8, 4, 5 };

list l1(t1, t1 + 5);

int t2[] ={ 3, 2, 6, 9, 0 };

list l2(t2, t2 + 5);

l1.sort();

list::iterator it = l2.begin();

it++; it++;

l1.splice(l1.end(),l2, it, l2.end());

print(l1.begin(), l1.end()); cout<<"Size:"<

print(l2.begin(), l2.end()); cout<<"Size:"<

return 0;

}

Show Suggested Answer Hide Answer
Suggested Answer: B

Contribute your Thoughts:

0/2000 characters
Argelia
3 months ago
I’m surprised it splices like that, thought it would mess up!
upvoted 0 times
...
Adolph
3 months ago
Definitely not C, that would be too easy!
upvoted 0 times
...
Anglea
3 months ago
Wait, does it really compile without errors?
upvoted 0 times
...
Dyan
4 months ago
I think it should be option A, right?
upvoted 0 times
...
Andree
4 months ago
Looks like it outputs 1 4 5 7 8 6 9 0 Size:8 3 2 Size:2.
upvoted 0 times
...
Misty
4 months ago
I’m leaning towards option A for the output, but I’m a bit confused about how the sort and splice operations interact.
upvoted 0 times
...
Goldie
4 months ago
I feel like this is similar to a practice question we did, but I can't remember if the output would include all elements from both lists or just part of them.
upvoted 0 times
...
Billy
4 months ago
I remember something about `splice` moving elements from one list to another, but I can't recall exactly how it affects the sizes.
upvoted 0 times
...
Laurel
5 months ago
I think the code should compile fine since it uses standard library features correctly, but I'm not sure about the output.
upvoted 0 times
...
Keneth
5 months ago
I think I've got this. The splice() call is moving the elements from the second iterator in l2 up to the end of l2 and appending them to the end of l1. The output should reflect that rearrangement. Let me double-check my work.
upvoted 0 times
...
Stephane
5 months ago
I've got a strategy here. I'll first sort out l1, then focus on the splice() call. That should help me understand how the elements are being rearranged between the two lists. Gotta pay close attention to the iterators.
upvoted 0 times
...
Patti
5 months ago
I'm a bit confused by the splice() call. It's not clear to me exactly which elements are being moved from l2 to l1. I'll need to review the documentation on that function.
upvoted 0 times
...
Brandon
5 months ago
Okay, let's see. The code is sorting the first list, l1, and then splicing elements from the second list, l2, into l1. I think the key is understanding how the splice() function works.
upvoted 0 times
...
Joaquin
5 months ago
Hmm, this looks like it's testing our understanding of the list container and its member functions. I'll need to carefully trace the code step-by-step to figure out what's happening.
upvoted 0 times
...
Rosina
10 months ago
Wait, did they just throw a bunch of random numbers into a list and expect us to make sense of it? Sounds like a typical programmer's idea of a good time.
upvoted 0 times
...
Hubert
10 months ago
D, definitely D. That's my final answer, and I'm sticking to it like a piece of chewing gum on the underside of a desk.
upvoted 0 times
...
Bulah
10 months ago
Hmm, I'm not sure about this one. Maybe I should've paid more attention in that C++ class instead of doodling unicorns in my notebook.
upvoted 0 times
Elden
8 months ago
User3: I agree, option C seems correct based on the code.
upvoted 0 times
...
Minna
9 months ago
User2: I think the program outputs: 1 3 4 5
upvoted 0 times
...
Trinidad
9 months ago
User1: Don't worry, let's try to figure it out together.
upvoted 0 times
...
...
Kina
10 months ago
Haha, I'm gonna go with B. Who needs to sort lists when you can just randomly rearrange them?
upvoted 0 times
Herman
9 months ago
User3: I agree, just randomly rearrange them!
upvoted 0 times
...
Ora
9 months ago
User2: Yeah, who needs to sort lists anyway?
upvoted 0 times
...
Shawn
10 months ago
User1: I think the program outputs: 3 4 5
upvoted 0 times
...
...
Veronika
10 months ago
The correct answer is C. The code sorts the first list, then splices the elements from the second list starting from the third element (index 2) to the end of the second list, and appends them to the end of the first list. This results in the output: 1 3 4 5.
upvoted 0 times
Lavonna
9 months ago
C) program outputs: 1 3 4 5
upvoted 0 times
...
Daniel
9 months ago
B) program outputs: 3 4 5
upvoted 0 times
...
Altha
9 months ago
A) program outputs: 1 2 4 5
upvoted 0 times
...
...
Jessenia
11 months ago
Hmm, I see your point. Let's wait for more opinions before deciding.
upvoted 0 times
...
Lizette
11 months ago
I disagree, I believe the answer is C) program outputs: 1 3 4 5.
upvoted 0 times
...
Jessenia
11 months ago
I think the answer is A) program outputs: 1 2 4 5.
upvoted 0 times
...

Save Cancel