LearnFrenzy
Sign in Take a free test

std::string · Exercise 2 of 4

std::string Predict the Output

  • 20 questions
  • Page 2 of 2
  • Answers explained
0Attempted
0Correct
0Wrong
Directions

Read each C++ program and choose what it prints. Every program was compiled and run on a 64-bit system, so the answers are the real output.

Question 11 Medium

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

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

int main() {
    std::string s = "Mississippi";
    std::cout << std::count(s.begin(), s.end(), 's') << ' '
              << s.find_first_of("ps") << ' '
              << s.find_last_not_of('i') << ' '
              << s.find_first_not_of("Mis") << '\n';
    return 0;
}
A 4 2 9 8
B 4 2 10 8
C 4 8 9 8
D 3 2 9 8
Question page & discussion
Question 12 Easy

What is the output of the following program, compiled as C++23?

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

int main() {
    std::string f = "report_2026.pdf";
    std::cout << f.starts_with("rep") << f.starts_with('R') << f.ends_with(".pdf")
              << f.contains("2026") << f.contains('x') << '\n';
    return 0;
}
A 10110
B 11110
C 10111
D 00110
Question page & discussion
Question 13 Medium

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

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

int main() {
    std::string_view sv = "key=value;";
    auto eq = sv.find('=');
    std::string_view key = sv.substr(0, eq);
    std::string_view val = sv.substr(eq + 1);
    val.remove_suffix(1);
    std::cout << key << '|' << val << '|' << sv.size() << '|' << val.size() << '\n';
    return 0;
}
A key|value|10|5
B key|value;|10|6
C key=|value|10|5
D key|value|9|5
Question page & discussion
Question 14 Hard

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 = "x", b = "x", c = "x";
    a += 65;
    b += "65";
    c += std::to_string(65);
    std::cout << a << ' ' << b << ' ' << c << ' ' << a.size() << b.size() << '\n';
    return 0;
}
A xA x65 x65 23
B x65 x65 x65 33
C xA xA x65 22
D x65 x65 xA 33
Question page & discussion
Question 15 Hard

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

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

int main() {
    std::string a = "ab\0cd";
    std::string b("ab\0cd", 5);
    std::cout << a.size() << ' ' << b.size() << ' ' << std::strlen(b.c_str()) << '\n';
    return 0;
}
A 2 5 2
B 5 5 5
C 5 5 2
D 2 2 2
Question page & discussion
Question 16 Medium

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

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

int main() {
    std::istringstream in("a;;b;c;");
    std::string part;
    int n = 0;
    while (std::getline(in, part, ';')) {
        std::cout << '<' << part << '>';
        ++n;
    }
    std::cout << ' ' << n << '\n';
    return 0;
}
A <a><><b><c> 4
B <a><b><c> 3
C <a><><b><c><> 5
D <a;;b;c;> 1
Question page & discussion
Question 17 Medium

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

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

int main() {
    std::string s = "c++ is Fun 2026!";
    std::string up = s;
    std::transform(up.begin(), up.end(), up.begin(),
                   [](unsigned char c) { return static_cast<char>(std::toupper(c)); });
    std::cout << up << " | " << s << '\n';
    return 0;
}
A C++ IS FUN 2026! | c++ is Fun 2026!
B C++ IS FUN 2026! | C++ IS FUN 2026!
C c++ IS FUN 2026! | c++ is Fun 2026!
D C++ Is Fun 2026! | c++ is Fun 2026!
Question page & discussion
Question 18 Medium

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

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

using namespace std::string_literals;

int main() {
    auto a = "Hi"s + " there";
    auto b = "Hi" " there";
    std::cout << a << '|' << b << '|' << a.size() << '|' << sizeof(b) << '\n';
    return 0;
}
A Hi there|Hi there|8|8
B Hi there|Hi there|8|9
C Hi there|Hi|8|8
D Hi there|Hi there|9|9
Question page & discussion
Question 19 Medium

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
B abc... ===>> 11
C abc. ===> 8
D abc ===>> 8
Question page & discussion
Question 20 Hard

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

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

bool is_rotation(const std::string& a, const std::string& b) {
    return a.size() == b.size() && (a + a).find(b) != std::string::npos;
}

int main() {
    std::cout << is_rotation("waterbottle", "erbottlewat")
              << is_rotation("abcd", "acbd")
              << is_rotation("aa", "a")
              << is_rotation("", "") << '\n';
    return 0;
}
A 1001
B 1000
C 1011
D 1101
Question page & discussion
Up next · Exercise 3Find the Error10 questions

Ready to test your Programming?

Timed online tests with instant scores and explanations.

Search LearnFrenzy

Esc

Quick links