Challenge - 5 Problems
Character Arrays Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of character array initialization
What is the output of this C++ code snippet?
C++
#include <iostream> int main() { char arr[] = {'H', 'e', 'l', 'l', 'o'}; std::cout << arr << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Think about how C++ treats character arrays without a null terminator when printing.
✗ Incorrect
The character array is not null-terminated, so printing it as a C-string leads to undefined behavior and likely garbage output.
❓ Predict Output
intermediate2:00remaining
Output of null-terminated character array
What will this C++ program print?
C++
#include <iostream> int main() { char arr[] = "Hello"; std::cout << arr << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Remember how string literals are stored in character arrays.
✗ Incorrect
The string literal "Hello" includes a null terminator automatically, so printing arr outputs Hello correctly.
🔧 Debug
advanced2:00remaining
Identify the error in character array usage
What error will this code produce when compiled or run?
C++
#include <iostream> int main() { char arr[5] = "Hello"; std::cout << arr << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Check the size of the array vs the string literal length including null terminator.
✗ Incorrect
The string "Hello" requires 6 characters including the null terminator, but the array size is only 5, causing a compilation error.
❓ Predict Output
advanced2:00remaining
Output after modifying character array
What is the output of this program?
C++
#include <iostream> int main() { char arr[] = "Test"; arr[2] = 'x'; std::cout << arr << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Look carefully at which character is changed and the original string.
✗ Incorrect
The original string is "Test". Changing arr[2] (the third character) from 's' to 'x' results in "Text".
🧠 Conceptual
expert2:00remaining
Size of character array with null terminator
Given the declaration
char arr[] = "Code";, what is the size of arr in bytes?Attempts:
2 left
💡 Hint
Remember that string literals include a null terminator at the end.
✗ Incorrect
The string "Code" has 4 characters plus 1 null terminator, so the array size is 5 bytes.