C++ Strings MCQ Quiz
Test your knowledge of C++ strings with this interactive quiz. Select the correct answer for each question and see immediate feedback.
Medium Level Questions Medium
Which header file is required to use the std::string class in C++?
The <string> header file contains the definition of the std::string class in C++. <cstring> and <string.h> contain C-style string functions, while <iostream> is for input/output operations.
What is the output of this code?
std::string s = "Hello";
std::cout << s.length();
The string "Hello" has 5 characters. The length() method returns the number of characters in the string, not including the null terminator.
Which method is used to find a substring within a string in C++?
The find() method is used to search for a substring within a string. It returns the position of the first occurrence of the substring, or std::string::npos if not found.
What is the output of this code?
std::string s1 = "Hello";
std::string s2 = "World";
std::cout << s1 + s2;
The + operator is overloaded for std::string to perform concatenation. It combines the two strings without adding any spaces between them.
Which method can be used to extract a substring from a string?
The substr() method is used to extract a substring from a string. It takes a starting position and an optional length parameter.
What is the value of std::string::npos?
std::string::npos is a static member constant value with the greatest possible value for an element of type size_t. It is used to represent "not found" or "all remaining characters" in string operations.
Which method is used to convert a std::string to a C-style string?
The c_str() method returns a pointer to a null-terminated character array (C-style string) that contains the same content as the std::string object.
What is the output of this code?
std::string s = "Hello";
s[0] = 'J';
std::cout << s;
std::string supports direct character access using the [] operator. The code changes the first character from 'H' to 'J', resulting in "Jello".
Which method is used to compare two strings in C++?
The compare() method is used to compare two strings. It returns 0 if the strings are equal, a negative value if the calling string is less than the parameter, or a positive value if it's greater.
What is the output of this code?
std::string s = "Hello";
std::cout << s.empty();
The empty() method returns true (1) if the string is empty and false (0) otherwise. Since the string contains "Hello", it's not empty, so empty() returns false, which is output as 0.
Advanced Level Questions Advanced
What is the time complexity of the find() method in std::string?
The find() method typically uses a linear search algorithm, which has O(n) time complexity in the worst case, where n is the length of the string being searched.
Which C++11 feature allows strings to be initialized with literal suffixes?
C++11 introduced user-defined literals, which allow creating objects from literal values with suffixes. For strings, the "s" suffix can be used to create std::string objects directly: auto str = "Hello"s;
What is the output of this code?
std::string s = "Hello";
std::cout << s.capacity();
The capacity() method returns the number of characters that the string has currently allocated space for, which is typically greater than or equal to the actual length to allow for efficient appending operations.
Which method can be used to reduce memory usage by freeing unused capacity?
The shrink_to_fit() method requests that the string reduce its capacity to fit its size. This can help reduce memory usage after operations that significantly reduce the string's length.
What is the output of this code?
std::string s1 = "Hello";
std::string s2 = std::move(s1);
std::cout << s1.empty();
When a string is moved (using std::move), the source string (s1) is left in a valid but unspecified state. In practice, most implementations will leave it empty, so s1.empty() returns true (1).