A string in C++ is a sequence of characters used to store text. It is often handled using the <code>std::string</code> class from the Standard Library.
Click to reveal answer
beginner
How do you declare and initialize a string variable in C++?
You declare a string variable using std::string and initialize it with text inside double quotes, for example: std::string name = "Alice";
Click to reveal answer
beginner
How can you find the length of a string in C++?
Use the .length() or .size() method on a string object, for example: name.length() returns the number of characters in the string.
Click to reveal answer
beginner
How do you concatenate two strings in C++?
You can use the + operator to join two strings, for example: std::string fullName = firstName + " " + lastName;
Click to reveal answer
beginner
How do you access individual characters in a string?
You can use the square brackets [] with an index to access characters, for example: char firstChar = name[0]; gets the first character.
Click to reveal answer
Which header file must you include to use std::string in C++?
A<code><string></code>
B<code><iostream></code>
C<code><vector></code>
D<code><cmath></code>
✗ Incorrect
The <string> header defines the std::string class.
What does str.length() return if str is a string?
AFirst character of <code>str</code>
BNumber of characters in <code>str</code>
CMemory size of <code>str</code>
DLast character of <code>str</code>
✗ Incorrect
.length() returns how many characters the string contains.
How do you combine two strings a and b in C++?
A<code>a + b</code>
B<code>a - b</code>
C<code>a * b</code>
D<code>a / b</code>
✗ Incorrect
The + operator joins two strings together.
What is the index of the first character in a C++ string?
ADepends on the string length
B1
C-1
D0
✗ Incorrect
String indexing in C++ starts at 0.
Which of these is a valid way to declare a string variable named city with value "Paris"?
A<code>std::string city = Paris;</code>
B<code>string city = 'Paris';</code>
C<code>std::string city = "Paris";</code>
D<code>char city = "Paris";</code>
✗ Incorrect
Strings use double quotes and must be declared with std::string.
Explain how to create, access, and find the length of a string in C++.
Think about how you handle words or sentences in code.
You got /3 concepts.
Describe how to join two strings together in C++ and why this might be useful.
Imagine putting two pieces of text side by side.
You got /3 concepts.
Practice
(1/5)
1. What does the following C++ code do? std::string s = "Hello"; int len = s.length();
easy
A. Changes the string s to "length"
B. Stores the length of the string "Hello" in variable len
C. Prints the string length to the screen
D. Deletes the string s
Solution
Step 1: Understand the string initialization
The code creates a string variable s with the value "Hello".
Step 2: Understand the length() function
The length() function returns the number of characters in the string, which is 5 for "Hello".
Final Answer:
Stores the length of the string "Hello" in variable len -> Option B
Quick Check:
string length = 5 [OK]
Hint: length() returns number of characters in a string [OK]
Common Mistakes:
Thinking length() changes the string
Confusing length() with printing
Assuming length() deletes the string
2. Which of the following is the correct way to declare and initialize a string in C++?
easy
A. std::string s = "Hello";
B. string s = 'Hello';
C. char s = "Hello";
D. string s = Hello;
Solution
Step 1: Check string declaration syntax
In C++, strings are declared using std::string and initialized with double quotes "".
Step 2: Analyze each option
std::string s = "Hello"; uses std::string with double quotes correctly. string s = 'Hello'; uses single quotes which are for single characters. char s = "Hello"; uses char which holds one character, not a string. string s = Hello; misses quotes around Hello.
Final Answer:
std::string s = "Hello"; -> Option A
Quick Check:
Correct string syntax uses std::string and double quotes [OK]
Hint: Use std::string and double quotes for strings [OK]
Common Mistakes:
Using single quotes for strings
Using char for multiple characters
Forgetting quotes around text
3. What is the output of this C++ code?
std::string s = "Hello"; char c = s[1]; std::cout << c << std::endl;
medium
A. o
B. H
C. l
D. e
Solution
Step 1: Understand string indexing
Strings in C++ are zero-indexed, so s[0] = 'H', s[1] = 'e'.
Step 2: Identify the character at index 1
The code stores s[1] in c, which is 'e', then prints it.
Final Answer:
e -> Option D
Quick Check:
Index 1 character = 'e' [OK]
Hint: Remember string indexing starts at 0 [OK]
Common Mistakes:
Thinking indexing starts at 1
Confusing characters at different positions
Expecting the whole string instead of one char
4. Find the error in this code snippet:
std::string s = "Hello"; char c = s.at(5); std::cout << c << std::endl;
medium
A. No error, prints 'o'
B. Compilation error: cannot assign char
C. Runtime error: out_of_range exception
D. Syntax error: wrong function name
Solution
Step 1: Check string length and indexing
The string "Hello" has length 5, valid indices are 0 to 4.
Step 2: Understand s.at(5) behavior
Accessing s.at(5) is out of range and throws a runtime exception.
Final Answer:
Runtime error: out_of_range exception -> Option C
Quick Check:
Accessing index 5 causes out_of_range error [OK]
Hint: s.at(index) throws error if index invalid [OK]
Common Mistakes:
Assuming s.at() works like s[] without error
Ignoring zero-based indexing
Confusing syntax and runtime errors
5. Given a string std::string s = "apple,banana,cherry";, which code correctly extracts the substring "banana"?
hard
A. std::string sub = s.substr(6, 6);
B. std::string sub = s.substr(5, 6);
C. std::string sub = s.substr(6, 7);
D. std::string sub = s.substr(7, 6);
Solution
Step 1: Identify the start index of "banana"
"apple," has 6 characters (indices 0 to 5), so 'b' starts at index 6.
Step 2: Determine length of "banana"
"banana" has 6 characters.
Step 3: Use substr(start, length)
Calling s.substr(6, 6) extracts 6 characters starting at index 6, which is "banana".
Final Answer:
std::string sub = s.substr(6, 6); -> Option A
Quick Check:
substr(6,6) = "banana" [OK]
Hint: Count start index and length carefully for substr() [OK]
Common Mistakes:
Off-by-one errors in start index
Wrong length parameter
Confusing substr parameters order
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.