Data Structures Practice Test — Questions and Answers
Question 1: Which of the following uses the first-in, first-out (FIFO) method?
- Hash Table
- Queue (Correct answer)
- Binary Search Tree
- Stack
Correct answer: Queue
A Queue is a linear data structure that follows the First-In, First-Out (FIFO) principle. This means the first element added to the queue is the first one to be removed, similar to people waiting in a line. Elements are added at the rear (enqueue) and removed from the front (dequeue).
Question 2: Which of the following isn't an example of an internal sort?
- Bubble Sort
- Insertion Sort
- Heap Sort
- Merge Sort (Correct answer)
Correct answer: Merge Sort
Internal sorting algorithms process data entirely within the main memory of a computer. Bubble Sort, Insertion Sort, and Heap Sort are all examples of internal sorts. Merge Sort, while it can be implemented internally, is often used as an external sorting algorithm when the data to be sorted is too large to fit into main memory, requiring the use of external storage like disk drives.
Question 3: The first element of an array's memory address is referred to as.
- first address
- base address (Correct answer)
- floor address
- foundation address
Correct answer: base address
The base address of an array is the memory address of its first element (at index 0). All other elements' addresses are calculated relative to this base address using their index and the size of each element. This fundamental concept is crucial for understanding how arrays are stored and accessed in memory.
Question 4: Because aposterior analysis is more accurate than apriori analysis, it is
- it is a result of reverse-engineering
- it contains the real data.
- it assumes all other facets to be dynamic (Correct answer)
- it assumes all other factors to be constant.
Correct answer: it assumes all other facets to be dynamic
A posteriori analysis, also known as empirical or experimental analysis, involves running the algorithm on actual data and measuring its performance. Unlike a priori analysis, which uses theoretical calculations and assumes constant factors, a posteriori analysis accounts for real-world factors like system load, hardware, and specific input data, making its results more reflective of actual performance. Therefore, it considers other facets to be dynamic and variable.
Question 5: Check that all of the statements are correct.
- Swift has enums, whereas Java does not.
- Swift has Optional, whereas Java 7 does not. (Correct answer)
- Swift has Type Inferencing, while Java does not. (Correct answer)
- Swift has Structs, whereas Java 7 does not. (Correct answer)
- Swift Classes are value types, unlike Java Classes, which are reference types.
Correct answer: Swift has Optional, whereas Java 7 does not.
Swift introduced Optionals to safely handle the absence of a value, preventing null pointer exceptions. Java 7 did not have a direct equivalent to Swift's Optional type; `null` was used, which is prone to NullPointerExceptions. Java 8 later introduced `java.util.Optional` to address this, but the question specifically refers to Java 7.
Question 6: Prefix notation is also referred to as
- Polish Notation (Correct answer)
- Polish Reverse Notation
- Reverse Polish Notation
- Reverse Notation
Correct answer: Polish Notation
Prefix notation, where operators precede their operands, was invented by Polish logician Jan Łukasiewicz. Hence, it is commonly referred to as Polish Notation. This notation eliminates the need for parentheses in expressions.
Question 7: The level is the point at where the model becomes executable code.
- Implementation level (Correct answer)
- Abstract level
- Application level
- All of the above
Correct answer: Implementation level
The implementation level is where the abstract data model or algorithm design is translated into concrete, executable code using a specific programming language. At this stage, data structures are defined and algorithms are written with specific syntax and semantics, making the model functional.
Question 8: In ___, the herder node serves as a sentinel.
- Queues
- Binary tree (Correct answer)
- Stacks
- Graphs
Correct answer: Binary tree
While 'herder node' is not a standard term, a 'sentinel node' is sometimes used in data structures like linked lists or trees to simplify boundary conditions. In the context of binary trees, a sentinel node might represent null children, simplifying traversal or insertion/deletion logic by always having a node to point to, even if it's a dummy.
Question 9: The number of binary trees with three nodes that deliver the result when traversed in post order. <br> What is the A,B,C sequence?
- 2
- 5 (Correct answer)
- 7
- 3
Correct answer: 5
The number of distinct binary trees with 'n' nodes is given by the n-th Catalan number. For n=3 nodes, the 3rd Catalan number is C_3 = (1/(3+1)) * (2*3 choose 3) = (1/4) * (6 choose 3) = (1/4) * 20 = 5. Each of these 5 distinct binary trees will produce a unique post-order traversal sequence for a given set of node values.
Question 10: InOrder is best described by which of the following?
- Right, Head, Left
- Right, Left, Head
- Left, Head, Right (Correct answer)
- Head, Left, Right
Correct answer: Left, Head, Right
In-order traversal of a binary tree visits the nodes in the sequence: traverse the left subtree, visit the root (head) node, then traverse the right subtree. This traversal method is particularly useful for binary search trees because it visits nodes in ascending order of their values.
Question 11: The traversal of a binary search tree in order will give
- reverse of input
- sorted list (Correct answer)
- unsorted list
- none of the above
Correct answer: sorted list
A key property of a Binary Search Tree (BST) is that for any node, all values in its left subtree are smaller than its own value, and all values in its right subtree are larger. Therefore, an in-order traversal (Left -> Root -> Right) of a BST will visit the nodes in monotonically increasing order, effectively producing a sorted list of the tree's elements.
Question 12: Before moving on to the next vertex, the _________ traversal processes all of a vertex's descendants.
- Breadth First
- With First
- Depth Limited
- Depth First (Correct answer)
Correct answer: Depth First
Depth-First Search (DFS) explores as far as possible along each branch before backtracking. This means it fully processes all descendants of a vertex down one path before moving to an unvisited neighbor of the current vertex or backtracking to an ancestor.
Question 13: Stack can also be referred to ;
- First in first out
- Last in last out
- Last in first out (Correct answer)
- First in last out
Correct answer: Last in first out
A Stack is a linear data structure that follows the Last-In, First-Out (LIFO) principle. This means the last element added to the stack is the first one to be removed, similar to a stack of plates where you remove the top one first. Elements are added (pushed) and removed (popped) from the same end, called the top.
Question 14: Which of these alogrithmic methods attempts to find a localized optimum solution -
- Dynamic approach
- Greedy approach (Correct answer)
- Divide and conquer approach
- All of the above
Correct answer: Greedy approach
A greedy algorithm makes the locally optimal choice at each stage with the hope of finding a global optimum. It doesn't consider future consequences or backtrack, simply picking the best immediate option. This often leads to a localized optimum, which may or may not be the global optimum.
Question 15: PreOrder is best described by which of the following?
- Left, Right, Head
- Right, Head, Left
- Head, Left, Right (Correct answer)
- Head, Right, Left
Correct answer: Head, Left, Right
Pre-order traversal of a binary tree visits the nodes in the sequence: visit the root (head) node, then traverse the left subtree, and finally traverse the right subtree. This traversal is often used to create a copy of the tree or to express the tree structure.
Question 16: A procedure that calls itself is referred to as a
- reverse polish
- recursive (Correct answer)
- illegal call
- none of the above
Correct answer: recursive
Recursion is a programming technique where a function or procedure calls itself directly or indirectly to solve a problem. This approach is often used for problems that can be broken down into smaller, self-similar subproblems, such as traversing tree structures or calculating factorials.
Question 17: Two-dimensional arrays are also referred to as
- matrix arrays
- tables arrays
- both A & B (Correct answer)
- none of above
Correct answer: both A & B
Two-dimensional arrays are commonly visualized and used as matrices in mathematics and computer science, representing rows and columns of data. They are also frequently referred to as tables because they organize data in a tabular format. Therefore, both 'matrix arrays' and 'tables arrays' are appropriate descriptions.
Question 18: Which method can determine whether two vertices x and y have a path connecting them?
- Breadth First Search
- Depth First Search
- Both Breadth First Search & Depth First Search (Correct answer)
- None of above
Correct answer: Both Breadth First Search & Depth First Search
Both Breadth-First Search (BFS) and Depth-First Search (DFS) are graph traversal algorithms that can be used to determine reachability between two vertices. If either algorithm, starting from vertex x, visits vertex y, then a path exists between them. BFS finds the shortest path in terms of number of edges, while DFS explores one path fully before backtracking.
Question 19: The best way to code programs and generate functions is to use recursion.
- A) False (Correct answer)
- B) True
Correct answer: A) False
While recursion can provide elegant and concise solutions for certain problems (like tree traversals or fractals), it is not always the 'best' way. Recursive solutions can be less efficient due to function call overhead, consume more memory (stack space), and can be harder to debug than iterative solutions for many problems. The 'best' approach depends on the specific problem and context.
Question 20: In order for a binary search method to work, the array (list) must be empty
- popped out of stack
- sorted (Correct answer)
- unsorted
- in a heap
Correct answer: sorted
The binary search algorithm relies on the array being sorted to efficiently locate an element. It works by repeatedly dividing the search interval in half. If the array is unsorted, this division strategy will not guarantee finding the element or determining its absence correctly.
Question 21: Non-homogeneous data items cannot be stored in which of the following data structures?
- Records
- Pointers
- Arrays (Correct answer)
- None of the above
Correct answer: Arrays
Arrays are typically designed to store a collection of homogeneous data items, meaning all elements must be of the same data type (e.g., an array of integers, an array of strings). Records (structs or objects) and pointers, however, can be used to manage or link non-homogeneous data items.
Question 22: Scheduling a project is an example of
- D) none of the above.
The correct answer for this question is missing from the provided data. Project scheduling typically involves concepts from graph theory, such as finding the critical path, and often utilizes optimization algorithms to manage tasks and resources efficiently.
Question 23: The data structure of a queue is based on
- LIFO
- FIFO (Correct answer)
- none of the above
Correct answer: FIFO
A queue is a linear data structure that strictly adheres to the First-In, First-Out (FIFO) principle. This means that the first element added to the queue is always the first one to be removed, much like a waiting line in real life. New elements are added to the rear (enqueue), and existing elements are removed from the front (dequeue).
Question 24: When determining the efficiency of an algorithm, the time factor is
- Counting the kilobytes of algorithm
- Counting the number of statements
- Counting microseconds
- Counting the number of key operations (Correct answer)
Correct answer: Counting the number of key operations
The efficiency of an algorithm, particularly its time complexity, is typically determined by counting the number of key operations it performs. Key operations are fundamental steps that significantly contribute to the algorithm's execution time, such as comparisons, assignments, or arithmetic calculations. This method provides a machine-independent measure of how an algorithm's performance scales with input size.
Question 25: What data structure may be utilized to determine whether or not a syntax contains balanced paranthesis?
- list
- queue
- stack (Correct answer)
- tree
Correct answer: stack
A stack is the ideal data structure for checking balanced parentheses due to its Last-In, First-Out (LIFO) nature. When an opening parenthesis is encountered, it's pushed onto the stack. Upon finding a closing parenthesis, the top element is popped and checked for a match, ensuring that parentheses are closed in the reverse order they were opened.
Which of the following uses the first-in, first-out (FIFO) method?