1/30
Looks like no tags are added yet.
Name | Mastery | Learn | Test | Matching | Spaced | Call with Kai | Chat |
|---|
No analytics yet
Send a link to your students to track their progress
What is an array in C++ and how are its elements stored in physical memory?
An array is a collection of elements of the exact same data type stored at contiguous (neighboring) memory locations. Arrays allow you to store multiple values under a single variable name.
What is the general syntax for declaring a fixed-size (static) array in C++?
data_type array_name[array_size]; \n Example: int numbers[5]; declares an array of 5 integers on the stack.
What are the two memory types of arrays in C++ (Static vs. Dynamic), and how is each declared?
Static Array (Stack): Size must be fixed at compile time. Syntax: int numbers[5]; \n Dynamic Array (Heap): Allocated at runtime using pointers. Syntax: int* ptr = new int[5];
How are array elements indexed and accessed in C++? What is the index of the first and last element?
Elements use index positions starting from 0. Syntax: array_name[index]. \n First element: array_name[0]. \n Last element (size N): array_name[N - 1].
What are the 5 common operations performed on arrays in C++?
What is a Multidimensional Array in C++, what is its syntax, and how is it laid out in RAM?
An array of arrays (e.g., matrix of rows/columns). Syntax: data_type name[size1][size2]; \n Layout: Contiguous in row-major order.
Why is memory management crucial when creating dynamic arrays with pointers, and what operator must be used to free them?
Dynamic arrays live on the heap and do not deallocate automatically. You must release them using delete[] array_name; to prevent memory leaks.
Which of the following correctly declares an array in C++? A) int arr[5]; B) array int arr[5]; C) int arr; D) int arr[5] = new int[5];
A) int arr[5]; \n Explanation: C++ declaration syntax is type name[size];.
How can you dynamically allocate an array of integers with 10 elements in C++?
int* arr = new int[10]; \n Explanation: new int[10] requests heap memory for 10 integers and returns the address, captured by an integer pointer.
What is the correct way to free dynamically allocated memory for an array? A) free(arr); B) delete arr; C) delete[] arr; D) free[] arr;
C) delete[] arr; \n Explanation: Memory allocated with new[] MUST be freed with delete[].
What will be the output of the following code snippet? \n int arr[5] = {10, 20, 30, 40, 50}; cout << arr[0] + arr[3];
50 (10 + 40)
What is the output of the following C++ code? \n int arr[5] = {1, 2, 3}; cout << arr[3] << " " << arr[4];
0 0 \n Explanation: When an array is partially initialized, C++ automatically zero-initializes all remaining uninitialized elements.
What does this code output, and what is numbers[2] after execution? \n int numbers[3] = {10, 20, 30}; cout << numbers[1]; numbers[2] = 50;
Output: 20 (numbers[1]). \n numbers[2] value: Updated from 30 to 50.
What element is accessed and what is its value? \n int matrix[2][2] = {{1, 2}, {3, 4}}; cout << matrix[1][0];
3 \n Explanation: matrix[1][0] accesses row index 1 and column index 0, which holds 3.
What is the main purpose of files in C++, and what are the 3 steps to work with files?
Main Purpose: To retain data across multiple program runs (persistence). \n 3 Steps: 1. Open the file, 2. Use/Process the file, 3. Close the file.
Which header files/stream types are required to work with files in C++? Define ifstream, ofstream, and fstream.
Header: #include \n ifstream: Read input from a file. \n ofstream: Write output to a file. \n fstream: Read and write.
How do you open an input file and check whether the file exists before reading?
ifstream inputFile("file.txt"); if (!inputFile) { cout << "Error"; } else { … } \n Testing if (!inputFile) is mandatory because reading from a missing file fails.
How do you write to an output file, and why do we generally not need to check if an output file exists before writing?
ofstream outputFile("file.txt"); outputFile << "Text"; \n We don't need to check because opening an ofstream automatically creates the file if it doesn't exist.
What is a vector (std::vector) in C++, which header is required, and how does it differ from a static array?
An STL dynamic contiguous container. Header: #include . \n Difference: Unlike static arrays with fixed compile-time sizes, vectors automatically expand or contract.
What is the difference between v[i] and v.at(i) when accessing elements of a vector?
v[i]: Fast, direct access without bounds checking (undefined behavior if out of bounds). \n v.at(i): Performs bounds checking and throws an out_of_range exception if invalid.
Why should large vectors always be passed to functions as const vector&?
Passing by value creates a full duplicate copy, wasting memory. const vector& passes a lightweight reference without copying while preventing modification.
What is the output of the following vector code? \n vector v; v.push_back(10); v.push_back(20); v.push_back(30); v.pop_back(); v[0] = 50; cout << v.size() << " " << v.at(0) << " " << v.back();
2 50 20
What are the specific capabilities and directional differences between ifstream, ofstream, and fstream?
ifstream: File -> Program (Read-only). \n ofstream: Program -> File (Write-only). \n fstream: Bidirectional (Read and Write).
What are the two syntactically valid ways to open an ifstream in C++?
If file.txt contains "Computer Science 1337", what will string text; inputFile >> text; cout << text; print?
Computer \n Explanation: The standard extraction operator >> stops reading at the first whitespace. Use getline to read whole lines.
Compare these three declarations: int numbers[5];, int numbers[5] = {1, 2, 3, 4, 5};, int* ptr = new int[5];.
Why is insertion "not directly possible in static arrays," and how does deletion work?
Insertion: Static arrays have fixed capacity; you must manually shift elements. \n Deletion: Simulated by shifting remaining elements left and decrementing the logical size counter.
What is Quick Sort in relation to Bubble Sort?
Bubble Sort: Simple O(n^2) algorithm suitable for small/nearly sorted data. \n Quick Sort: Advanced divide-and-conquer algorithm with average O(n log n) time complexity.
For int matrix[3][3]; (each int 4 bytes): How many total elements, bytes, and valid indices?
Total elements: 9. \n Total bytes: 36 bytes of contiguous memory. \n Valid indices: Rows 0-2, Columns 0-2.
What is the difference between v.size() and v.capacity() for a std::vector?
v.size() is the number of elements currently stored. v.capacity() is the total elements it can hold before reallocating a larger heap block.
What is the output of this range-based loop? \n vector v = {10, 20, 30}; for (int x : v) { x += 5; } cout << v[0] << " " << v[1] << " " << v[2];
10 20 30 \n Explanation: for (int x : v) creates a copy. Use for (int& x : v) to modify in place.