What does std::to_string(2.5) return?
For floating-point values to_string uses printf's %f format: six decimals. Use std::format("{}", 2.5) for the shortest form, "2.5".
std::string · Exercise 1 of 4
Twenty multiple-choice questions on std::string: creation, concatenation, find and npos, substr, replace, erase, comparison, conversions, string_view and C++20/23 helpers. Pick an option to check your answer instantly.
What does std::to_string(2.5) return?
For floating-point values to_string uses printf's %f format: six decimals. Use std::format("{}", 2.5) for the shortest form, "2.5".
Which statement about std::string_view is true?
A view is just a pointer and a length. Returning a view of a local std::string creates a dangling view.
Why would you call s.c_str()?
The pointer remains valid only until s is modified or destroyed.
Which C++20 call tests whether url begins with "https"?
starts_with and ends_with were added in C++20; contains followed in C++23.
With using namespace std::string_literals;, what is the type of "hi"s?
The s suffix creates a std::string; the sv suffix (std::string_view_literals) creates a std::string_view.
Given std::string s = "cat";, what does s[0] = 'b'; do?
Unlike Java or Python strings, std::string is mutable. (Modifying a string literal itself would be undefined behaviour.)
Which reads "New Delhi" completely into a std::string city?
>> stops at the space and would read only "New".
What does s.find_first_of("aeiou") return?
The argument is a set of characters, not a substring. find_first_not_of finds the first character that is not in the set.
Given std::string s = "Code";, what does s += 65; do?
operator+= has a char overload, and 65 converts to char. To append the digits, write s += std::to_string(65).
Which call reverses s in place?
std::string has no reverse member, but all <algorithm> functions work on its iterators. C++20 also offers std::ranges::reverse(s).
Timed online tests with instant scores and explanations.