1/35
What does &x mean, and what does p mean?
&x gets x's address; p accesses the value at p's address.
What is the relationship between arr[i] and pointer arithmetic?
arr[i] is equivalent to *(arr + i).
Which index accesses the fourth element of an array?
Index 3. C++ arrays start at index 0.
Can a stack pointer point to a heap object?
Yes. The pointer and the object it points to can be in different memory regions.
How do you dynamically allocate an integer array of size n?
int* arr = new int[n];
How do you release an array allocated with new[]?
delete[] arr;
How do you dynamically allocate and release one integer?
int* p = new int; then delete p;
What is a memory leak?
Allocated memory becomes unreachable without being released.
What is a dangling pointer?
A pointer that refers to memory that is no longer valid.
When is a local object's destructor automatically called?
When the object goes out of scope.
When is a dynamically allocated object's destructor called?
When its memory is released using delete.
What is a shallow copy of an object containing an owning pointer?
It copies the pointer address, not the dynamically allocated data.
What are three risks of shallow copying owned memory?
Shared modifications, dangling pointers, and double deletion.
What is a deep copy?
A copy that allocates separate memory and duplicates the original data.
What must a deep-copy constructor do before copying array elements?
Allocate a separate destination array of the required size.
What are the essential steps of deep-copy assignment?
Check self-assignment, release old memory, allocate new memory, copy elements, return *this.
Why check this != &objToCopy in operator=?
To prevent self-assignment from destroying the object's own data.
What are the three functions in the Rule of Three?
Destructor, copy constructor, and copy assignment operator.
What does unique_ptr provide, and what does std::move do?
Exclusive ownership; std::move transfers ownership to another unique_ptr.
When is a shared_ptr-managed object destroyed?
When its last shared_ptr owner releases it.
What header is required for file input/output?
Declare an input file stream named inFS.
ifstream inFS;
Open data.txt for reading using inFS.
inFS.open("data.txt");
How do you check whether inFS successfully opened a file?
inFS.is_open()
How do you close an input file stream?
inFS.close();
Declare an output file stream named outFS.
ofstream outFS;
Write the integer total to an output file stream.
outFS << total;
What does eof() return?
True if the previous stream operation reached end-of-file.
What does fail() return?
True if the previous stream operation failed.
What does good() return?
True if no stream errors have occurred.
How do you reset an istringstream's error flags?
inSS.clear();
How do you replace an istringstream's contents with line?
inSS.str(line);
What does while (inSS >> value) check?
Whether extraction succeeded; stops at EOF or invalid input.
Declare an output string stream named outSS.
ostringstream outSS;
Append a name and age to an output string stream.
outSS << name << " " << age;
Retrieve the string stored in outSS.
string result = outSS.str();