Computer Knowledge
Data Structures and Algorithms
1,256 Questions
Data Structures and Algorithms form the core of computer science, focusing on arrays, linked lists, trees, and sorting mechanisms. These concepts are essential for solving complex computational problems efficiently. Test takers preparing for technical and administrative IT exams will find these questions highly relevant.
Array OperationsLinked List ApplicationsSorting AlgorithmsTree Data StructuresMultilevel IndexingAlgorithm Time Complexity
Data Structures and Algorithms Questions
A
Correct answer
Explanation
In Ruby, the index method (also called find_index) returns the position of the first occurrence of the specified element in the array. For days.index("Saturday"), this would return 5 (zero-indexed) or 6 (one-indexed position), which is the correct way to find an element's index.
-
A list of hashes which contains a list
-
A hash of hashes which contains a list
-
A list of list which contains another list
-
A list of arrays and hashes containing lists
A
Correct answer
Explanation
Without seeing the actual structure code, based on the correct answer being 'A list of hashes which contains a list', this describes a Perl data structure where you have an outer array (list), each element is a hashref, and each hash contains a key whose value is an arrayref (list). This is common in Perl for representing structured data like database query results or nested configurations.
D
Correct answer
Explanation
Vector insertions or deletions at arbitrary positions require shifting all subsequent elements to maintain contiguous storage. In the worst case, this means moving N-1 elements, resulting in O(N) time complexity.
-
Binary Search
-
Sequential Search
-
Both a and b
-
None
A
Correct answer
Explanation
The correct answer is A (Binary Search). When the number of records is large and the input is already sorted, binary search is the most efficient with O(log n) time complexity. Sequential search has O(n) complexity and becomes very slow for large datasets. Binary search repeatedly divides the search interval in half, making it exponentially faster than linear search for sorted data.
-
Using Index to access
-
Using subscript to access
-
Both a and b
-
None of the above
A
Correct answer
Explanation
The correct answer is A (Using Index to access). In COBOL and similar languages, accessing array elements using an index is more efficient than using subscripts. Indexes use binary addressing and direct memory access, while subscripts may require additional calculations or conversions. For performance-critical code involving repeated array access, index-based access is the preferred method.
B
Correct answer
Explanation
Java arrays are statically typed - an array is declared to hold a specific type (like int[], String[]) and cannot hold mixed types. To store different types, you would need an array of Object or use a Collection.
-
Merge
-
Binary
-
Quick
-
Bubble
B
Correct answer
Explanation
Merge Sort, Quick Sort, and Bubble Sort are all sorting algorithms. Binary search is a search algorithm for finding elements in sorted arrays, not a sorting algorithm.
-
Merge Sort
-
Selection Sort
-
Bubble Sort
-
Insersion Sort
A
Correct answer
Explanation
Merge Sort has O(n log n) average and worst-case time complexity because it divides the array in half recursively (log n levels) and merges n elements at each level. Selection Sort, Bubble Sort, and Insertion Sort are all O(n^2).
-
Merge Sort
-
Bubble Sort
-
Selection Sort
-
Quick Sort
A,D
Correct answer
Explanation
Divide and conquer algorithms recursively break problems into smaller subproblems, solve them independently, and combine results. Merge Sort divides array into halves then merges. Quick Sort partitions around a pivot and recursively sorts partitions.
-
Merge Sort
-
Selection Sort
-
Bubble Sort
-
Radix Sort
D
Correct answer
Explanation
Merge Sort, Selection Sort, and Bubble Sort all compare elements to determine ordering. Radix Sort processes individual digits/buckets and doesn't compare elements directly, making it non-comparison based.
-
Quick sort partition algorithm is not in-place.
-
Radix sort is stable.
-
In merge sort, maximum complexity is involved in Merge sub-procedure.
-
In quick sort, after recursive calls to partition algorithms, merging is not needed.
-
the outermost loop
-
the innermost loop
-
all loops are executed the same number of times
-
cannot be determined without knowing the size of the loops
B
Correct answer
Explanation
In nested loops, the innermost loop executes the most times because it completes all its iterations for each iteration of every outer loop. For example, in loops with 3, 4, and 5 iterations, the innermost runs 3×4×5=60 times.
-
- Bit map indexes are used on low-cardinality columns(having low distinct values) 2.B-tree indexes are most effective for high-cardinality data(only for unique columns)
-
B-tree indexes cannot be used in environments typically have large amounts of data and ad hoc queries
-
bitmap indexes can be created on partitioned tables
-
All of the above
-
Via Set
-
Direct
-
Calc
-
Index Sequential
B
Correct answer
Explanation
Direct record access is fastest because it uses a direct database key to locate records without requiring navigation through sets or index lookups. This provides the most efficient path to the data.
-
10 20 30
-
10 30 20
-
30 20 10
-
1 2 3
B
Correct answer
Explanation
In Perl, the sort function is non-destructive and returns the sorted list without modifying the original array in-place. Because the return value is discarded, @array remains unchanged as 10 30 20. Distractors assume in-place sorting or numerical conversion.