C++ Arrays & Strings Quiz
40 comprehensive questions on C++ arrays and strings, covering fixed-size arrays, C-style strings, std::string fundamentals, string operations, and array iteration patterns — with 20 code examples to master memory management and string handling.
Question 1
When declaring a fixed-size array in C++, what fundamental constraint must be known at compile time?
#include <iostream>
int main() {
// Fixed-size array - size must be compile-time constant
int numbers[5] = {1, 2, 3, 4, 5};
for(int i = 0; i < 5; ++i) {
std::cout << numbers[i] << " ";
}
return 0;
}Question 2
In a scenario where you need to store a collection of integers that won't change size during program execution, what array type provides the most efficient memory usage and access performance?
Question 3
When working with C-style strings that contain user input, what critical safety issue must be addressed to prevent buffer overflow vulnerabilities?
#include <iostream>
#include <cstring>
int main() {
char buffer[10];
std::cout << "Enter text: ";
std::cin >> buffer; // Dangerous - no bounds checking
// What if user enters more than 9 characters?
std::cout << "You entered: " << buffer << std::endl;
return 0;
}Question 4
What fundamental difference exists between C-style strings and std::string when it comes to memory management and safety?
Question 5
When initializing a fixed-size array with fewer values than its declared size, what happens to the remaining elements?
#include <iostream>
int main() {
int arr[5] = {1, 2, 3}; // Only 3 values provided
for(int i = 0; i < 5; ++i) {
std::cout << arr[i] << " "; // What are arr[3] and arr[4]?
}
return 0;
}Question 6
In a performance-critical application that processes text data from external sources, what string type provides the best balance between safety and the ability to work with C APIs?
Question 7
When concatenating multiple strings in a loop where the total size is unknown beforehand, what approach avoids repeated memory allocations and copying?
#include <iostream>
#include <string>
#include <vector>
int main() {
std::vector<std::string> words = {"Hello", " ", "World", "!"};
std::string result;
// Inefficient: reallocates and copies on each append
for(const auto& word : words) {
result += word;
}
std::cout << result << std::endl;
return 0;
}Question 8
What happens when you attempt to access an array element beyond its bounds in C++?
#include <iostream>
int main() {
int arr[3] = {1, 2, 3};
// This is undefined behavior
std::cout << arr[5] << std::endl; // Accessing out of bounds
return 0;
}Question 9
When comparing two std::string objects for equality, what factors determine whether they are considered equal?
#include <iostream>
#include <string>
int main() {
std::string s1 = "Hello";
std::string s2 = "Hello";
if(s1 == s2) {
std::cout << "Strings are equal" << std::endl;
}
return 0;
}Question 10
In a situation where you need to iterate through a fixed-size array while performing complex operations on each element, what iteration pattern provides the most readable and maintainable code?
#include <iostream>
int main() {
int numbers[5] = {1, 2, 3, 4, 5};
// Range-based for loop - modern and safe
for(int num : numbers) {
std::cout << num * 2 << " ";
}
return 0;
}Question 11
When working with C-style strings that may contain embedded null characters, what critical limitation affects their usability?
Question 12
What is the most efficient way to initialize a large fixed-size array with all elements set to the same value?
#include <iostream>
#include <algorithm>
int main() {
const int SIZE = 1000;
int arr[SIZE];
// Efficient initialization with std::fill
std::fill(std::begin(arr), std::end(arr), 42);
std::cout << "First element: " << arr[0] << std::endl;
std::cout << "Last element: " << arr[SIZE-1] << std::endl;
return 0;
}Question 13
When passing a fixed-size array to a function, what happens to the array's size information and how can it be preserved?
#include <iostream>
// Array decays to pointer, size information lost
void printArray(int arr[], int size) {
for(int i = 0; i < size; ++i) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
}
// Template preserves size information
// template<size_t N>
// void printArray(int (&arr)[N]) {
// for(int num : arr) {
// std::cout << num << " ";
// }
// }
int main() {
int numbers[5] = {1, 2, 3, 4, 5};
printArray(numbers, 5); // Must pass size separately
return 0;
}Question 14
In a program that needs to build a string incrementally from various sources, what std::string method provides the most efficient way to append data without creating temporary objects?
Question 15
What is the key difference between array initialization syntax and assignment syntax in C++?
#include <iostream>
int main() {
int arr1[3] = {1, 2, 3}; // Initialization
int arr2[3];
// arr2 = {1, 2, 3}; // This is invalid!
// Must use assignment to individual elements or std::copy
arr2[0] = 1; arr2[1] = 2; arr2[2] = 3;
return 0;
}Question 16
When comparing C-style strings using strcmp(), what does the return value indicate about the relationship between the strings?
#include <iostream>
#include <cstring>
int main() {
const char* str1 = "apple";
const char* str2 = "banana";
int result = strcmp(str1, str2);
if(result < 0) {
std::cout << str1 << " comes before " << str2 << std::endl;
} else if(result > 0) {
std::cout << str1 << " comes after " << str2 << std::endl;
} else {
std::cout << "Strings are equal" << std::endl;
}
return 0;
}Question 17
In a performance-critical loop that processes array elements, what iteration method provides the best combination of safety and performance?
Question 18
What happens when you copy a std::string object, and how does this differ from copying a C-style string?
#include <iostream>
#include <string>
int main() {
std::string s1 = "Hello World";
std::string s2 = s1; // Deep copy - safe and independent
s2[0] = 'h'; // Modifies only s2
std::cout << "s1: " << s1 << std::endl;
std::cout << "s2: " << s2 << std::endl;
return 0;
}Question 19
When working with multidimensional fixed-size arrays, what syntax correctly declares and initializes a 2D array?
#include <iostream>
int main() {
// 2D array: 3 rows, 4 columns
int matrix[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
// Access elements
std::cout << matrix[1][2] << std::endl; // 7
return 0;
}Question 20
In a function that needs to modify a string passed by reference while maintaining the original, what parameter type provides the most efficient and safe approach?
Question 21
When implementing a function that searches for a substring within a larger string, what std::string method provides the most straightforward and safe implementation?
#include <iostream>
#include <string>
int main() {
std::string text = "Hello, World!";
std::string pattern = "World";
size_t found = text.find(pattern);
if(found != std::string::npos) {
std::cout << "Found at position: " << found << std::endl;
} else {
std::cout << "Not found" << std::endl;
}
return 0;
}Question 22
What is the most significant limitation of using fixed-size arrays for storing collections of unknown size at runtime?
Question 23
When concatenating strings where performance is critical and the total size is known, what approach minimizes memory allocations?
#include <iostream>
#include <string>
int main() {
std::string part1 = "Hello";
std::string part2 = " ";
std::string part3 = "World";
// Pre-calculate total size
size_t totalSize = part1.size() + part2.size() + part3.size();
std::string result;
result.reserve(totalSize); // Avoid reallocations
result += part1;
result += part2;
result += part3;
std::cout << result << std::endl;
return 0;
}Question 24
In a program that processes text files with varying line lengths, what string type handles dynamic content most safely and efficiently?
Question 25
What is the correct way to determine the length of a C-style string, and why is this important?
#include <iostream>
#include <cstring>
int main() {
const char* str = "Hello\0World"; // Embedded null
std::cout << "strlen: " << strlen(str) << std::endl; // 5
std::cout << "sizeof: " << sizeof(str) << std::endl; // Pointer size
return 0;
}Question 26
When iterating through an array using indices, what common mistake can lead to out-of-bounds access and how can it be avoided?
#include <iostream>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
// Correct: use < size, not <= size
for(size_t i = 0; i < 5; ++i) {
std::cout << arr[i] << " ";
}
// Wrong: i <= 5 would access arr[5] which is out of bounds
return 0;
}Question 27
What is the most efficient way to check if a std::string is empty, and why does this matter for performance?
#include <iostream>
#include <string>
int main() {
std::string s = "Hello";
// Efficient: O(1) check
if(s.empty()) {
std::cout << "Empty" << std::endl;
}
// Less efficient: O(n) check
// if(s.size() == 0) or if(s == "")
return 0;
}Question 28
When working with arrays of strings, what iteration pattern provides the most readable code while maintaining type safety?
#include <iostream>
#include <string>
#include <vector>
int main() {
std::vector<std::string> names = {"Alice", "Bob", "Charlie"};
// Range-based for loop - clean and safe
for(const std::string& name : names) {
std::cout << name << " ";
}
return 0;
}Question 29
What happens when you assign one C-style string to another using simple assignment, and why is this problematic?
#include <iostream>
#include <cstring>
int main() {
const char* str1 = "Hello";
const char* str2;
str2 = str1; // Only copies pointer, not content!
std::cout << str2 << std::endl; // Works by accident
// But if str1 goes out of scope or is modified...
return 0;
}Question 30
In a function that needs to extract a substring from a std::string, what method provides the most flexible and safe substring extraction?
#include <iostream>
#include <string>
int main() {
std::string text = "Hello, World!";
// Extract "World" (position 7, length 5)
std::string substring = text.substr(7, 5);
std::cout << substring << std::endl;
return 0;
}Question 31
When declaring a fixed-size array as a function parameter, what syntax correctly accepts any size array of that type?
#include <iostream>
// Function accepting any size int array
void printArray(int arr[], size_t size) {
for(size_t i = 0; i < size; ++i) {
std::cout << arr[i] << " ";
}
std::cout << std::endl;
}
// Template version that preserves size
// template<size_t N>
// void printArray(int (&arr)[N]) {
// for(int num : arr) {
// std::cout << num << " ";
// }
// }
int main() {
int arr1[3] = {1, 2, 3};
int arr2[5] = {1, 2, 3, 4, 5};
printArray(arr1, 3);
printArray(arr2, 5);
return 0;
}Question 32
What is the most significant advantage of std::string over C-style strings for international text processing?
Question 33
When implementing a function that reverses the contents of a fixed-size array in-place, what algorithm provides the most efficient implementation?
#include <iostream>
#include <algorithm>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
// Use std::reverse for efficiency and correctness
std::reverse(std::begin(arr), std::end(arr));
for(int num : arr) {
std::cout << num << " ";
}
return 0;
}Question 34
In a program that frequently searches for characters within strings, what std::string method provides the most efficient character finding?
#include <iostream>
#include <string>
int main() {
std::string text = "Hello, World!";
// Find first space
size_t spacePos = text.find(' ');
if(spacePos != std::string::npos) {
std::cout << "Space found at: " << spacePos << std::endl;
}
return 0;
}Question 35
What is the key safety advantage of using std::string::at() instead of operator[] for array-like access?
#include <iostream>
#include <string>
int main() {
std::string s = "Hello";
try {
char c = s.at(10); // Throws exception for out of bounds
} catch(const std::out_of_range& e) {
std::cout << "Out of bounds access detected" << std::endl;
}
// s[10] would cause undefined behavior
return 0;
}Question 36
When working with arrays of objects that have expensive copy constructors, what iteration pattern minimizes unnecessary copying?
Question 37
What is the most efficient way to clear a std::string and prepare it for new content?
#include <iostream>
#include <string>
int main() {
std::string s = "Hello World";
s.clear(); // Efficient: keeps capacity, just resets size
// Now s is empty but retains allocated memory
s = "New content";
std::cout << s << std::endl;
return 0;
}Question 38
In a function that processes array elements and may need to exit early based on a condition, what iteration pattern provides the most control?
#include <iostream>
int main() {
int arr[5] = {1, 2, 3, 4, 5};
// Traditional loop allows early exit and complex control flow
for(size_t i = 0; i < 5; ++i) {
if(arr[i] == 3) {
std::cout << "Found 3 at index " << i << std::endl;
break; // Exit early
}
std::cout << arr[i] << " ";
}
return 0;
}Question 39
What is the primary reason C-style strings cannot safely store binary data containing null bytes?
Question 40
When implementing a function that needs to modify individual characters in a string, what std::string method provides the safest indexed access?
#include <iostream>
#include <string>
int main() {
std::string s = "Hello";
// Safe indexed access with bounds checking
if(s.length() > 0) {
s[0] = 'h'; // Valid index
}
// s.at(10) would throw exception
// s[10] would cause undefined behavior
std::cout << s << std::endl;
return 0;
}