String Data Structure Quiz for Beginners

Last Updated : 25 Sep, 2023

Strings are considered a data type in general and are typically represented as arrays of bytes (or words) that store a sequence of characters. Strings are defined as an array of characters. The difference between a character array and a string is the string is terminated with a special character ‘\0’.
More on String Data Structure

String Data Structure

String Data Structure

Question 1

What is the output of the following code?

C++

#include <iostream>
using namespace std;

int main()
{

    string str1 = "Hello";
    string str2 = "Geeks";
    string str3 = str1 + str2;
    cout << str3 << endl;

    return 0;
}
Cross

Hello +  Geeks

Cross

str1 + str2

Cross

Hello Geeks

Tick

HelloGeeks



Question 1-Explanation: 

"+" operator is used to concatenate two string , so the str1 and str2 is concatenated into third string.

Hence, Option (D) is the correct answer.

Question 2

What is the output of the following code?

C++

#include <iostream>
using namespace std;

int main()
{

    string str = "Hello";
    cout << str.substr(2, 5);
    return 0;
}
Cross

Syntax error

Cross

ello

Tick

llo

Cross

None



Question 2-Explanation: 

The substr function takes two values pos and len as an argument and returns a newly constructed string object with its value initialized to a copy of a sub-string of this object. Copying of string starts from pos and is done till pos+len means [pos, pos+len).

  • pos: Position of the first character to be copied.
  • len: Length of the sub-string.

So str.substr(2, 5) will copy the characters from index 2 to 4. which is llo.

Hence (C) is the correct answer.

Question 3

What is the output of the following code?

C++

#include<bits/stdc++.h> 
using namespace std;

int main()
{
    // Write C++ code here
    char str_array2[] = "Geeks";
    cout << sizeof(str_array2) << " ";
    char str_array[] = { 'G', 'e', 'e', 'k', 's'};
    cout << sizeof(str_array);
    return 0;
}
Cross

6, 6

Cross

5, 6

Tick

6, 5

Cross

None



Question 3-Explanation: 

when we are initializing a char array as a value of the string, then  C++ automatically adds a null character at the end of the string, and the size of the char array is increased by one. 

eg:    char str_array2[] = "Geeks";  // the length of the array is 6 here.

But when we are initializing a char array in the form of sequence of characters,  then the size of the char array is the number of characters present in the array.

eg:   char str_array[] = { 'G', 'e', 'e', 'k', 's'};   // the length of the array is 5 here.

Hence Option (C) is the correct answer.

Question 4

If two string s are identical, then strcmp() functions returns ______

Cross

-1

Cross

1

Cross

YES

Tick

0



Question 4-Explanation: 

If two strings are identical, then strcmp() functions returns 0.

The strcmp() function returns three different values after the comparison of the two strings which are as follows:

  • A value equal to zero when both strings are found to be identical. That is, all of the characters in both strings are the same.
  • A value greater than zero is returned when the first not-matching character in first_str has a greater ASCII value than the corresponding character in second_str or we can also say that if the character in first_str is lexicographically after the character of second_str, then zero is returned.
  • A value less than zero is returned when the first not-matching character in first_str has a lesser ASCII value than the corresponding character in second_str. We can also say that if the character in first_str is lexicographically before the character of second_str, zero is returned.

Hence Option (C) is correct.

Question 5

How will you print "\n" on the screen?

Tick

  cout << "\\\n";

Cross

cout<<"\\\\\n";

Cross

cout<<"//n";

Cross

cout<<"///n"



Question 5-Explanation: 

The statement cout << "\\\n";  will print \n on the screen.

Hence (A) is the correct option.

Question 6

What will the output of the following questions:

C++

#include <bits/stdc++.h>
using namespace std;

int main()
{

    string a = "Hello";
    string b = "World";
    string c = b.append(a);
    cout << c << endl;

    return 0;
}
Cross

Hello world

Cross

World hello

Tick

WorldHello

Cross

Error



Question 6-Explanation: 

append function will append the characters of string a into b.

For more Reference Please visit:
https://www.geeksforgeeks.org/stdstringappend-in-c/

Hence Option(C) is correct.

Question 7

What is missing in the given code?

C++

#include <bits/stdc++.h>
using namespace std;

void reverseStr(string& str)
{

    for (int i = 0; i < n / 2; i++)
        swap(str[i], str[n - i - 1]);
}

// Driver program
int main()
{
    string str = "geeksforgeeks";
    reverseStr(str);
    cout << str;
    return 0;
}
Cross

swap function is not defined

Cross

string is not defined here.

Tick

Variable n is not declared anywhere.

Cross

None



Question 7-Explanation: 

Variable n is the size of the string here, which is not defined here. the correct code will be as follow:

C++

#include using namespace std; void reverseStr(string& str) { int n = str.length(); for (int i = 0; i < n / 2; i++) swap(str[i], str[n - i - 1]); } // Driver program int main() { string str = "geeksforgeeks"; reverseStr(str); cout << str; return 0; }[/sourcecode][tabbyending]

Hence (C) is the correct Option.

Question 8

What is the function present to sort a string in C++?

Cross

sort();

Tick

 sort(str.begin(), str.end());

Cross

Sort_asec();

Cross

None



Question 8-Explanation: 

 sort(str.begin(), str.end()); is the syntax for sorting the string in C++.

For more reference, Please visit:
https://www.geeksforgeeks.org/sort-string-characters/

Hence Option(B) is the correct answer.

Question 9

What is the below image explaining?

 

Cross

The image is about the substring of a string

Tick

The image is about the subsequence of a string

Cross

The image is about arrays.

Cross

None



Question 9-Explanation: 

The above image is about the subsequences of a string.

Subsequences of String

Hence Option (B) is the correct answer.

Question 10

What is a palindromic string?

Cross

The string of length 0 

Cross

The string of having a special character.

Cross

The string of having an uppercase letter

Tick

The string whose reversed string is equal to the original string.



Question 10-Explanation: 

A string is called a palindrome if the reverse of the string is the same as the original one. For example, “madam”, “racecar”, and “12321”.

 

Hence Option (D) is correct.

There are 20 questions to complete.


Share your thoughts in the comments

Similar Reads