An Introduction to Python Data Structures — Hash-map, Tree, Graph Quiz

Explore the basics of non-linear data structures in Python, including how hash-maps, trees, and graphs function differently from linear structures like arrays and stacks.

  1. Hash-map Structure

    Which data structure is primarily used to implement a hash-map as described in the article?

    1. Array
    2. Stack
    3. Tree
    4. Queue

    Explanation: Hash-maps are generally implemented using arrays, as noted in the article. Trees are more common in other data structures and algorithms, while stacks and queues are linear structures unrelated to hash-map implementation.

  2. Key Characteristic of Hash-map

    What is the main feature of a hash-map in Python according to the article?

    1. Does not allow duplicate values
    2. Requires sorted keys
    3. Stores data as key-value pairs
    4. Arranges data in a linear order

    Explanation: Hash-maps use key-value pairs, where each key is mapped to a value via a hash function. They are not arranged in a linear order, do not require sorted keys, and allow duplicate values as long as keys are unique.

  3. Hash Function Collision

    What major disadvantage can occur with hash functions when using hash-maps?

    1. Slow access speed
    2. Collision of keys
    3. Data always sorted
    4. Excess memory usage

    Explanation: A major disadvantage is that a hash function might produce the same key for different data, causing collisions. The article does not mention automatic sorting, slow access speed, or excess memory usage as primary issues.

  4. Handling Collisions

    How does the article suggest handling hash-map collisions?

    1. Creating a list at the key and appending
    2. Sorting all values
    3. Raising an error immediately
    4. Removing the conflicting data

    Explanation: The recommended solution is to create a list at each key's position and append new colliding items to that list. The article does not suggest removing data, sorting values, or raising immediate errors.

  5. Recursive Structure

    Which non-linear data structure property is highlighted in the article?

    1. Fixed memory allocation
    2. Strictly linear arrangement
    3. Constant sorting order
    4. Recursive type of structure

    Explanation: Non-linear data structures are described as recursive, meaning they can be defined in terms of themselves. The article contrasts this with linear arrangement, and does not attribute constant sorting or fixed memory allocation to them.

  6. Basic Hash-map Operation

    Which of these is NOT listed as a basic operation for hash-maps?

    1. Get Item
    2. Delete
    3. Sort
    4. Insert

    Explanation: The article lists Insert, Delete, and Get Item as basic operations for hash-maps. Sorting is not mentioned as a typical operation performed on hash-maps.

  7. get_hash Method Function

    What does the get_hash method do in the provided hash-map class?

    1. Calculates a numeric hash key from a string
    2. Stores the key and value
    3. Prints all values
    4. Deletes items from the map

    Explanation: The get_hash method takes a key and calculates a numeric hash (hash_key) using the ord function and a mathematical operation. It does not store, delete, or print data directly.

  8. List Initialization in Hash-map

    How is the internal storage for the hash-map initialized in the class?

    1. A fixed set of keys
    2. A list of zeros
    3. A list of empty lists based on a maximum size
    4. A single empty list

    Explanation: The hash-map class initializes storage as a list of empty lists, with one list for each possible hash key, determined by the maximum size. It is not initialized with zeros, a single list, or predefined keys.

  9. Data Access in Hash-map

    Which method retrieves a value from the hash-map using its key?

    1. add
    2. get_hash
    3. print_hm
    4. get

    Explanation: The 'get' method retrieves the value paired with a specific key by calculating its hash and searching in the corresponding list. 'add' is for insertion, 'print_hm' outputs values, and 'get_hash' computes the hash key.

  10. Difference from Linear Structures

    What distinguishes non-linear data structures like trees and graphs from linear data structures such as arrays or stacks?

    1. They cannot store key-value pairs
    2. They require less memory
    3. Data can only be accessed sequentially
    4. Data is stored across multiple levels

    Explanation: Non-linear structures store data across multiple levels and do not arrange it in a single line. Sequential access and memory claims are features of linear data structures or not mentioned, and non-linear structures can store key-value pairs.