Recall & Review
beginner
What is a character array in C++?
A character array is a sequence of characters stored in contiguous memory locations. It is used to store text or strings in C++.
Click to reveal answer
beginner
How do you declare a character array to hold the word "hello"?
You declare it like this: <br>
char word[6] = {'h', 'e', 'l', 'l', 'o', '\0'}; <br> The extra '\0' is the null terminator to mark the end of the string.Click to reveal answer
beginner
Why is the null terminator '\0' important in character arrays?
The null terminator '\0' tells C++ where the string ends. Without it, functions that work with strings may read beyond the array, causing errors.
Click to reveal answer
beginner
How can you initialize a character array with a string literal?
You can write: <br>
char greeting[] = "hello"; <br> This automatically adds the null terminator and sets the array size to 6.Click to reveal answer
intermediate
What happens if you forget to add the null terminator in a character array?
The program may read extra garbage characters after the intended string, causing unexpected behavior or crashes.
Click to reveal answer
What does the '\0' character represent in a character array?
✗ Incorrect
The '\0' character is the null terminator that marks the end of a string in a character array.
How do you declare a character array to hold the word "cat" including the null terminator?
✗ Incorrect
Both B and C correctly declare a character array with the word "cat" and the null terminator.
What is the size of the array when you declare
char name[] = "Bob";?✗ Incorrect
The size is 4 because the string "Bob" has 3 characters plus 1 for the null terminator.
Which of these is NOT a correct way to initialize a character array?
✗ Incorrect
Option C is incorrect because the array size 5 is too small to hold the 5 characters plus the null terminator.
What will happen if you print a character array without a null terminator using
std::cout?✗ Incorrect
Without a null terminator, printing may continue past the array boundary, showing garbage characters.
Explain what a character array is and why the null terminator is important.
Think about how C++ knows where the text ends.
You got /3 concepts.
Describe two ways to initialize a character array with the word "dog".
One way uses quotes, the other uses braces.
You got /3 concepts.