C++ String Aptitude Questions and Answers

C++ String Aptitude: This section contains C++ String Aptitude Questions and Answers with explanations.
Submitted by Nidhi, on February 21, 2021

1) There are the following statements that are given below, which of them are correct about string in C++?

  1. A string is the sequence of characters.
  2. In C++, the string is a predefined class. This is used to perform string operations.
  3. In C++, we can also use character array as well as string class.
  4. All of the above

Options:

  1. A and B
  2. A and C
  3. B and C
  4. D

2) What is the correct output of the given code?

#include <iostream>
#include <string.h>
using namespace std;

int main()
{
    string str1 = "ABC";
    string str2;

    strcpy(str2, str1);

    cout << str2 << endl;

    return 0;
}

Options:

  1. ABC
  2. Syntax error
  3. Garbage value
  4. Runtime error

3) What is the correct output of the given code?

#include <iostream>
#include <string.h>
using namespace std;

int main()
{
    string str1 = "ABC";
    string str2;

    str1.copy(str2);

    cout << str2 << endl;

    return 0;
}

Options:

  1. ABC
  2. Syntax error
  3. Garbage value
  4. Runtime error

4) What is the correct output of the given code?

#include <iostream>
#include <string.h>
using namespace std;

int main()
{
    string str1 = "ABC";
    char str2[4];

    str1.copy(str2, 3);

    cout << str2 << endl;

    return 0;
}

Options:

  1. ABC
  2. Syntax error
  3. Garbage value
  4. Runtime error

5) What is the correct output of the given code?

#include <iostream>
#include <string.h>
using namespace std;

int main()
{
    string str1 = "ABC";
    string str2;

    str2 = str1;

    cout << str2 << endl;

    return 0;
}

Options:

  1. ABC
  2. Syntax error
  3. Garbage value
  4. Runtime error

6) What is the correct output of the given code?

#include <iostream>
#include <string.h>
using namespace std;

int main()
{
    string str1 = "ABC";
    string str2;

    str2 = str1;

    cout << str2.length();

    return 0;
}

Options:

  1. 3
  2. Syntax error
  3. Garbage value
  4. Runtime error

7) Which of the following function is used to remove all characters of a string object in C++?

  1. removeall()
  2. remove()
  3. clear()
  4. delete()

8) What is the correct output of the given code?

#include <iostream>
#include <string.h>
using namespace std;

int main()
{
    char ch[4] = "ABC";

    string str;

    strcpy((char*)str, ch);
    cout << str << endl;

    return 0;
}

Options:

  1. ABC
  2. Syntax error
  3. Garbage value
  4. Runtime error

9) What is the correct output of the given code?

#include <iostream>
#include <string.h>
using namespace std;

int main()
{
    char ch[4] = "ABC";

    string str;

    for (int i = 0; i < 3; i++)
        str += ch[i];

    cout << str << endl;

    return 0;
}

Options:

  1. ABC
  2. Syntax error
  3. Garbage value
  4. Runtime error

10) What is the correct output of the given code?

#include <iostream>
#include <string.h>
using namespace std;

int main()
{
    string str = "Hello World";
    ;
    char ch;

    ch = str.at(4);

    cout << ch << endl;

    return 0;
}

Options:

  1. l
  2. o
  3. Syntax error
  4. Runtime error

11) What is the correct output of the given code?

#include <iostream>
#include <string.h>
using namespace std;

int main()
{
    string str = "Hello World";

    cout << str.substr(6) << endl;

    return 0;
}

Options:

  1. World
  2. Garbage value
  3. Syntax error
  4. Runtime error

12) The substr() function is overloaded?

  1. Yes
  2. No

13) Which of the following function is used to concatenate a string to another string object in C++?

  1. concat()
  2. strcat()
  3. append()
  4. None of the above

14) Which of the following function is used to change characters in string object from the specified index?

  1. change()
  2. update()
  3. edit()
  4. replace()

15) Which of the following function is used to get the index of a specified character in a string object?

  1. get()
  2. search()
  3. loc()
  4. find()





Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.