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 5 Question 93 Discussion

Actual exam question for C++ Institute's CPA exam
Question #: 93
Topic #: 5
[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:

Willetta
3 months ago
I'm going with D) It prints: to. Surely, the swap() function will only affect the first two elements, and the rest will be ignored, right?
upvoted 0 times
Lynelle
2 months ago
Yeah, I agree. The swap() function only affects the first two elements.
upvoted 0 times
...
Fabiola
3 months ago
I think it prints: to
upvoted 0 times
...
...
Abel
3 months ago
Haha, this question is a classic! The answer is clearly B) It prints: toHow. Swapping the elements of a string array is like a magic trick, isn't it?
upvoted 0 times
...
Deandrea
3 months ago
I think the answer is A) It prints: Hoto. The swap() function will swap the elements of the string array, but the output will be concatenated.
upvoted 0 times
Alfreda
2 months ago
Oh, I see. Thanks for clarifying!
upvoted 0 times
...
Blair
3 months ago
No, it actually prints: toHow.
upvoted 0 times
...
Jacqueline
3 months ago
I think the answer is A) It prints: Hoto.
upvoted 0 times
...
...
Kris
3 months ago
I think it prints: toHow because the swap function swaps the elements at index 0 and 1
upvoted 0 times
...
Magda
3 months ago
The correct answer is B) It prints: toHow. The swap() function will swap the elements of the string array, so the output will be reversed.
upvoted 0 times
Frank
3 months ago
Oh, I see. Thanks for clarifying!
upvoted 0 times
...
Deandrea
3 months ago
Actually, it prints: toHow
upvoted 0 times
...
Tijuana
3 months ago
No, I believe it prints: Hoto
upvoted 0 times
...
Dahlia
3 months ago
I think the answer is B) It prints: toHow
upvoted 0 times
...
...
Fernanda
3 months ago
I'm not sure, but I think it prints: toHow as well
upvoted 0 times
...
Brice
4 months ago
I believe it prints: toHow because the swap function swaps the elements in the array
upvoted 0 times
...
Long
4 months ago
I think it prints: toHow
upvoted 0 times
...

Save Cancel