String Interview Questions

    Question 1C++ String Declaration

    Which is the correct way to declare a string in C++ using the Standard Library?

    Question 2C++ Guess the Output – String Initialization

    What will the following code print?

    #include <iostream> #include <string> using namespace std; int main() { string s = "Hello"; cout << s; return 0; }

    Question 3C++ String Length

    Which function is used to get the length of a string in C++?

    Question 4C++ Guess the Output – String Concatenation

    What will be the output of this code?

    #include <iostream> #include <string> using namespace std; int main() { string a = "Good"; string b = "Morning"; cout << a + " " + b; return 0; }

    Question 5C++ Character Access

    How can we access the 3rd character of a string s?

    Question 6C++ Guess the Output – String Substring

    What will the following code print?

    #include <iostream> #include <string> using namespace std; int main() { string s = "Programming"; cout << s.substr(0, 4); return 0; }

    Question 7C++ String Comparison

    Which operator can be used to compare two strings in C++?

    Question 8C++ Guess the Output – String Append

    What will the output of the following code be?

    #include <iostream> #include <string> using namespace std; int main() { string s = "Code"; s.append("Lab"); cout << s; return 0; }

    Question 9C++ String to C-Style String

    Which function is used to convert a C++ string to a C-style character array?

    Question 10C++ Guess the Output – String Replace

    What will this code print?

    #include <iostream> #include <string> using namespace std; int main() { string s = "HelloWorld"; s.replace(5, 5, " C++"); cout << s; return 0; }