• Tutorials
  • Courses
  • Tracks

Quiz on C++ Strings

Last Updated :
Discuss
Comments

Question 1

What is a C++ string?

  • A data type used to store integers.

  • A data type used to store characters.

  • A sequence of characters.

  • A pointer to an integer.

Question 2

Which of the following header file is required to use C++ strings?

  • #include <iostream>

  • #include <string>

  • #include <vector>

  • #include <cstdlib>

Question 3

Which of the following is a correct way to initialize a string in C++?

  • string str = "Hello";

  • string str("Hello");

  • string str{"Hello"};

  • All of the above

Question 4

What will be the output of the following code?

C++
#include <iostream>
#include <string>
using namespace std;

int main() {
    string str = "Hello, Geek!";
    cout << str.length();
    return 0;
}
  • 12

  • 13

  • 14

  • 15

Question 5

Which method is used to concatenate two strings in C++?

  • concat()

  • attach()

  • connect()

  • append()

Question 6

What will be the output of the following code?

C++
#include <iostream>
#include <string>
using namespace std;

int main() {
    string str1 = "Hello";
    string str2 = "World";
    string str3 = str1 + " " + str2;
    cout << str3;
    return 0;
}
  • HelloWorld

  • Hello World

  • Hello

  • Hello " " World

Question 7

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

  • size()

  • length()

  • Only a

  • both a and b

Question 8

What will be the output of the following code?

C++
#include <iostream>
#include <string>
using namespace std;

int main() {
    string str = "geeksforgeek";
    str.push_back('s');
    cout << str;
    return 0;
}
  • sgeeksforgeek

  • geeksforgeek

  • geekforgeeks

  • geeksforgeeks

Question 9

Which of the following method(s) is used to clear the contents of a string in C++?

  • clear()

  • erase()

  • remove()

  • delete()

Question 10

What will be the output of the following code?

C++
#include <iostream>
#include <string>
using namespace std;

int main() {
    string str = "Hello";
    str.pop_back();
    cout << str;
    return 0;
}
  • Hello

  • Hell

  • ello

  • Helo

There are 35 questions to complete.

Take a part in the ongoing discussion