Quiz on C++ Strings

Last Updated :
Discuss
Comments

Question 1

What is a C++ string?

  • A

    A data type used to store integers.

  • B

    A data type used to store floats.

  • C

    A sequence of characters.

  • D

    A pointer to an integer.

Question 2

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

  • A

    #include <iostream>

  • B

    #include <string>

  • C

    #include <vector>

  • D

    #include <cstdlib>

Question 3

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

  • A

    string str = "Hello";

  • B

    string str("Hello");

  • C

    string str{"Hello"};

  • D

    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;
}
  • A

    12

  • B

    13

  • C

    14

  • D

    15

Question 5

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

  • A

    concat()

  • B

    attach()

  • C

    connect()

  • D

    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;
}
  • A

    HelloWorld

  • B

    Hello World

  • C

    Hello

  • D

    Hello " " World

Question 7

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

  • A

    size()

  • B

    length()

  • C

    Only a

  • D

    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;
}
  • A

    sgeeksforgeek

  • B

    geeksforgeek

  • C

    geekforgeeks

  • D

    geeksforgeeks

Question 9

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

  • A

    clear()

  • B

    erase()

  • C

    remove()

  • D

    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;
}
  • A

    Hello

  • B

    Hell

  • C

    ello

  • D

    Helo

There are 33 questions to complete.

Take a part in the ongoing discussion