0
0
C++programming~20 mins

Character arrays in C++ - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Character Arrays Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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;
}
AH
BCompilation error
CHello
DRandom characters or garbage output
Attempts:
2 left
💡 Hint
Think about how C++ treats character arrays without a null terminator when printing.
Predict Output
intermediate
2: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;
}
ACompilation error
BH
CHello
DHello\0
Attempts:
2 left
💡 Hint
Remember how string literals are stored in character arrays.
🔧 Debug
advanced
2: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;
}
APrints Hell
BCompilation error: initializer string too long
CPrints Hello
DRuntime error: buffer overflow
Attempts:
2 left
💡 Hint
Check the size of the array vs the string literal length including null terminator.
Predict Output
advanced
2: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;
}
AText
BTexst
CTexs
DTex
Attempts:
2 left
💡 Hint
Look carefully at which character is changed and the original string.
🧠 Conceptual
expert
2:00remaining
Size of character array with null terminator
Given the declaration char arr[] = "Code";, what is the size of arr in bytes?
A5
B4
C6
DUndefined
Attempts:
2 left
💡 Hint
Remember that string literals include a null terminator at the end.