LearnFrenzy
Sign in Take a free test

std::string · Predict the Output · Question 19

resize, assign and append with counts Answer, explanation & discussion

  • Output
  • Medium
  • 0 comments
  • Answer explained
  • Moderated community
Question 19 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 s = "abc";
    s.resize(6, '.');
    std::string t;
    t.assign(3, '=');
    t.append(2, '>');
    s.resize(4);
    std::cout << s << ' ' << t << ' ' << s.size() + t.size() << '\n';
    return 0;
}
  • A abc. ===>> 9 Correct
  • B abc... ===>> 11
  • C abc. ===> 8
  • D abc ===>> 8
Correct Answer: Option A

resize(6, '.') pads to "abc...", resize(4) cuts it back to "abc.". assign(3, '=') and append(2, '>') build "===>>".

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