C++ Strings MCQs

1 min read

In C++, strings are handled in two primary ways: traditional C-style strings (null-terminated character arrays) and the modern std::string class introduced in the Standard Template Library (STL). The std::string class, included via the <string> header, provides a highly flexible, dynamic, and memory-safe way to manipulate text compared to raw character arrays. It abstracts memory management and offers a rich set of built-in member functions like append(), find(), substr(), and length() for efficient string operations. Furthermore, C++ supports seamless concatenation using the + operator and simple relational comparisons without needing standard C-library functions like strcmp. Understanding both C-style arrays and std::string is essential for handling text, parsing data, and mastering C++ memory concepts.

1. Which header is commonly used for std::string?

a) <string>
b) <iostream>
c) <vector>
d) <cstring>
Correct Answer: a) <string>
Explanation:
std::string is declared in the string header.

2. In which namespace is std::string defined?

a) global
b) std
c) string
d) cpp
Correct Answer: b) std
Explanation:
std::string is part of the std namespace.

3. What is true about the size of std::string?

a) It is fixed at compile time
b) It must be managed manually
c) It can grow or shrink dynamically
d) It is always 256
Correct Answer: c) It can grow or shrink dynamically
Explanation:
std::string manages dynamic character storage.

4. What is a C-style string?

a) A std::string object
b) A std::vector object
c) An integer array
d) A null-terminated character array
Correct Answer: d) A null-terminated character array
Explanation:
C-style strings end with a null character.

5. Which function returns the number of characters in std::string?

a) size()
b) capacity()
c) max_size()
d) reserve()
Correct Answer: a) size()
Explanation:
size returns the current character count.

6. Which function returns the same value as size()?

a) reserve()
b) length()
c) clear()
d) find()
Correct Answer: b) length()
Explanation:
length is equivalent to size for std::string.

7. Which function checks whether a string is empty?

a) clear()
b) resize()
c) empty()
d) capacity()
Correct Answer: c) empty()
Explanation:
empty returns true when size is zero.

8. Which function adds characters to the end of a string?

a) insert()
b) substr()
c) erase()
d) append()
Correct Answer: d) append()
Explanation:
append adds content at the end.

9. Which operator concatenates two std::string objects?

a) +
b) -
c) *
d) /
Correct Answer: a) +
Explanation:
The plus operator joins strings.

10. Which operator appends to an existing std::string?

a) =
b) +=
c) ==
d) ?
Correct Answer: b) +=
Explanation:
The += operator appends to the left operand.

11. Which function returns a portion of a string?

a) append()
b) compare()
c) substr()
d) clear()
Correct Answer: c) substr()
Explanation:
substr creates a new string from a range.

12. Which function searches for a substring?

a) assign()
b) resize()
c) swap()
d) find()
Correct Answer: d) find()
Explanation:
find returns the position of a match or npos.

13. What is std::string::npos?

a) A constant meaning no position
b) A null pointer
c) The zero index
d) An empty string
Correct Answer: a) A constant meaning no position
Explanation:
npos is returned when a search fails.

14. Which member function performs bounds-checked character access?

a) operator[]
b) at()
c) front()
d) back()
Correct Answer: b) at()
Explanation:
at throws out_of_range for invalid indexes.

15. Which member accesses a character by index without bounds checking?

a) at()
b) front()
c) operator[]
d) back()
Correct Answer: c) operator[]
Explanation:
operator[] does not check the index.

16. Which function returns the first character?

a) back()
b) end()
c) size()
d) front()
Correct Answer: d) front()
Explanation:
front accesses the first character.

17. Which function returns the last character?

a) back()
b) front()
c) begin()
d) clear()
Correct Answer: a) back()
Explanation:
back accesses the last character.

18. Which function appends one character?

a) push_front()
b) push_back()
c) pop_back()
d) append_front()
Correct Answer: b) push_back()
Explanation:
push_back adds a character at the end.

19. Which function removes the last character?

a) clear()
b) erase()
c) pop_back()
d) resize()
Correct Answer: c) pop_back()
Explanation:
pop_back removes the final character.

20. Which function removes all characters from a string?

a) substr()
b) pop_back()
c) erase()
d) clear()
Correct Answer: d) clear()
Explanation:
clear makes the string empty.

21. Which function can remove characters at a given position?

a) erase()
b) insert()
c) append()
d) compare()
Correct Answer: a) erase()
Explanation:
erase deletes characters from a string.

22. Which function replaces a range of characters?

a) substr()
b) replace()
c) find()
d) assign()
Correct Answer: b) replace()
Explanation:
replace substitutes existing characters with new content.

23. Which function inserts characters into a string?

a) pop_back()
b) clear()
c) insert()
d) length()
Correct Answer: c) insert()
Explanation:
insert adds characters at a specified position.

24. Which function compares a string with another string?

a) append()
b) reserve()
c) empty()
d) compare()
Correct Answer: d) compare()
Explanation:
compare returns an ordering result.

25. Which function returns a null-terminated C string?

a) c_str()
b) size()
c) begin()
d) reserve()
Correct Answer: a) c_str()
Explanation:
c_str provides a const char pointer with a null terminator.

26. Which function provides access to the underlying character array?

a) length()
b) data()
c) empty()
d) npos
Correct Answer: b) data()
Explanation:
data returns a pointer to the character storage.

27. Which function returns the current storage capacity?

a) size()
b) length()
c) capacity()
d) max_size()
Correct Answer: c) capacity()
Explanation:
capacity is the allocated storage size.

28. Which function requests capacity for at least a given number of characters?

a) clear()
b) shrink_to_fit()
c) resize()
d) reserve()
Correct Answer: d) reserve()
Explanation:
reserve increases capacity without changing size.

29. Which function changes the size of a string?

a) resize()
b) reserve()
c) capacity()
d) c_str()
Correct Answer: a) resize()
Explanation:
resize adds or removes characters to reach a size.

30. Which function requests reducing capacity to fit size?

a) reserve()
b) shrink_to_fit()
c) clear()
d) push_back()
Correct Answer: b) shrink_to_fit()
Explanation:
shrink_to_fit asks to release excess capacity.

31. Which function returns an iterator to the beginning?

a) end()
b) rbegin()
c) begin()
d) rend()
Correct Answer: c) begin()
Explanation:
begin points to the first character.

32. Which function returns an iterator to the end?

a) front()
b) back()
c) begin()
d) end()
Correct Answer: d) end()
Explanation:
end points one past the last character.

33. Which function returns a reverse iterator to the first reversed element?

a) rbegin()
b) begin()
c) end()
d) size()
Correct Answer: a) rbegin()
Explanation:
rbegin starts reverse traversal at the last character.

34. Which function returns a reverse iterator to the end of reversed sequence?

a) rbegin()
b) rend()
c) begin()
d) end()
Correct Answer: b) rend()
Explanation:
rend marks the end of reverse traversal.

35. Can std::string contain a null character inside its contents?

a) No
b) Only at the end
c) Yes
d) Only if static
Correct Answer: c) Yes
Explanation:
std::string tracks size, so embedded nulls are allowed.

36. Who manages storage for std::string?

a) Manual new and delete
b) Fixed stack storage
c) Hardware directly
d) std::string automatically manages storage
Correct Answer: d) std::string automatically manages storage
Explanation:
std::string handles allocation and deallocation.

37. Which function assigns new contents to a string?

a) assign()
b) find()
c) substr()
d) length()
Correct Answer: a) assign()
Explanation:
assign replaces the string content.

38. Which function copies characters into a character array?

a) assign()
b) copy()
c) append()
d) insert()
Correct Answer: b) copy()
Explanation:
std::string::copy writes characters to an array.

39. Which function reads a full line including spaces into std::string?

a) cin >>
b) scanf without format
c) std::getline
d) cin.get for one char only
Correct Answer: c) std::getline
Explanation:
getline reads until a newline by default.

40. Which can read until a custom delimiter?

a) cin >>
b) string::find
c) string::size
d) std::getline with a delimiter
Correct Answer: d) std::getline with a delimiter
Explanation:
getline accepts an optional delimiter character.

41. What does cin >> std::string do?

a) Reads a token until whitespace
b) Reads a full line
c) Reads binary data
d) Reads only one character
Correct Answer: a) Reads a token until whitespace
Explanation:
Extraction stops at whitespace by default.

42. Which function can discard a leftover newline before getline?

a) cin.get() only
b) cin.ignore()
c) cout
d) return
Correct Answer: b) cin.ignore()
Explanation:
ignore discards characters from the stream.

43. What is std::string_view?

a) An owning string
b) A C-style string
c) A non-owning view of characters
d) An output stream
Correct Answer: c) A non-owning view of characters
Explanation:
string_view refers to existing character data.

44. Does std::string_view own the characters it refers to?

a) Yes always
b) Only on heap
c) Only if copied
d) No
Correct Answer: d) No
Explanation:
string_view does not manage lifetime.

45. What is char in C++?

a) A single character type
b) A string class
c) An array type
d) A stream type
Correct Answer: a) A single character type
Explanation:
char stores one character.

46. What does strlen require?

a) std::string
b) A null-terminated character array
c) std::vector
d) An integer
Correct Answer: b) A null-terminated character array
Explanation:
strlen counts until the null terminator.

47. What does strcpy do?

a) Compares strings
b) Concatenates strings
c) Copies a C-style string
d) Finds a substring
Correct Answer: c) Copies a C-style string
Explanation:
strcpy copies characters including the terminator.

48. What does strcmp do?

a) Copies strings
b) Concatenates strings
c) Returns length
d) Compares C-style strings
Correct Answer: d) Compares C-style strings
Explanation:
strcmp performs lexicographical comparison.

49. What does strcat do?

a) Appends one C-style string to another
b) Compares strings
c) Copies strings
d) Finds strings
Correct Answer: a) Appends one C-style string to another
Explanation:
strcat adds the source to the destination.

50. For a character array holding hello plus a null terminator, what is sizeof?

a) 5
b) 6
c) 4
d) Unknown
Correct Answer: b) 6
Explanation:
sizeof includes the null terminator in the array size.

51. What does == do for two std::string objects?

a) Compares addresses
b) Assigns
c) Compares contents
d) Concatenates
Correct Answer: c) Compares contents
Explanation:
Equality checks character sequences.

52. How are std::string relational comparisons performed?

a) By memory address
b) By capacity
c) By length only
d) Lexicographically
Correct Answer: d) Lexicographically
Explanation:
Comparison follows character order.

53. What is the type of a string literal?

a) An array of const char
b) std::string
c) char
d) const char pointer exactly
Correct Answer: a) An array of const char
Explanation:
String literals are const char arrays that can decay to pointers.

54. Can std::string be constructed from a null-terminated character array?

a) No
b) Yes
c) Only if static
d) Only through string_view
Correct Answer: b) Yes
Explanation:
std::string has constructors for C-style strings.

55. What is the size of an empty std::string?

a) 1
b) -1
c) 0
d) Undefined
Correct Answer: c) 0
Explanation:
An empty string has no characters.

56. What is capacity usually relative to size?

a) Smaller
b) Exactly equal always
c) Zero
d) Greater than or equal
Correct Answer: d) Greater than or equal
Explanation:
Capacity may include extra room for growth.

57. What may reserve do?

a) Increase capacity
b) Decrease size
c) Clear the string
d) Compare strings
Correct Answer: a) Increase capacity
Explanation:
reserve requests more storage.

58. Does reserve guarantee the exact capacity requested?

a) Yes, exactly
b) No, it requests at least that amount
c) It throws always
d) It changes size
Correct Answer: b) No, it requests at least that amount
Explanation:
The implementation may allocate more.

59. What does max_size represent?

a) Current capacity
b) Current length
c) A maximum possible number of characters
d) The null terminator
Correct Answer: c) A maximum possible number of characters
Explanation:
max_size is an implementation limit.

60. What can string reallocation invalidate?

a) Nothing
b) Capacity only
c) Size only
d) Iterators and references
Correct Answer: d) Iterators and references
Explanation:
Reallocation can move the character storage.

61. Can a const char pointer be used to construct std::string?

a) Yes
b) No
c) Only if static
d) Only with streams
Correct Answer: a) Yes
Explanation:
std::string can copy from a null-terminated pointer.

62. What does std::string s(5, 'a'); create?

a) 5
b) aaaaa
c) aaa
d) An empty string
Correct Answer: b) aaaaa
Explanation:
The fill constructor creates five copies of a.

63. What is the size of std::string s(3, 'b');?

a) 0
b) 2
c) 3
d) 4
Correct Answer: c) 3
Explanation:
The constructor creates three characters.

64. Can std::string append a single character?

a) No
b) Only C-style strings
c) Only another string
d) Yes, using push_back or append
Correct Answer: d) Yes, using push_back or append
Explanation:
Strings support character and string appending.

65. Which function inserts a string at a given position?

a) insert()
b) erase()
c) clear()
d) size()
Correct Answer: a) insert()
Explanation:
insert places new characters at a position.

66. Which function removes a given number of characters from a position?

a) find()
b) erase(pos, count)
c) substr()
d) compare()
Correct Answer: b) erase(pos, count)
Explanation:
erase deletes a range starting at pos.

67. Which function finds the first character that matches any character in a set?

a) find_last_not_of()
b) find_first_not_of()
c) find_first_of()
d) rfind()
Correct Answer: c) find_first_of()
Explanation:
find_first_of searches forward for matching characters.

68. Which function finds the last character that matches any character in a set?

a) find_first_of()
b) find()
c) substr()
d) find_last_of()
Correct Answer: d) find_last_of()
Explanation:
find_last_of searches backward for matching characters.

69. Which function finds the first character not in a given set?

a) find_first_not_of()
b) find_first_of()
c) find_last_of()
d) length()
Correct Answer: a) find_first_not_of()
Explanation:
It locates characters outside the specified set.

70. Which function finds the last character not in a given set?

a) find_first_not_of()
b) find_last_not_of()
c) find()
d) empty()
Correct Answer: b) find_last_not_of()
Explanation:
It searches backward for a character outside the set.

71. What does substr return?

a) A pointer
b) A reference to internal storage
c) A new std::string
d) void
Correct Answer: c) A new std::string
Explanation:
substr creates a copied substring.

72. What does replace do?

a) Searches for text
b) Sorts characters
c) Only allocates memory
d) Replaces a range with new contents
Correct Answer: d) Replaces a range with new contents
Explanation:
replace modifies a selected character range.

73. What does compare return when two strings are equal?

a) 0
b) 1
c) -1
d) npos
Correct Answer: a) 0
Explanation:
Zero indicates equality in comparison.

74. What does empty return for an empty string?

a) false
b) true
c) 0
d) npos
Correct Answer: b) true
Explanation:
empty is true when size is zero.

75. What happens if front is called on an empty string?

a) It returns null
b) It returns an empty character
c) It is undefined behavior
d) It throws out_of_range
Correct Answer: c) It is undefined behavior
Explanation:
front requires a non-empty string.

76. What happens if back is called on an empty string?

a) It is safe
b) It returns 0
c) It throws always
d) It is undefined behavior
Correct Answer: d) It is undefined behavior
Explanation:
back requires a non-empty string.

77. What does at do for an invalid index?

a) Throws out_of_range
b) Causes undefined behavior
c) Returns null
d) Resizes the string
Correct Answer: a) Throws out_of_range
Explanation:
at performs bounds checking.

78. Which member accesses a character by index without bounds checking?

a) at()
b) operator[]
c) front()
d) back()
Correct Answer: b) operator[]
Explanation:
operator[] is unchecked for performance.

79. Which function is guaranteed to provide a null-terminated string?

a) size()
b) capacity()
c) c_str()
d) begin()
Correct Answer: c) c_str()
Explanation:
c_str returns a null-terminated character array.

80. In C++17, which non-const member can return a modifiable character array?

a) size()
b) length()
c) empty()
d) data()
Correct Answer: d) data()
Explanation:
Non-const data provides direct access to storage.

81. Can std::string store embedded null characters?

a) Yes
b) No
c) Only at the end
d) Only in character arrays
Correct Answer: a) Yes
Explanation:
The stored size includes embedded nulls.

82. How do C-string functions usually treat an embedded null character?

a) They include it as data
b) They stop at it
c) They skip it
d) They throw
Correct Answer: b) They stop at it
Explanation:
C-string APIs use null as the terminator.

83. What does operator> compare for std::string?

a) Length only
b) Capacity
c) Lexicographical order
d) Memory address
Correct Answer: c) Lexicographical order
Explanation:
Relational operators compare character sequences.

84. Which can swap two strings?

a) copy
b) assign
c) append
d) swap member or std::swap
Correct Answer: d) swap member or std::swap
Explanation:
swap exchanges contents efficiently.

85. Which function converts a string to int?

a) std::stoi
b) std::to_string
c) std::stod
d) std::getline
Correct Answer: a) std::stoi
Explanation:
stoi parses integer text.

86. Which function converts a string to double?

a) std::stoi
b) std::stod
c) std::to_string
d) c_str
Correct Answer: b) std::stod
Explanation:
stod parses floating-point text.

87. Which function converts a number to string?

a) std::stoi
b) std::stod
c) std::to_string
d) strlen
Correct Answer: c) std::to_string
Explanation:
to_string creates text from numeric values.

88. What does std::stoi throw if no conversion can be performed?

a) bad_cast
b) overflow_error
c) logic_error
d) invalid_argument
Correct Answer: d) invalid_argument
Explanation:
invalid_argument indicates unparsable input.

89. What does std::stoi throw if the value is out of range?

a) out_of_range
b) invalid_argument
c) runtime_error only
d) bad_alloc
Correct Answer: a) out_of_range
Explanation:
out_of_range indicates overflow or underflow.

90. Which header is used for std::string_view?

a) <string>
b) <string_view>
c) <iostream>
d) <cstring>
Correct Answer: b) <string_view>
Explanation:
string_view is declared in its own header.

91. What is a risk of using std::string_view?

a) Ownership overhead
b) Automatic null termination
c) Dangling if the underlying data is destroyed
d) No performance benefit
Correct Answer: c) Dangling if the underlying data is destroyed
Explanation:
string_view does not extend lifetime.

92. Is std::string mutable by default?

a) No
b) Only if const
c) Only for literals
d) Yes
Correct Answer: d) Yes
Explanation:
A non-const string can be modified.

93. What is true about const std::string?

a) Its contents cannot be modified
b) It cannot be read
c) It has no size
d) It has no c_str
Correct Answer: a) Its contents cannot be modified
Explanation:
const prevents modification.

94. Can std::string be used in a range-based for loop?

a) No
b) Yes
c) Only character arrays
d) Only with pointers
Correct Answer: b) Yes
Explanation:
std::string provides begin and end.

95. Is std::string storage contiguous?

a) No
b) Only before C++11
c) Yes, since C++11
d) Only string_view is contiguous
Correct Answer: c) Yes, since C++11
Explanation:
Characters are stored contiguously.

96. Does std::string support custom allocators?

a) No
b) Only malloc
c) Only global new
d) Yes
Correct Answer: d) Yes
Explanation:
std::string is a template with an allocator parameter.

97. What does size count?

a) Characters excluding a terminating null
b) Bytes including a terminating null
c) Capacity
d) Words
Correct Answer: a) Characters excluding a terminating null
Explanation:
size reflects logical character count.

98. What does length count?

a) Capacity
b) Characters
c) Null terminator
d) Substrings
Correct Answer: b) Characters
Explanation:
length is equivalent to size.

99. Which function searches for a substring starting from the end?

a) find()
b) find_first_of()
c) rfind()
d) substr()
Correct Answer: c) rfind()
Explanation:
rfind searches backward for a substring.

100. Which is best for a read-only non-owning reference to characters?

a) std::string always
b) char pointer always
c) std::vector
d) std::string_view
Correct Answer: d) std::string_view
Explanation:
string_view is designed for non-owning views.
← Previous: C++ STL MCQs (Standard Template Library)
Next →: C++ Templates - MCQs
NewC++ OOP MCQs Object-Oriented Programming

C++ OOP MCQs Object-Oriented Programming

Object-Oriented Programming (OOP) in C++ revolutionizes software design by organizing code around data (objects) rather than relying solely on functions…

By MCQs Generator
NewC++ Pointers and References - MCQs

C++ Pointers and References - MCQs

In C++, pointers and references are powerful features that provide direct access to memory and allow for highly efficient data…

By MCQs Generator
NewC++ Classes and Objects - MCQs

C++ Classes and Objects - MCQs

In C++, a class is a fundamental building block of Object-Oriented Programming (OOP) that acts as a user-defined blueprint for…

By MCQs Generator