Data Structures Lec 4 - 8: Vectors, Lists, Stacks, Queues

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/144

flashcard set

Earn XP

Description and Tags

200 question-and-answer style flashcards covering template classes, vectors, linked lists, stacks, queues, deques, iterators, and related algorithms from the lecture notes.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

145 Terms

1
New cards

What is a C++ template class?

A. Class parameterized by type.

B. A precompiled library.

C. A function pointer.

D. An object instance.

A. Class parameterized by type.

2
New cards

Which keyword syntax begins a template class declaration in C++?

A. class<T>

B. template<typename T>

C. typedef<T>

D. generic<class T>

B. template<typename T>

3
New cards

In the STL, what kind of container is called a sequence?

A. Elements inserted/removed arbitrarily.

B. Key-value pairs organized.

C. Sorted unique elements.

D. Fixed-size memory storage.

A. Elements inserted/removed arbitrarily.

4
New cards

What header file must be included to use std::vector?

A. &amp;lt;list&amp;gt;

B. &amp;lt;array&amp;gt;

C. &amp;lt;vector&amp;gt;

D. &amp;lt;memory&amp;gt;

C.

5
New cards

What placeholder represents the actual data type in a template class declaration?

A. Type identifier.

B. Template parameter T.

C. Argument type.

D. Generic placeholder K.

B. Template parameter T.

6
New cards

What header file must be included to use std::vector?

A. &lt;list&gt;

B. &lt;array&gt;

C. &lt;vector&gt;

D. &lt;memory&gt;

C. <vector>

7
New cards

Give an example of instantiating a template class called some_container with Person objects.

A. some_container<Person> example;

B. some_container example;

C. some_container(Person) example;

D. some_container<object> example;

A. some_container<Person> example;

8
New cards

Name one key advantage a vector has over a built-in array?

A. Faster access by index.

B. Automatic size growth.

C. Stores different types.

D. Lower memory usage.

B. Automatic size growth.

9
New cards

What operation adds an element to the end of a vector?

A. add()

B. insert_end()

C. push_front()

D. push_back()

D. push\_back

10
New cards

Which vector member function returns the current number of stored elements?

A. capacity()

B. length()

C. size()

D. count()

C. size()

11
New cards

Which vector property indicates how many elements it can store before reallocating?

A. size

B. length

C. capacity

D. limit

C. capacity

12
New cards

Which member function reserves storage space for future vector growth?

A. allocate()

B. resize()

C. reserve()

D. expand()

C. reserve()

13
New cards

Which vector member function inserts an element at a specified position?

A. add_at()

B. place()

C. insert()

D. append()

C. insert()

14
New cards

Which vector member function removes an element at a specified position?

A. remove()

B. clear_at()

C. delete_at()

D. erase()

D. erase()

15
New cards

What is the difference between operator[] and at() when accessing vector elements?

A. at() is faster.

B. operator[] checks range.

C. at() throws exception.

D. operator[] is safer.

C. at() throws exception.

16
New cards

What is the average time complexity of vector::push\\_back?

A. O(n)

B. O(log n)

C. O(1)

D. O(1) amortized.

D. O(1) amortized.

17
New cards

How does a vector typically grow when it runs out of capacity?

A. Adds one element.

B. Doubles capacity.

C. Increases by 50%.

D. Creates a new vector.

B. Doubles capacity.

18
New cards

Which vector member function creates a deep copy of another vector during assignment?

A. copy()

B. clone()

C. Assignment operator (operator=).

D. duplicate()

C. Assignment operator (operator=).

19
New cards

What three special functions make up C++’s Rule of Three?

A. Constructor, destructor, method.

B. Copy constructor, assignment, destructor.

C. Push, pop, top.

D. Begin, end, size.

B. Copy constructor, assignment, destructor.

20
New cards

What does a vector’s destructor do?

A. Clears all elements.

B. Deletes dynamic array memory.

\n\n C. Shrinks capacity.

D. Resets size to zero.

B. Deletes dynamic array memory.

21
New cards

What data field in a custom vector tracks the physical size of the underlying array?

A. logical_size

B. current_capacity

C. element_count

D. allocated_size

B. current\_capacity

22
New cards

What data field in a custom vector tracks the logical number of items?

A. max_capacity

B. num_items

C. array_size

D. current_elements

B. num\_items

23
New cards

Why is size\_t used for vector indexes?

A. Requires less memory.

B. Faster arithmetic.

C. Unsigned, non-negative index.

D. Universal integer type.

C. Unsigned, non-negative index.

24
New cards

What is the time complexity of inserting an element in the middle of a vector?

A. O(1)

B. O(log n)

C. O(n)

D. O(n^2)

C. O(n)

25
New cards

What is a node in a linked list?

A. A data item.

B. A pointer to data.

C. Data and pointers.

D. A list segment.

C. Data and pointers.

26
New cards

In a singly linked list node, what does the field next store?

A. Pointer to previous node.

B. Data value.

C. Pointer to next node.

D. Node index.

C. Pointer to next node.

27
New cards

Why are insertions and removals after a referenced node in a singly linked list O(1)?

A. Cache hit rates are high.

B. Pointer assignments, no shifting.

C. Directly addressed.

D. Small data size.

B. Pointer assignments, no shifting.

28
New cards

Why is inserting at an arbitrary position in a vector O(n)?

A. Memory must be reallocated.

B. Elements must shift.

C. Accessing index takes time.

D. Sorting is required.

B. Elements must shift.

29
New cards

When building a list by adding each new node to the front, which pointer must be updated?

A. Tail pointer.

B. Current pointer.

C. Head pointer.

D. Last pointer.

C. Head pointer.

30
New cards

To insert a node after a referenced node in a singly list, how many pointer changes are needed?

A. One.

B. Two.

C. Three.

D. Four.

B. Two.

31
New cards

To remove the node after a referenced node in a singly list, what must you do first?

A. Update the next pointer.

B. Save node pointer.

C. Set node to NULL.

D. Adjust head pointer.

B. Save node pointer.

32
New cards

Why can’t singly linked lists be traversed backward without additional data?

A. No previous link.

B. Only forward link.

C. Data stored elsewhere.

D. Not designed for it.

B. Only forward link.

33
New cards

What is the average complexity of inserting a node before a given node in a singly list?

A. O(1)

B. O(\log n)

C. O(n)

D. O(n \text{ log n})

C. O(n) for predecessor.

34
New cards

What extra pointer does a doubly linked list node contain beyond next?

A. first

B. prev

C. parent

D. back

B. prev

35
New cards

How long does it take to insert a node before a known node in a doubly linked list?

A. O(n)

B. O(log n)

C. O(1)

D. O(n^2)

C. O(1)

36
New cards

What is the primary disadvantage of doubly linked lists compared to singly lists?

A. Slower traversal.

B. More complex deletion.

C. More memory, overhead.

D. Cannot be reversed.

C. More memory, overhead.

37
New cards

What simple modification turns a doubly linked list into a circular list?

A. Set head to tail.

B. Link the tail’s next to head.

C. Make all nodes self-referential.

D. Add a counter.

B. Tail links to head.

38
New cards

What major risk comes with circular lists during traversal?

A. Memory leaks.

B. Data corruption.

C. Infinite loop.

D. Stack overflow.

C. Infinite loop.

39
New cards

How can you detect if a list is circular given a pointer to any node?

A. Check if head is NULL.

B. Return to start node.

C. Compare node addresses.

D. Count total nodes.

B. Return to start node.

40
New cards

What is an iterator?

A. A fixed memory address.

B. A movable container position marker.

C. A direct data pointer.

D. A container's size.

B. Movable container position marker.

41
New cards

What does operator\\* on a list iterator return?

A. The iterator itself.

B. A copy of the data.

C. Reference to data.

D. The node address.

C. Reference to data.

42
New cards

Which operator advances a list iterator to the next node?

A. operator--

B. operator+

C. operator++

D. operator-&amp;gt;

C. operator++

43
New cards

What is a const\\_iterator used for?

A. Modifying container elements.

B. Read-only iteration.

C. Backward traversal only.

D. Iterating constant containers.

B. Read-only iteration.

44
New cards

Which list member returns an iterator to the first element?

A. front()

B. begin()

C. start()

D. head()

B. begin()

45
New cards

Which list member returns an iterator that represents the position past the last element?

A. last()

B. end()

C. tail()

D. out()

B. end()

46
New cards

Which list function inserts at the front of a doubly linked list?

A. add_front()

B. insert_first()

C. push_front()

D. prepend()

C. push\_front()

47
New cards

What does list::insert return?

A. The inserted data.

B. Iterator to new node.

C. A boolean indicating success.

D. Number of elements.

B. Iterator to new node.

48
New cards

Which function removes the first node of a list?

A. remove_first()

B. delete_front()

C. pop_front()

D. shift()

C. pop\_front()

49
New cards

Which function removes the last node of a list?

A. remove_last()

B. erase_end()

C. pop_back()

D. cut_end()

C. pop\_back()

50
New cards

What does list::erase(pos) return?

A. Iterator to erased node.

B. Iterator to preceding node.

C. Iterator to following node.

D. void.

C. Iterator to following node.

51
New cards

How does the list copy constructor usually copy another list?

A. Copies memory block.

B. Deep copies pointers.

C. push\_back each element.

D. Swaps contents.

C. push\_back each element.

52
New cards

Why should a list destructor delete nodes directly instead of using pop\_back in a loop?

A. More readable code.

B. Avoid unnecessary traversal.

C. Ensures proper memory access.

D. Better error handling.

B. Avoid unnecessary traversal.

53
New cards

What does the list assignment operator typically do internally?

A. Direct memory copy.

B. Copy-and-swap.

C. Shallow copy.

D. Resizes memory.

B. Copy-and-swap.

54
New cards

What storage policy governs a stack?

A. First-In, First-Out (FIFO).

B. Last-In, First-Out (LIFO).

C. Random Access.

D. Priority-based.

B. Last-In, First-Out (LIFO).

55
New cards

Name the four fundamental public operations of a stack.

A. add(), remove(), get(), is_empty()

B. push(), pop(), peek(), size()

C. empty(), top(), pop(), push()

D. insert(), delete(), front(), back()

C. empty(), top(), pop(), push()

56
New cards

Which stack member returns its logical size?

A. count()

B. length()

C. capacity()

D. size()

D. size()

57
New cards

When implementing a stack with a vector, which vector function is used for push?

A. insert()

B. add()

C. push_back()

D. append()

C. push\_back()

58
New cards

In a linked-list stack, what pointer points to the stack’s top?

A. bottom_of_stack

B. tail

C. top_of_stack (head).

D. current_node

C. topofstack (head).

59
New cards

Which stack-based algorithm can determine whether a string is a palindrome?

A. Recursively compare halves.

B. Use a two-pointer approach.

C. Push/pop for reverse.

D. Store in a hash table.

C. Push/pop for reverse.

60
New cards

How can a stack convert a positive decimal integer to binary?

A. Iterate and concatenate.

B. Push/pop remainders.

C. Convert to hexadecimal first.

D. Use bitwise operations.

B. Push/pop remainders.

61
New cards

Which data structure is most suitable for checking balanced parentheses?

A. A queue.

B. A stack.

C. A linked list.

D. A vector.

B. A stack.

62
New cards

What condition indicates a mismatch when checking parentheses?

A. Stack overflow occurs.

B. Empty or no match.

C. Non-parenthesis character.

D. String is too long.

B. Empty or no match.

63
New cards

How can undo/redo be modeled with stacks?

A. Use a single queue.

B. Two stacks: undo, redo.

C. A doubly linked list.

D. Store actions in an array.

B. Two stacks: undo, redo.

64
New cards

What storage policy governs a queue?

A. Last-In, First-Out (LIFO).

B. First-In, First-Out (FIFO).

C. Random access.

D. Priority-based.

B. First-In, First-Out (FIFO).

65
New cards

Name three basic public operations of a queue.

A. add(), remove(), get()

B. insert(), delete(), peek()

C. enqueue(), dequeue(), peek()

D. push(), pop(), front().

D. push(), pop(), front().

66
New cards

In a linked-list queue, which pointer references the node to be removed next?

A. back_of_queue

B. front_of_queue

C. current_node

D. head

B. frontofqueue

67
New cards

Why is a circular array queue more efficient than a simple array queue for pop?

A. Uses less memory.

B. No element shifting.

C. Easier to implement.

D. Faster initial setup.

B. No element shifting.

68
New cards

Which two indexes are tracked in a circular array queue?

A. start_index, end_index

B. head_index, tail_index

C. front_index, rear_index

D. first, last_element

C. frontindex, rearindex

69
New cards

When a circular queue becomes full, what must be done before inserting?

A. Wait for space.

B. Reallocate larger array.

C. Discard oldest element.

D. Throw an error.

B. Reallocate larger array.

70
New cards

After reallocation of a circular queue, to what does front\_index reset?

A. To original front.

B. To current rear.

C. 0.

D. To capacity - 1.

C. 0

71
New cards

What field in a circular queue tracks the number of stored items?

A. max_size

B. capacity

C. num_items

D. item_count

C. num\_items

72
New cards

What is the advantage of a circular queue’s O(1) push/pop operations?

A. Uses contiguous memory.

B. Reduced memory footprint.

C. No element shifting.

D. Simplified iterator design.

C. No element shifting.

73
New cards

How does a simple priority queue differ from a FIFO queue?

A. Enqueue is faster.

B. Pop removes highest value.

C. It's a LIFO structure.

D. Only stores integers.

B. Pop removes highest value.

74
New cards

What feature distinguishes a deque from both a stack and a queue?

A. Supports random access.

B. Insert/remove both ends.

C. Guaranteed sorted order.

D. Fixed memory size.

B. Insert/remove both ends.

75
New cards

Why does std::deque support random-access iterators while std::queue does not?

A. std::queue is abstract.

B. std::deque uses linked list.

C. Block-based storage.

D. std::queue is for LIFO.

C. Block-based storage.

76
New cards

In a block-based deque implementation, what does offset store?

A. Total memory used.

B. Current block number.

C. Size of each block.

D. Index of first element.

D. Index of first element.

77
New cards

How is operator[] implemented for a block-based deque?

A. Linear scan.

B. Compute block/data index.

C. Direct array access.

D. Hash table lookup.

B. Compute block/data index.

78
New cards

When push\_back causes a deque to exceed capacity, what is typically added?

A. New block.

B. Array is doubled.

C. All elements shifted.

D. Memory is reallocated.

A. New block.

79
New cards

What happens when popfront causes offset to equal BLOCKSIZE?

A. Queue becomes empty.

B. First block deleted.

C. Offset is reset to zero.

D. All remaining elements shift.

B. First block deleted.

80
New cards

Give one real-world example where stacks are used in software?

A. Network packet routing.

B. Web browser history.

C. Compilers: function call stack.

D. Online banking transactions.

C. Compilers: function call stack.

81
New cards

Give one real-world example where queues are used?

A. Word processor spell check.

B. Image pixel manipulation.

C. Print spoolers.

D. Game character movement.

C. Print spoolers.

82
New cards

What string constants are often used in balanced-parentheses code for quick matching?

A. BRACKETS, PARENS

B. OPEN, CLOSE strings.

C. BEGIN, END

D. LEFT, RIGHT

B. OPEN, CLOSE strings.

83
New cards

When a postfix evaluator reads a digit token, what does it do with it?

A. Evaluates immediately.

B. Pushes to operator stack.

C. Push to operand stack.

D. Ignores it.

C. Push to operand stack.

84
New cards

In a postfix expression evaluator, what is done when an operator token is read?

A. Store on operator stack.

B. Pop, apply, push.

C. Print to console.

D. Error out.

B. Pop, apply, push.

85
New cards

Why does a palindrome algorithm need to empty the stack at the end?

A. To free memory.

B. Build reverse string.

C. Verify stack integrity.

D. Clear for next use.

B. Build reverse string.

86
New cards

In the decimal-to-binary algorithm, why are remainders pushed on a stack?

A. To calculate sums.

B. Reverse order for pop.

C. To prevent overflow.

D. For quick retrieval.

B. Reverse order for pop.

87
New cards

What is the top pointer set to after a linked-list stack pop?

A. Old node's address.

B. NULL always.

C. topofstack->next.

D. Previous node's address.

C. topofstack->next.

88
New cards

What should a stack pop do if the stack is empty?

A. Return default value.

B. Do nothing silently.

C. Throw exception.

D. Print warning.

C. Throw exception.

89
New cards

What key responsibility does a destructor have in any class that allocates dynamic memory?

A. Initialize all members.

B. Free allocated memory.

C. Close open files.

D. Perform deep copy.

B. Free allocated memory.

90
New cards

When is the copy constructor automatically invoked?

A. Object created with new.

B. Assigning objects.

C. Pass/return/initialize object.

D. Calling member function.

C. Pass/return/initialize object.

91
New cards

What is a shallow copy?

A. Duplicates all data.

B. Pointers duplicated, data shared.

C. Only basic types copied.

D. Creates a new object by default.

B. Pointers duplicated, data shared.

92
New cards

What is a deep copy?

A. Copies pointer values only.

B. Duplicates all pointers.

C. Independent data copy.

D. Copies references to data.

C. Independent data copy.

93
New cards

Why does list::erase return an iterator to the following node?

A. To point to the beginning.

B. Loop can continue safely.

C. It returns the removed node.

D. To indicate end of list.

B. Loop can continue safely.

94
New cards

What must list::begin() return when the list is empty?

A. NULL.

B. The same iterator value as end().

C. A valid iterator to nothing.

D. An error value.

B. Same as end().

95
New cards

How does operator++(int) (postfix) differ from prefix ++ for iterators?

A. Postfix is faster.

B. Prefix returns copy.

C. Postfix returns original.

D. They are identical.

C. Postfix returns original.

96
New cards

What does list::empty() return when the list contains nodes?

A. true

B. 1

C. 0

D. false

D. false

97
New cards

Which list function increases num\_items after adding a node at the front?

A. insert_first()

B. add_head()

C. push_front()

D. increment_size()

C. push\_front()

98
New cards

Which list function decreases num\_items after removing from the back?

A. remove_last()

B. pop_back()

C. delete_end()

D. decrement_size()

B. pop\_back()

99
New cards

In an airline simulation, what variable stores the time the current service will finish?

A. service_time

B. finish_time

C. arrival_time

D. time_done

D. time\_done

100
New cards

What does an arrival\_rate represent in the simulation?

A. Total passengers served.

B. Average wait time.

C. Passenger arrival probability.

D. Service duration.

C. Passenger arrival probability.