Data Structures Introduction
- 31Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
In computer science, what is a data structure?
In computer science, what is a data structure?
31 questions
- 1
In computer science, what is a data structure?
- A
A method for sorting data in ascending order
- B
A way to store, organize, and manage data for easy access and modification
- C
A type of computer hardware that stores information
- D
A programming language used to write algorithms
Correct answer
BA way to store, organize, and manage data for easy access and modification
Explanation
A data structure is a way to store, organize, and manage data so that it can be accessed and modified efficiently. It is not a programming language, hardware, or sorting method.
- A
- 2
What is the purpose of parallel arrays?
- A
To combine two arrays into one by merging their elements
- B
To store different types of data about the same entity in separate arrays with the same number of elements
- C
To allow multiple arrays to share the same memory space
- D
To store multiple copies of the same array for redundancy
Correct answer
BTo store different types of data about the same entity in separate arrays with the same number of elements
Explanation
Parallel arrays are two or more arrays with the same number of elements where each element in one array corresponds to the element in the same position in another array. This allows storing different data types about the same entity.
- A
- 3
Which of the following is NOT a requirement for an array?
- A
A size that can be changed after creation
- B
A type that determines what kind of data can be stored
- C
A name to reference the array
- D
All elements must be of the same type
Correct answer
AA size that can be changed after creation
Explanation
An array has a fixed size that cannot be changed after creation. The other options are requirements: a name, a type, and all elements must be of the same type.
- A
- 4
Arrays store elements contiguously in memory. This property directly enables which operation to be O(1)?
- A
Accessing an element by index
- B
Searching for a specific value
- C
Deleting an element from the end
- D
Inserting an element at the beginning
Correct answer
AAccessing an element by index
Explanation
Because array elements are stored contiguously, the memory address of any element can be computed directly from its index, allowing constant-time access.
- A
- 5
What is the worst-case time complexity for inserting an element at the beginning of an array of size n?
- A
O(n)
- B
O(log n)
- C
O(n^2)
- D
O(1)
Correct answer
AO(n)
Explanation
Inserting at the beginning requires shifting all existing elements one position to the right, which takes O(n) operations in the worst case.
- A
- 6
What is the primary difference between an array and an ArrayList?
- A
An ArrayList is a built-in language feature, while an array requires a class import
- B
An array allows constant-time insertion and deletion, while an ArrayList does not
- C
An array can store objects, while an ArrayList can only store primitive types
- D
An ArrayList can dynamically resize, while an array has a fixed size
Correct answer
DAn ArrayList can dynamically resize, while an array has a fixed size
Explanation
The main difference is that an ArrayList can grow or shrink dynamically, whereas an array has a fixed size once created. ArrayLists can only store objects, not primitives, but autoboxing often mitigates this.
- A
- 7
Which of the following methods is NOT a standard method of the ArrayList class?
- A
remove()
- B
push()
- C
toArray()
- D
add()
Correct answer
Bpush()
Explanation
push() is a method of the Stack class, not the ArrayList class. Standard ArrayList methods include add, remove, get, set, clear, and toArray.
- A
- 8
How does an ArrayList achieve O(1) access time despite its data not being stored contiguously?
- A
It uses linked lists to chain elements together
- B
It stores references to objects contiguously, allowing direct index-based retrieval
- C
It stores all data in a contiguous block and resizes as needed
- D
It uses a hash table to map indices to memory locations
Correct answer
BIt stores references to objects contiguously, allowing direct index-based retrieval
Explanation
The ArrayList stores references (pointers) to the actual objects in a contiguous array. Accessing an index directly retrieves the reference, which then points to the object in memory.
- A
- 9
What principle does a stack data structure follow?
- A
Last In, First Out (LIFO)
- B
First In, Last Out (FILO)
- C
Random Access
- D
First In, First Out (FIFO)
Correct answer
ALast In, First Out (LIFO)
Explanation
A stack follows the LIFO (Last In, First Out) principle, meaning the last element added is the first one to be removed.
- A
- 10
Which stack method removes and returns the top element?
- A
contains()
- B
peek()
- C
push()
- D
pop()
Correct answer
Dpop()
Explanation
The pop() method removes the element at the top of the stack and returns it. push() adds an element, peek() returns the top without removing, and contains() checks for existence.
- A
- 11
What is the time complexity for inserting (push) an element onto a stack?
- A
O(1)
- B
O(n log n)
- C
O(n)
- D
O(log n)
Correct answer
AO(1)
Explanation
Push adds an element to the top of the stack in constant time, regardless of the stack's size.
- A
- 12
Which real-world application uses a stack data structure?
- A
Autocomplete in text messaging
- B
Job scheduling in an operating system
- C
Finding the shortest path between two points
- D
Undo/Redo functionality in word processors
Correct answer
DUndo/Redo functionality in word processors
Explanation
Undo/Redo uses a stack to keep track of actions. Job scheduling uses queues, autocomplete uses tries, and shortest path uses graphs.
- A
- 13
A queue follows which principle?
- A
Random Access
- B
First In, Last Out (FILO)
- C
Last In, First Out (LIFO)
- D
First In, First Out (FIFO)
Correct answer
DFirst In, First Out (FIFO)
Explanation
A queue follows FIFO (First In, First Out), where the first element added is the first to be removed.
- A
- 14
In a queue, where are new elements added and old elements removed?
- A
New elements are added to the back; old elements are also removed from the back
- B
New elements are added to the front; old elements are removed from the back
- C
New elements are added to the front; old elements are also removed from the front
- D
New elements are added to the back; old elements are removed from the front
Correct answer
DNew elements are added to the back; old elements are removed from the front
Explanation
In a queue, elements are added at the back (tail) and removed from the front (head) to maintain FIFO order.
- A
- 15
What is the worst-case time complexity for searching in a queue of size n?
- A
O(n)
- B
O(log n)
- C
O(1)
- D
O(n^2)
Correct answer
AO(n)
Explanation
Searching for an element in a queue requires checking each element from front to back in the worst case, taking O(n) time.
- A
- 16
A linked list is a sequential access data structure. What does each node contain?
- A
Data and a reference to the previous node
- B
Only data
- C
A reference to the next node only
- D
Data and a reference to the next node
Correct answer
DData and a reference to the next node
Explanation
Each node in a singly linked list contains data and a reference (pointer) to the next node. The last node's reference points to null.
- A
- 17
What is the time complexity for inserting a node at the head of a linked list?
- A
O(1)
- B
O(n)
- C
O(log n)
- D
O(n log n)
Correct answer
AO(1)
Explanation
Inserting at the head only requires updating the new node's pointer to the old head and updating the head reference, both constant-time operations.
- A
- 18
Why is the worst-case time complexity for deleting a node in the middle of a linked list O(n)?
- A
Because deleting a node requires shifting all subsequent nodes
- B
Because you must first traverse to the node before the one to delete
- C
Because the pointer adjustment takes O(n) time
- D
Because you must copy all remaining nodes to a new list
Correct answer
BBecause you must first traverse to the node before the one to delete
Explanation
To delete a node in the middle, you must first find the node before it by traversing from the head, which can take O(n) steps. The actual deletion (pointer update) is O(1).
- A
- 19
What is the main advantage of a doubly linked list over a singly linked list?
- A
It supports constant-time access to any element
- B
It allows traversal in both forward and backward directions
- C
It uses less memory per node
- D
It allows insertion at the head in O(1) time
Correct answer
BIt allows traversal in both forward and backward directions
Explanation
A doubly linked list has an extra pointer (previous), enabling traversal both forward and backward. This is useful for operations like undo/redo or browser back/forward.
- A
- 20
In a dictionary (map), what is a key-value pair?
- A
A pair that maps an integer index to a value
- B
A pair where keys and values can be duplicated
- C
A pair where each key is associated with a value, and keys must be unique
- D
A pair where each value is associated with a key, and values must be unique
Correct answer
CA pair where each key is associated with a value, and keys must be unique
Explanation
A dictionary stores data as key-value pairs. Each key is unique and maps to exactly one value. Values can be duplicated, but keys cannot.
- A
- 21
What data structure is commonly used to implement a dictionary?
- A
Binary search tree
- B
Linked list
- C
Array
- D
Hash table
Correct answer
DHash table
Explanation
Dictionaries are typically implemented using hash tables, which provide average O(1) time for insertion, deletion, and search operations.
- A
- 22
What is a hash collision?
- A
When a hash function produces an index outside the table size
- B
When the hash table becomes full
- C
When two different keys are mapped to the same index by a hash function
- D
When two hash functions are used simultaneously
Correct answer
CWhen two different keys are mapped to the same index by a hash function
Explanation
A hash collision occurs when two distinct keys produce the same hash value (index). Solutions include open addressing and closed addressing (chaining).
- A
- 23
In a binary search tree, which property ensures logarithmic search time?
- A
The tree is always balanced
- B
All leaf nodes are at the same depth
- C
Each node has at most two children, and left child ≤ parent ≤ right child
- D
The root node contains the median value
Correct answer
CEach node has at most two children, and left child ≤ parent ≤ right child
Explanation
The ordered property (left ≤ parent ≤ right) allows the search to eliminate half of the tree at each step, leading to O(log n) average time.
- A
- 24
In a tree data structure, what is a leaf node?
- A
A node with no children
- B
A node that has exactly one child
- C
The topmost node of the tree
- D
A node that is a child of the root
Correct answer
AA node with no children
Explanation
A leaf node has no children. The root is at the top, and nodes with children are internal nodes.
- A
- 25
What is a try (prefix tree) primarily used for?
- A
Performing arithmetic operations
- B
Storing and retrieving strings efficiently
- C
Representing undirected graphs
- D
Sorting numbers in descending order
Correct answer
BStoring and retrieving strings efficiently
Explanation
A try stores characters in a tree structure, allowing fast retrieval of words, making it ideal for autocomplete and spellcheck.
- A
- 26
How is the end of a word indicated in a try?
- A
By a leaf node containing the last character
- B
By a null pointer in the node's array
- C
By a flag node or special marker
- D
By a node with no children
Correct answer
CBy a flag node or special marker
Explanation
A flag (e.g., a special node or boolean) marks the end of a word so that prefixes and full words can be distinguished.
- A
- 27
In a max heap, which value is stored at the root?
- A
The maximum value in the heap
- B
The first value inserted
- C
The minimum value in the heap
- D
The median value
Correct answer
AThe maximum value in the heap
Explanation
In a max heap, the root contains the largest value, and every parent node is greater than or equal to its children.
- A
- 28
What is the heapify process used for after deleting the root of a heap?
- A
To increase the size of the heap
- B
To convert the heap into an array
- C
To restore the heap property by swapping nodes downward
- D
To sort the heap in ascending order
Correct answer
CTo restore the heap property by swapping nodes downward
Explanation
After removing the root, the last element is moved to the root, and heapify (sifting down) restores the heap property by swapping with larger children (in a max heap).
- A
- 29
Which sorting algorithm is based on the heap data structure?
- A
Quick sort
- B
Heap sort
- C
Insertion sort
- D
Merge sort
Correct answer
BHeap sort
Explanation
Heap sort builds a heap from the list and repeatedly extracts the root to produce a sorted list.
- A
- 30
What distinguishes a graph from a tree?
- A
A graph has no root node and can have cycles, while a tree has a root and is acyclic
- B
A graph cannot have weighted edges, while a tree can
- C
A graph can only have one edge between two nodes, while a tree can have multiple
- D
A graph is linear, while a tree is hierarchical
Correct answer
AA graph has no root node and can have cycles, while a tree has a root and is acyclic
Explanation
Trees are a type of acyclic graph with a root. Graphs can be cyclic, have no designated root, and can be directed or undirected.
- A
- 31
In an undirected graph, what indicates a cycle?
- A
A path from a node back to itself following edges
- B
A node with degree 0
- C
An edge that connects a node to itself
- D
All edges are bidirectional
Correct answer
AA path from a node back to itself following edges
Explanation
A cycle exists if there is a path (of at least one edge) that starts and ends at the same node. In an undirected graph, any two connected nodes form a potential cycle.
- A

Ask anything
We've read this whole study set, so questions about it get the best answers.