0
0
C++programming~5 mins

Character arrays in C++ - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
AA newline character
BA space character
CA tab character
DThe end of the string
How do you declare a character array to hold the word "cat" including the null terminator?
ABoth B and C
Bchar cat[4] = "cat";
Cchar cat[4] = {'c', 'a', 't', '\0'};
Dchar cat[3] = {'c', 'a', 't'};
What is the size of the array when you declare char name[] = "Bob";?
A3
B4
C5
DDepends on compiler
Which of these is NOT a correct way to initialize a character array?
Achar arr[] = "hello";
Bchar arr[6] = {'h', 'e', 'l', 'l', 'o', '\0'};
Cchar arr[5] = "hello";
Dchar arr[6] = "hello";
What will happen if you print a character array without a null terminator using std::cout?
AIt prints characters until it finds a null terminator, possibly reading garbage
BIt prints only the characters in the array
CIt causes a compile error
DIt prints nothing
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.