0
0
C++programming~10 mins

Character arrays in C++ - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a character array named name with space for 10 characters.

C++
char name[[1]];
Drag options to blanks, or click blank then click option'
A10
Bchar
C5
Dstring
Attempts:
3 left
💡 Hint
Common Mistakes
Using a data type like 'char' or 'string' inside the brackets instead of a number.
Forgetting to specify the size of the array.
2fill in blank
medium

Complete the code to initialize the character array word with the string "hello".

C++
char word[] = [1];
Drag options to blanks, or click blank then click option'
A'hello'
B"hello"
Chello
D'h','e','l','l','o'
Attempts:
3 left
💡 Hint
Common Mistakes
Using single quotes instead of double quotes for strings.
Not enclosing the string in any quotes.
3fill in blank
hard

Fix the error in the code to correctly print the first character of the array letters.

C++
char letters[] = {'a', 'b', 'c'};
std::cout << letters[1] << std::endl;
Drag options to blanks, or click blank then click option'
A[0]
B(0)
C{0}
D<0>
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses or curly braces instead of square brackets.
Using an incorrect index like 1 instead of 0.
4fill in blank
hard

Fill both blanks to create a loop that prints each character in the array chars until the null character is found.

C++
char chars[] = {'C', '+', '+', '\0'};
for (int i = [1]; chars[i] [2] '\0'; i++) {
    std::cout << chars[i];
}
Drag options to blanks, or click blank then click option'
A0
B1
C!=
D==
Attempts:
3 left
💡 Hint
Common Mistakes
Starting the loop at 1 instead of 0.
Using '==' instead of '!=' in the loop condition.
5fill in blank
hard

Fill all three blanks to create a character array greeting initialized with "Hi!" and print its length excluding the null character.

C++
char greeting[] = [1];
int length = 0;
while (greeting[2] != '\0') {
    [3];
}
std::cout << length << std::endl;
Drag options to blanks, or click blank then click option'
A"Hi!"
B[length]
C++length
D[length++]
Attempts:
3 left
💡 Hint
Common Mistakes
Using single quotes for the string initialization.
Not incrementing the length variable correctly.
Using the wrong index syntax in the while condition.