Programming and Problem Solving Through Python Language Theory Question

Dictionary In Python

  1. What is the primary characteristic of a dictionary in Python ?
    1. Dictionaries are ordered collections of elements.
    2. Dictionaries can contain duplicate values.
    3. Dictionaries store elements in a sorted manner.
    4. Dictionaries use key-value pairs to store data.
    Answer : D
    Explain : Dictionary is an unordered collection of items where each item consist of key and a value.
  2. How are dictionaries represented in Python ?
    1. In square brackets: []
    2. In curly braces: {}
    3. In parentheses: ()
    4. In angle brackets: <>
    Answer : B
    Explain : Dictionary use curly bracket {}.
  3. What will be the output of the following code ?
    my_dict = {"name": "Alice", "age": 25, "city": "New York"}
    print(my_dict["age"])	
    
    1. "Alice"
    2. 25
    3. "age"
    4. Error
    Answer : B
    Explain : When we call key in dictionary it gives us value.
  4. Which method is used to add a new key-value pair to a dictionary ?
    1. add()
    2. insert()
    3. append()
    4. update()
    Answer : D
    Explain : To add key and value pair to a dictionary we have to use update() function.
  5. What will be the output of following code :
    a={1:10,2:10,3:"string"}
    b={4:"hello"}
    a.update(b)
    print(a)
    
    
    1. {4:"hello",1:10,2:10,3:"string"}
    2. {1: 10, 2: 10, 3: 'string', 4: 'hello'}
    3. Error
    4. None of above
    Answer : B
    Explain : update() function updates the a dictionary. So now the dictionary a becomes {1: 10, 2: 10, 3: 'string', 4: 'hello'}.
  6. What will be the output of the following code ?
    my_dict = {"apple": 3, "banana": 5, "cherry": 2}
    my_dict["apple"] = 6
    print(my_dict["apple"])
    
    
    1. 3
    2. 5
    3. 6
    4. Error
    Answer : C
    Explain : key - 'apple' changes it's value to 6.
  7. Which of the following methods is used to remove a key-value pair from a dictionary ?
    1. remove()
    2. delete()
    3. pop()
    4. discard()
    Answer : C
    Explain : pop() function is used to remove key-value pair in a dictionary.
  8. Dictionaries in Python are:
    1. Ordered
    2. Immutable
    3. Indexed by integers
    4. Unordered
    Answer : D
    Explain : Dictionary is an unorder collection of items. It is mutable in nature which means it can be modified after create.
  9. Solve the given code :
    d1 = {"a":40, "b":45}
    d2 = {"a":40, "b":45}
    print(d1 < d2)
    
    
    1. True
    2. False
    3. Equal
    4. Error
    Answer : D
    Explain : Error because in dictionary we don't use comparsion operator.
  10. Which of the following methods returns a list of all the keys in a dictionary ?
    1. get_keys()
    2. keys()
    3. all_keys()
    4. dict_keys()
    Answer : B
    Explain : keys() function is used to get key and value() function is used to get key value.
  11. What will be the output of the following code ?
    my_dict = {"a": 1, "b": 2, "c": 3}
    del my_dict["b"]
    print(my_dict)
    
    
    1. {"a": 1, "c": 3}
    2. {"a": 1, "b": 2}
    3. {"b": 2, "c": 3}
    4. {"a": 1, "b": 2, "c": 3}
    Answer : A
    Explain : del my_dict["b"] means delete the key and key value both.
  12. What will be the output of the following code ?
    count={}
    count[(1,2,3)] = 5
    count[(3,2,1)] = 7
    count[(1,2)] = 6
    print(count)
     
    
    1. {}
    2. {(1, 2, 3): 5, (3, 2, 1): 7, (1, 2): 6}
    3. {(1, 2, 3): 5} {(3, 2, 1): 7} {(1, 2): 6}
    4. Error
    Answer : B
    Explain : dictname[key]= key value, at the end the dictionary made {(1, 2, 3): 5, (3, 2, 1): 7, (1, 2): 6
  13. What will be the output of following code ?
    dict ={}
    dict[5]= 5
    dict[6]=(5,6,7)
    print(dict[6][0])
    
    1. (5,6,7)
    2. 5
    3. {5:5, 6: (5,6,7)}
    4. Error
    Answer : B
    Explain : dict[key][index value] so we key value 6 , we get index of 0 is 5.
  14. In which data type, indexing is not valid ?
    1. list
    2. dictionary
    3. string
    4. None of above
    Answer : B
    Explain : In the dictionary, there are no index values. The dictionary is a comma-separated collection of key: value pairs enclosed in curly brackets.
  15. What is the output of the following code ?
    a = {1: 'A', 2: 'B', 3: 'C'}
    b = {4: 'D', 5: 'E'}
    a.update(b)
    print(a)
    1. {1: 'A', 2: 'B', 3: 'C'}
    2. {1: 'A', 2: 'B', 3: 'C', 4: 'D', 5: 'E'}
    3. Error
    4. {4: 'D', 5: 'E'}
    Answer : B
    Explain : a.update(b) means- a update with b data.

Next Set

2