Hashing: 32 MCQs with answers

All 32 multiple-choice questions from this kit, free and with no sign-up.

Warm-up · 22 questions

Q1. What is the primary purpose of a hash function in a hash table?

Beginner
A To compress the data to save memory B To map keys to indices in an array C To sort the keys in ascending order D To encrypt the data for security
Show answer & explanation

To map keys to indices in an array

A hash function computes an index from a key, enabling O(1) average-time access in a hash table.

Q2. Which of the following is a common hash function for integer keys?

Beginner
A key % table_size B key * table_size C key / table_size D key + table_size
Show answer & explanation

key % table_size

The modulo operation (key % table_size) is a simple and widely used hash function to map keys into the range of table indices.

Q3. What is a collision in hashing?

Beginner
A The hash table is full B The hash function returns a negative value C The key is not found in the table D Two different keys map to the same hash index
Show answer & explanation

Two different keys map to the same hash index

A collision occurs when a hash function produces the same index for two or more distinct keys.

Q4. Which collision resolution technique uses a linked list at each table slot?

Beginner
A Linear probing B Separate chaining C Double hashing D Quadratic probing
Show answer & explanation

Separate chaining

Separate chaining stores all elements that hash to the same index in a linked list (or similar structure) at that slot.

Q5. Which of the following is a common application of hashing?

Beginner
A Sorting numbers B Database indexing C Graph coloring D Binary tree traversal
Show answer & explanation

Database indexing

Hashing is widely used in database indexing, symbol tables, caches, and many other applications requiring fast lookups.

Q6. A hash function can map two different keys to the same index.

Beginner
A True B False
Show answer & explanation

True

Yes, this is called a collision, which is inevitable when the number of possible keys exceeds the table size.

Q7. In a hash table, the order of elements is preserved as they are inserted.

Beginner
A True B False
Show answer & explanation

False

Hash tables do not preserve insertion order; elements are placed based on their hash values, not their insertion sequence.

Q8. What is the primary purpose of a hash function in a hash table?

Beginner
A To map keys to array indices B To sort the keys in ascending order C To encrypt the keys for security D To compress the data to save memory
Show answer & explanation

To map keys to array indices

A hash function's main role is to convert a key into an array index, enabling direct access to the table slot.

Q9. Which of the following is a common hash function for integer keys?

Beginner
A Key multiplied by table size ($k \times m$) B Key divided by table size ($k / m$) C Key plus table size ($k + m$) D Key modulo table size ($k \mod m$)
Show answer & explanation

Key modulo table size ($k \mod m$)

The modulo operation ($k \mod m$) is a standard and simple hash function for integer keys, producing an index in the range $[0, m-1]$.

Q10. What is a collision in hashing?

Beginner
A When the hash function is too slow B When two keys are identical C When two different keys map to the same index D When the hash table is full
Show answer & explanation

When two different keys map to the same index

A collision occurs when a hash function maps two distinct keys to the same array index, requiring a resolution strategy.

Q11. Which collision resolution technique uses a linked list at each table slot?

Beginner
A Double hashing B Separate chaining C Quadratic probing D Linear probing
Show answer & explanation

Separate chaining

Separate chaining stores all elements that hash to the same index in a linked list at that slot.

Q12. In open addressing, when a collision occurs, what does the algorithm do?

Beginner
A It discards the new key B It probes alternative slots until an empty one is found C It rehashes the entire table immediately D It creates a new linked list at the current slot
Show answer & explanation

It probes alternative slots until an empty one is found

Open addressing resolves collisions by searching for the next available slot using a probe sequence, unlike chaining which uses extra memory outside the table.

Q13. What is the load factor (α) of a hash table?

Beginner
A The average length of the linked lists B The size of the hash table in bytes C The number of collisions that have occurred D The ratio of the number of stored elements to the table size
Show answer & explanation

The ratio of the number of stored elements to the table size

The load factor $\alpha = n/m$ (where $n$ is the number of keys and $m$ is the table size) indicates how full the table is, affecting performance.

Q14. Which of the following is a characteristic of a good hash function?

Beginner
A It depends on the order of insertion B It is as complex as possible C It distributes keys uniformly across the table D It always returns the same index for all keys
Show answer & explanation

It distributes keys uniformly across the table

A good hash function minimizes collisions by spreading keys evenly over the table, leading to near O(1) average operations.

Q15. What is the average time complexity for search in a well-designed hash table?

Beginner
A O(n) B O(n log n) C O(log n) D O(1)
Show answer & explanation

O(1)

With a good hash function and low load factor, the average search time is constant, O(1), independent of the number of elements.

Q16. In linear probing, if slot h(k) is occupied, where is the next slot tried?

Beginner
A h(k) + 1 (mod table size) B h(k) + 2 (mod table size) C A random slot D h(k) * 2 (mod table size)
Show answer & explanation

h(k) + 1 (mod table size)

Linear probing checks consecutive slots: $(h(k) + i) \mod m$ for $i = 1, 2, \ldots$, so the next slot is $h(k)+1$ modulo the table size.

Q17. What is the main disadvantage of separate chaining?

Beginner
A It has O(n) average search time B It requires extra memory for pointers or linked lists C It only works with integer keys D It cannot handle collisions
Show answer & explanation

It requires extra memory for pointers or linked lists

Separate chaining uses additional memory to store linked list nodes, which can be a drawback compared to open addressing that uses the table itself.

Q18. What is the primary operation that a hash function performs in a hash table?

Beginner
A It compresses the data to save memory. B It maps keys to array indices. C It encrypts the keys for security. D It sorts the keys in ascending order.
Show answer & explanation

It maps keys to array indices.

A hash function takes a key and computes an array index where the corresponding value is stored. This mapping enables average O(1) access.

Q19. Which of the following is a common collision resolution technique in hashing?

Beginner
A Binary search B Separate chaining C Recursion D Merge sort
Show answer & explanation

Separate chaining

Separate chaining is a collision resolution method where each table slot holds a linked list of keys that hash to the same index. Other options are unrelated to collision resolution.

Q20. What is the average time complexity for insert, delete, and search operations in a well-designed hash table?

Beginner
A O(1) B O(log n) C O(n log n) D O(n)
Show answer & explanation

O(1)

A well-designed hash table with a good hash function and appropriate load factor achieves average constant time O(1) for insert, delete, and search operations.

Q21. In the context of hashing, what does the term 'load factor' (α) represent?

Beginner
A The size of the hash table in bytes B The ratio of the number of stored elements to the table size C The number of collisions in the table D The number of empty slots in the table
Show answer & explanation

The ratio of the number of stored elements to the table size

The load factor α is defined as the number of elements stored divided by the table size. It is a key indicator of how full the table is and affects performance.

Q22. Which of the following is a common application of hashing?

Beginner
A Finding the shortest path in a graph B Sorting a list of numbers C Traversing a binary tree D Implementing a symbol table in a compiler
Show answer & explanation

Implementing a symbol table in a compiler

Hashing is widely used in symbol tables (e.g., in compilers), databases, and caches to provide fast key-based lookup. Sorting, tree traversal, and graph algorithms are not primary applications of hashing.

Building up · 7 questions

Q23. In open addressing, when a collision occurs, what does the algorithm do?

Intermediate
A It probes for the next available slot B It creates a new linked list C It rehashes the entire table D It discards the new key
Show answer & explanation

It probes for the next available slot

Open addressing resolves collisions by probing (e.g., linear, quadratic, or double hashing) to find another empty slot within the same array.

Q24. What is the load factor (α) of a hash table?

Intermediate
A Number of slots minus number of keys B Number of collisions divided by number of keys C Number of slots divided by number of keys D Number of keys divided by number of slots
Show answer & explanation

Number of keys divided by number of slots

Load factor α = n/m, where n is the number of keys stored and m is the number of slots. It indicates how full the table is.

Q25. Which of the following is a characteristic of a good hash function?

Intermediate
A It is as complex as possible B It always returns the same index for all keys C It depends on the order of insertion D It distributes keys uniformly across the table
Show answer & explanation

It distributes keys uniformly across the table

A good hash function minimizes collisions by spreading keys uniformly over the available indices.

Q26. What is the average time complexity for search in a well-designed hash table?

Intermediate
A O(log n) B O(n) C O(n log n) D O(1)
Show answer & explanation

O(1)

With a good hash function and low load factor, hash table operations achieve constant average time complexity, O(1).

Q27. In linear probing, if slot h(k) is occupied, where is the next slot tried?

Intermediate
A h(k) - 1 B h(k) + 1 C h(k) + 2 D h(k) * 2
Show answer & explanation

h(k) + 1

Linear probing checks consecutive slots: (h(k) + i) mod m for i = 0, 1, 2, ... until an empty slot is found.

Q28. What is the main disadvantage of separate chaining?

Intermediate
A It has O(n) average search time B It cannot handle collisions C It only works with integer keys D It requires extra memory for pointers
Show answer & explanation

It requires extra memory for pointers

Separate chaining uses linked lists, which require additional memory for node pointers, unlike open addressing which stores all elements in the array itself.

Q29. Which of the following is NOT a method of open addressing?

Intermediate
A Separate chaining B Linear probing C Double hashing D Quadratic probing
Show answer & explanation

Separate chaining

Separate chaining is a different collision resolution technique that uses linked lists, not open addressing.

Topper level · 3 questions

Q30. What is the primary reason for choosing a prime number as the table size in modulo-based hashing?

Advanced
A It simplifies the code B It allows storing more keys C It makes the hash function faster D It reduces the number of collisions
Show answer & explanation

It reduces the number of collisions

A prime table size helps distribute keys more uniformly, especially when keys have patterns, reducing clustering and collisions.

Q31. In double hashing, the probe sequence is determined by:

Advanced
A The key's length B A single hash function C A random number generator D Two different hash functions
Show answer & explanation

Two different hash functions

Double hashing uses a second hash function to compute the step size, making the probe sequence more flexible and reducing clustering.

Q32. What is the worst-case time complexity for search in a hash table with separate chaining?

Advanced
A O(n) B O(log n) C O(n^2) D O(1)
Show answer & explanation

O(n)

In the worst case, all keys collide into one chain, making the search linear in the number of keys, O(n).

Make your own

Your notes can be a kit like these

Every kit here was made from real study material. Point Veda at a chapter, a PDF, a photo or a link and get notes, flashcards, practice MCQs, games and a scored test — free, in 42 languages.

  • Chapter notes
  • Flashcards
  • Practice MCQs
  • Scored tests
  • Exam prep

Get Veda free See what students get

Free to start · No ads · Works in 42 languages