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 Quiz for Beginners Please wait while the activity loads. If this activity does not load, try refreshing your browser. Also, this page requires javascript. Please visit using a browser with javascript enabled. If loading fails, click here to try again 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; } Hello + Geeks str1 + str2 Hello Geeks HelloGeeks String Data Structure Quiz for Beginners Discuss itQuestion 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; } Syntax error ello llo None String Data Structure Quiz for Beginners Discuss itQuestion 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; } 6, 6 5, 6 6, 5 None String Data Structure Quiz for Beginners Discuss itQuestion 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 ______ -1 1 YES 0 String Data Structure Quiz for Beginners Discuss itQuestion 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? cout << "\\\n"; cout<<"\\\\\n"; cout<<"//n"; cout<<"///n" String Data Structure Quiz for Beginners Discuss itQuestion 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; } Hello world World hello WorldHello Error String Data Structure Quiz for Beginners Discuss itQuestion 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; } swap function is not defined string is not defined here. Variable n is not declared anywhere. None String Data Structure Quiz for Beginners Discuss itQuestion 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++? sort(); sort(str.begin(), str.end()); Sort_asec(); None String Data Structure Quiz for Beginners Discuss itQuestion 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? The image is about the substring of a string The image is about the subsequence of a string The image is about arrays. None String Data Structure Quiz for Beginners Discuss itQuestion 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? The string of length 0 The string of having a special character. The string of having an uppercase letter The string whose reversed string is equal to the original string. String Data Structure Quiz for Beginners Discuss itQuestion 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. 12 There are 20 questions to complete. You have completed questions question Your accuracy is Correct Wrong Partial-Credit You have not finished your quiz. If you leave this page, your progress will be lost. Correct Answer You Selected Not Attempted Final Score on Quiz Attempted Questions Correct Attempted Questions Wrong Questions Not Attempted Total Questions on Quiz Question Details Results Date Score Hint Time allowed minutes seconds Time used Answer Choice(s) Selected Question Text Need more practice! Keep trying! Not bad! Good work! Perfect! Share your thoughts in the comments Add Your Comment Please Login to comment... Similar Reads Complete Guide to String Data Structure Free C Programming Online Course for Beginners Free Java Course Online for Beginners - Java Programming Complete Guide to DSA for Beginners Learn SAP - An Online Course for Beginners Learn JavaScript - Free Course for Beginners LMNs-Data Structure Complete Guide to Linked List Data Structure Complete Guide to Arrays Data Structure GATE Quiz Trending in News View More How to Organize Your Digital Files with Cloud Storage and Automation10 Best Blender Alternatives for 3D Modeling in 2024How to Transfer Photos From iPhone to iPhoneWhat are Tiktok AI Avatars? 30 OOPs Interview Questions and Answers (2024)