Section 1 (Arrays) + Section 4(Files)+Section 5(Vectors)

0.0(0)
Studied by 0 people
call kaiCall Kai
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
GameKnowt Play
Card Sorting

1/30

encourage image

There's no tags or description

Looks like no tags are added yet.

Last updated 3:41 AM on 10/2/26
Name
Mastery
Learn
Test
Matching
Spaced
Call with Kai
Chat

No analytics yet

Send a link to your students to track their progress

31 Terms

1
New cards

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.

2
New cards

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.

3
New cards

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];

4
New cards

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].

5
New cards

What are the 5 common operations performed on arrays in C++?

  1. Traversing: Looping through each element. \n 2. Insertion: Adding an element. \n 3. Deletion: Removing an element. \n 4. Searching: Locating a value. \n 5. Sorting: Rearranging elements.
6
New cards

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.

7
New cards

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.

8
New cards

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];.

9
New cards

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.

10
New cards

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[].

11
New cards

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)

12
New cards

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.

13
New cards

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.

14
New cards

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.

15
New cards

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.

16
New cards

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.

17
New cards

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.

18
New cards

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.

19
New cards

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.

20
New cards

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.

21
New cards

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.

22
New cards

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

23
New cards

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).

24
New cards

What are the two syntactically valid ways to open an ifstream in C++?

  1. ifstream inputFile; inputFile.open("file.txt"); \n 2. ifstream inputFile("file.txt");
25
New cards

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.

26
New cards

Compare these three declarations: int numbers[5];, int numbers[5] = {1, 2, 3, 4, 5};, int* ptr = new int[5];.

  1. Stack memory, garbage values. \n 2. Stack memory, explicitly initialized elements. \n 3. Heap memory, ptr is a stack variable holding the address of the first heap element.
27
New cards

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.

28
New cards

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.

29
New cards

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.

30
New cards

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.

31
New cards

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.