BlackFriday 2024! 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 CPA Topic 8 Question 91 Discussion

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

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

#include

#include

using namespace std;

int main()

{

string s1[]= {"How" , "to" };

s1[0].swap(s1[1]);

for (int i=0; i<2; i++) {

cout << s1[i];

}

return( 0 );

}

Show Suggested Answer Hide Answer
Suggested Answer: B

Contribute your Thoughts:

Eun
4 months ago
I think it prints: toHow too, the swap function swaps the elements in the array
upvoted 0 times
...
Marge
4 months ago
Haha, this question is like a linguistic version of 'the chicken or the egg'. I'm just gonna try to swap my brain with the compiler and see what happens.
upvoted 0 times
...
Nathan
4 months ago
This is a tricky one! I'm gonna guess C) It prints: Ht. Maybe the swap() function somehow cuts the strings in half or something. Who knows?
upvoted 0 times
...
Alpha
4 months ago
Hmm, I'm not sure about this one. I'll go with D) It prints: to, just to be safe. Can't go wrong with the simplest option, right?
upvoted 0 times
Christiane
3 months ago
I agree with User3, it's probably: Hoto
upvoted 0 times
...
Orville
3 months ago
I'm going with: Hoto
upvoted 0 times
...
Murray
3 months ago
No, I believe it prints: Ht
upvoted 0 times
...
Theresia
3 months ago
I think it prints: to
upvoted 0 times
...
...
Lashunda
4 months ago
I'm not sure, but I think it prints: toHow as well
upvoted 0 times
...
Marquetta
4 months ago
I believe it prints: toHow because the swap function swaps the elements in the array
upvoted 0 times
...
Genevive
4 months ago
I think the answer is A) It prints: Hoto. The swap() function should reverse the order of the strings in the array.
upvoted 0 times
Alita
3 months ago
Correct, the output would be Hoto.
upvoted 0 times
...
Lillian
4 months ago
I think the answer is A) It prints: Hoto. The swap() function should reverse the order of the strings in the array.
upvoted 0 times
...
...
Marisha
4 months ago
I think it prints: toHow
upvoted 0 times
...
Nina
4 months ago
The correct answer is B) It prints: toHow. The swap() function swaps the contents of the string objects s1[0] and s1[1], so the output should be 'toHow'.
upvoted 0 times
Buck
4 months ago
That's correct! The swap() function swaps the contents of the string objects s1[0] and s1[1].
upvoted 0 times
...
Xenia
4 months ago
I think it prints: toHow
upvoted 0 times
...
...

Save Cancel