LearnFrenzy
Sign in Take a free test

std::string · Predict the Output · Question 1

Concatenation and size of the result Answer, explanation & discussion

  • Output
  • Easy
  • 0 comments
  • Answer explained
  • Moderated community
Question 1 Back to Exercise

What is the output of the following C++ program on a 64-bit system?

main.cpp
#include <iostream>
#include <string>

int main() {
    std::string a = "Learn", b = "C++";
    std::string c = a + " " + b + '!';
    a += b;
    std::cout << c << ' ' << c.size() << ' ' << a << '\n';
    return 0;
}
  • A Learn C++! 10 LearnC++ Correct
  • B Learn C++! 9 LearnC++
  • C Learn C++! 10 Learn
  • D LearnC++! 9 LearnC++
Correct Answer: Option A

"Learn" + " " + "C++" + '!' has 5 + 1 + 3 + 1 = 10 characters. += appends b to a in place.

Discussion & Comments 0

Share your method, a shortcut, or ask about a step you didn't follow.

No comments yet. Be the first to share your solution!

Post your comment

Comments appear after a quick review by our team.

Search LearnFrenzy

Esc

Quick links