LearnFrenzy
Sign in Take a free test

C++ Programming · Topic 5 of 27

std::string

  • 4 exercises
  • 60 questions
  • ~60 min
  • Answers explained

Interview Questions

std::string · Study material

C++ std::string – Interview Questions

1. What is the difference between std::string and a C-style string?

A C string is a null-terminated char array with a fixed buffer and C library functions. std::string is a class that stores its length, manages and grows its memory, and supports copying, comparison and concatenation with operators.

2. What does find() return if the substring is not found?

std::string::npos, the largest value of size_t. Compare with npos, not with -1.

3. What is the difference between size() and capacity() for a string?

size is the number of characters; capacity is how many characters fit before reallocation. Short strings often fit inside the object itself (small string optimisation).

4. What does substr(pos, len) do?

Returns a new string with up to len characters starting at pos. It throws std::out_of_range if pos > size().

5. How do you convert an int to a string and back?

std::to_string(n) and std::stoi(s); also std::format("{}", n) and std::from_chars / std::to_chars for fast, locale-independent conversion.

6. Why does "Hello" + "World" not compile?

Both operands are const char arrays that decay to pointers, and pointers cannot be added. At least one operand must be a std::string: std::string("Hello") + "World" or "Hello"s + "World".

7. What is std::string_view?

A C++17 non-owning reference to a sequence of characters (pointer + length). It is cheap to copy, avoids allocations, but must not outlive the data it refers to.

8. How are strings compared?

Lexicographically, character by character using char values; a shorter string that is a prefix of a longer one is smaller.

9. What is the small string optimisation (SSO)?

Implementations store short strings (typically up to 15–22 characters) inside the string object without heap allocation, making them very cheap.

10. How do you check if a string starts with a prefix?

s.starts_with("pre") in C++20, or s.rfind("pre", 0) == 0 in older code.

11. What does c_str() return?

A const char* to the string's characters followed by a null terminator, valid until the string is modified or destroyed.

12. What happens with s.at(i) when i is out of range?

It throws std::out_of_range; s[i] does not check and causes undefined behaviour (s[s.size()] is allowed and gives '\0').

13. How do you split a string by a delimiter?

Use std::getline on an std::istringstream with the delimiter, or loop with find and substr. C++20 ranges offer std::views::split.

14. How do you remove all spaces from a string?

std::erase(s, ' ') in C++20, or s.erase(std::remove(s.begin(), s.end(), ' '), s.end()).

15. Is std::string mutable?

Yes – unlike strings in Java or Python, a C++ string's characters can be changed in place.

16. How do you check whether two strings are anagrams?

Sort copies of both and compare, or count character frequencies in an array of 26 or 256 ints.

17. What exceptions can std::stoi throw?

std::invalid_argument when no conversion can be performed, and std::out_of_range when the value does not fit in an int.

18. How do you reverse a string?

std::reverse(s.begin(), s.end()) in place, or std::string r(s.rbegin(), s.rend()) for a reversed copy.

19. What is the "s" suffix in "abc"s?

A user-defined literal from std::string_literals that creates a std::string instead of a const char array.

20. What is the complexity of s.find(t)?

O(n × m) in the worst case for the usual implementation; for many searches on large texts, algorithms such as std::boyer_moore_searcher are faster.

Ready to test your Programming?

Timed online tests with instant scores and explanations.

About this section

About C++ Programming

C++ adds object-oriented programming, templates and the powerful Standard Template Library (STL) to C. It powers game engines, browsers, trading systems and most competitive programming. This section takes you from basic syntax to classes, inheritance, polymorphism, modern C++ (C++11 to C++23) and the STL – every concept with runnable examples and practice questions checked on a real compiler.

Why it matters

  • OOP concepts – classes, inheritance, polymorphism, encapsulation – are asked in almost every interview
  • C++ with the STL is the most popular language for competitive programming and DSA rounds
  • Questions on constructors, virtual functions, references and smart pointers decide technical rounds
  • Modern C++ skills are in demand at product companies, fintech and game studios

Who it is for

  • School students (Class 11–12 computer science with C++) preparing for board exams and practicals
  • College students (B.Tech, BCA, MCA) learning OOP and data structures for semester exams and labs
  • Freshers preparing for placements, coding tests and C++ technical interviews
  • Competitive programmers who want to master the STL

What every topic gives you

  • Study material in five tabs: concepts with runnable examples, coding techniques, programs, interview questions and a cheat sheet
  • MCQs that check your understanding of every concept
  • "Predict the output" puzzles – the trickiest part of written tests
  • "Find the error" programs that train you to spot bugs quickly
  • Complete programs with solutions and real output, verified on a compiler

Asked in

  • Campus placements
  • Product company interviews
  • Coding rounds & DSA
  • Competitive programming
  • GATE CS
  • University exams

How to use this section

  1. Read the study material of a topic and run the examples yourself.
  2. Practise the four exercises – MCQs, output, errors, programs – and read every explanation.
  3. Test yourself with the timed online test, then revise the topics you got wrong.

Search LearnFrenzy

Esc

Quick links