Lecture 2
Object Oriented Data Types quiz
Answer each question to see whether you got it right and why. Your answers are saved in this browser.
Which statement describes an algorithm?
An algorithm is a sequence of steps. How data is organized is a data structure.
An ADT specifies exactly how its data is stored in memory.
An ADT defines what operations exist, not how they are implemented. A stack ADT can use an array or linked nodes.
Which is an ADT operation?
pushdescribes what a stack does. The others are implementation or machine details.Start with an empty stack and run
push(3),push(7),pop(). What value doespop()return?The stack is LIFO: after
push(3),push(7)it holds[3, 7], andpop()removes the last pushed value, 7.Which structure removes items in first-in, first-out order?
A queue is FIFO. A stack is LIFO.
What is the final value of
maximum?int a[] = {3, 1, 6, 4}; int maximum = a[0]; for (int i = 1; i < 4; ++i) { if (a[i] > maximum) maximum = a[i]; } std::cout << maximum;maximumstarts at 3, stays 3 after reading 1, becomes 6, then stays 6 after reading 4.A program compiles but prints 11 instead of 10. What kind of error is this?
It built and ran but produced wrong output, which is a logical error.
Which expression allocates an
intduring execution?newcreates an object with dynamic storage. The others are ordinary automatic or constant variables.The size of
int scores[5];can be changed while the program runs.A fixed-size array's size is set at declaration and cannot change. Use dynamic allocation (or
std::vector) when the size is only known at runtime.With
int a[3] = {7, 4, 9};, what isa[1]?Index 0 is the first element, so index 1 is the second, which is 4.
What does this print?
int a[4] = {2, 3, 5, 7}; std::cout << a[0] + a[3];a[0]is 2 anda[3](the last element) is 7, so the sum is 9.What does this print?
int a[4] = {5, 8, 3, 1}; a[2] = a[0] + a[1]; std::cout << a[2];a[2]is overwritten with5 + 8 = 13.Inside
int f(const int a[], int n), what doesconstforbid?conststops you modifying any element througha. Reading elements and looping are fine.A function receiving
int a[]can tell how many elements the caller's array has.The array parameter is adjusted to a pointer, so the length is lost. Pass it as a separate argument.
What does this print?
int a[2][3] = {{1, 2, 3}, {4, 5, 6}}; std::cout << a[1][0];Indexing is
[row][column]. Row 1 is{4, 5, 6}and column 0 is 4.Which is a valid parameter for
int a[2][3]?The column extent must be given so the compiler can compute row-major offsets. Only the first dimension may be omitted.
With
int x = 5;, what does&xmean?In an expression,
&is the address-of operator.What does this print?
int a = 4; int* p = &a; *p = *p + 3; std::cout << a;*pisa, so the assignment stores4 + 3 = 7intoa.In
*p = 20;the*means "pointer to".In an expression
*dereferences. "Pointer to" is its role only in a declaration likeint* p;.Which memory must eventually be released by you?
Memory from
newhas dynamic storage duration and lives until youdeleteit. Locals are cleaned up automatically.What does this print?
int* p = new int(8); std::cout << *p; delete p; p = nullptr;The value is read before
delete, so it prints 8. Afterdelete p; p = nullptr;the pointer must not be dereferenced.After
delete p;(and no reassignment), isstd::cout << *p;valid?The object is gone, so reading it is a use-after-free. Note that
pis still non-null unless you set it tonullptr, so a non-null check does not save you.After moving the
coutbeforedelete z, what does this print?int* h = new int(9); int* z = new int(5); *z = *h + *z; std::cout << *z; delete z; delete h;*zbecomes9 + 5 = 14. Printing beforedelete zavoids the use-after-free.Which statement correctly releases
int* p = new int[3];?An array from
new[]must be released withdelete[].delete p;andfree(p);are wrong, andp = nullptr;leaks the memory.Which allocates
nintegers at runtime?new int[n]is the standard way. A variable-lengthint a[n];is not standard C++.What does this print?
int* a = new int[3]{1, 2, 3}; int sum = 0; for (int i = 0; i < 3; ++i) sum += a[i]; std::cout << sum; delete[] a;1 + 2 + 3 = 6, and
delete[]correctly releases the array.Which step must come before
delete[]of the old array when resizing?Allocate the larger block, copy the elements, then delete the old one. Deleting first destroys the data.
void extendArray(int* a, int size)(no references) can resize the caller's array and update the caller's pointer.Parameters are copies. Assigning
a = bwould only change the local pointer, leaving the caller with a dangling pointer to the deleted block. The function needsint*& aandint& size.After
int* middle = a + 2;and then a resize that deletes the old block and repointsa, what is*middle?middlestill points into the deleted block. Recompute it from the newa(middle = a + 2;) or use an index.What does this print?
int* a = new int[4]{2, 4, 6, 8}; int* middle = a + 2; int* smaller = new int[3]; for (int i = 0; i < 3; ++i) smaller[i] = a[i]; delete[] a; a = smaller; middle = a + 2; std::cout << *middle; delete[] a;The first three values
{2, 4, 6}are copied,middleis refreshed to index 2 of the new block, and that element is 6.What is wrong with this loop when copying into
int* smaller = new int[3];?for (int i = 0; i <= 3; ++i) smaller[i] = a[i];i <= 3runs for i = 0, 1, 2, 3, but valid indices are 0 to 2. Usei < 3.With
capacity == 0, what capacity does this give?int next = capacity == 0 ? 1 : capacity * 2; std::cout << next;Doubling 0 would stay 0, so the empty case is handled separately and grows to 1.
A full array
{8, 1, 6}(size 3, capacity 3) is grown with doubling, then 4 is appended. What are the size, capacity and elements?Capacity doubles to 6, the three elements are copied in order, and the append makes size 4.
If
scoresandcountinScoreLogareprivate, code outside the class can still modifycountdirectly.Private members are only accessible inside the class. Outside code must go through
addandremoveLast, which keepcountvalid.A
ScoreLoghas capacity 4 and starts empty.add(6),add(3),add(8),add(5),add(9)are called. How many of these calls returntrue?The first four fill the log to
[6, 3, 8, 5]. The fifth findscount == 4and returnsfalse, leaving the log unchanged.The log is
[6, 3, 8, 5]. You callremoveLast(r)twice, thenadd(7). What does it hold, and what was the lastrbefore the add?Removals return 5, then 8 (last-in, first-out), leaving
[6, 3]. Adding 7 gives[6, 3, 7].If
removeLast(r)is called on an emptyScoreLog, it returnsfalseandrkeeps its old value.A failed operation leaves the object and any output argument unchanged, which is why the empty check comes before
removed = ....What does this print?
Pair<int> p(8, 3); p.swapValues(); std::cout << p.larger();After the swap the pair is
(3, 8), andlarger()is still 8. Swapping changes order, not the maximum.In
T larger() const { return first < second ? second : first; }, which is returned whenfirst == second?first < secondis false on a tie, so the expression returnsfirst.void swapValues()could be declaredconstbecause it only uses member variables.A
constmember must not modify the object.swapValues()assigns tofirstandsecond, so it cannot beconst.What does
bestRowreturn? (It keeps the first row on ties and uses strict>.)int a[3][3] = {{-2, 4, 1}, {5, -4, 2}, {0, 1, 1}}; std::cout << bestRow(a, 3);The row sums are 3, 3 and 2. Rows 0 and 1 tie, and strict
>keeps index 0.What does
bestRow(a, 0)return, given the sheet's version that starts withbest = -1?The loop never runs, so
bestkeeps its initial value-1, which means no rows.