Computer Science Mid-Term Exam 2025
Computer Science Mid-Term Exam 2025
Linear search iterates through each element in a dataset to find a target value, operating efficiently with both sorted and unsorted datasets but being less optimal for large datasets due to its O(n) complexity. Binary search, on the other hand, is much more efficient with a time complexity of O(log n) but requires the dataset to be sorted beforehand . These differences significantly impact the performance based on the dataset organization .
A 'perfect hash function' is a type of hashing function where there are no collisions; every input maps uniquely to a distinct hash value. This is significant as it ensures constant time complexity O(1) for search operations, enhancing performance significantly. However, constructing a perfect hash function can be computationally challenging and is typically used when the set of keys is fixed and known beforehand .
Metadata in a database system represents data about data, providing information about other data's structure, origin, usage constraints, and more. This differs from actual data as metadata does not represent the content but rather describes the data's attributes, making it essential for data management, understanding the data context, ensuring data integrity, and optimizing queries .
The 'finally' block in Python exception handling is used to execute code regardless of whether an exception occurs or not. It ensures that the specified block of code runs no matter what, typically used for cleanup actions like releasing resources or closing files. In contrast, the 'try' block contains code that might cause an exception, and the 'except' block handles the exception if it occurs. Unlike 'except', 'finally' is always executed after 'try', regardless of the success or failure of the try block .
Bubble Sort and Selection Sort both have O(n^2) time complexity, but Bubble Sort has the advantage of potentially finishing early if the list becomes sorted ahead of time due to its ability to detect sorted sequences during passes. This adaptivity is absent in Selection Sort, making Bubble Sort sometimes faster in practice for nearly sorted datasets .
A Tuple in Python is an immutable sequence, meaning once it is created, its elements cannot be modified, which makes it distinct from Lists that are mutable. Sets, meanwhile, are unordered collections of unique items. The immutability of Tuples makes them suitable for use as dictionary keys and ensures data isn't changed accidentally, which is critical for maintaining data integrity and program stability .
The mid-index in the Binary Search algorithm is crucial as it determines the middle point for dividing the dataset into halves, enabling a divide-and-conquer approach to efficiently locate the target value. It is calculated using integer division as \((first + last) // 2\), ensuring an accurate midpoint selection by avoiding floating-point arithmetic issues, thus maintaining performance efficiency .
A Database Management System (DBMS) offers several advantages over traditional file systems, including better data integrity, reduced redundancy, easier access to data, and more security. Unlike file systems, DBMS provides a centralized framework to manage data, allowing for concurrent access and transaction management, which enhances the system's reliability and efficiency .
A Deque (double-ended queue) differs from a regular Queue as it allows insertion and deletion of elements from both ends, making it more versatile. In contrast, a Queue follows the First-In-First-Out (FIFO) principle, permitting insertions at the back and deletions at the front only. The flexibility of deques makes them suitable for scenarios needing access to both ends, efficiently supporting operations like O(1) insertions and deletions .
A stack in Python can be implemented using a list where the append() method is used to add elements to the stack (push), and the pop() method is used to remove elements from the top of the stack (pop). The stack follows the Last-In-First-Out (LIFO) principle, with the 'append()' method adding elements at the end of the list and 'pop()' removing the last element. This simplicity in methods facilitates efficient stack management .