Programming and Problem Solving Through Python Language Theory Question

List in Python

  1. Which of the following data types is mutable in Python ?
    1. int
    2. str
    3. list
    4. tuple
    Answer : B
    Explain : List is mutable which means that item in a list can be modify.
  2. What is the output of the following code snippet ?
    my_list = [1, 2, 3, 4, 5]
    result = my_list[1:3]
    print(result)
    
    1. [1, 2]
    2. [2, 3]
    3. [2, 3, 4]
    4. [1, 3]
    Answer : B
    Explain : In this silicing is done. So element which index value 1 and 2 give as result and 3 is stop value.
  3. What will be the output of following code ?
    x =['XX', 'YY']
    for i in x:
        i.lower()
    print(x)
    
    
    1. ['XX', 'YY']
    2. ['xx', 'yy']
    3. [XX, YY]
    4. None of the above
    Answer : A
    Explain : Because i.upper() value does not store anywhere.
  4. How do you add an element to the end of a list ?
    1. list.append(element)
    2. list.insert(-1, element)
    3. list.extend(element)
    4. list.add(element)
    Answer : A
    Explain : With use of list.append(element) we add element in list at the end.
  5. What is the output of following code ?
    a=[[1,2,3],[4,5,6],[7,8,9]]
    print(a[1][:])
    
    1. [1,2,3]
    2. [4,5,6]
    3. [2,5,8]
    4. None of the above
    Answer : B
    Explain : It takes value list a which index value [1]. Here[:] it take full list because of not stop value.
  6. What is the data type of following object ?
    a= [5,'abc',3.2,6]
    1. tuple
    2. array
    3. list
    4. dictionary
    Answer : C
    Explain : List is the data type of following object.
  7. What is the result of the following code ?
    my_list = [1, 2, 3]
    my_list.insert(1, 4)
    print(my_list)
    
    
    1. [4, 2, 3]
    2. [1, 2, 4]
    3. [1, 4, 2, 3]
    4. [1, 4, 3]
    Answer : D
    Explain : In this my_list.insert(1, 4) , 1 is index value where as 4 is value. Just switch it and you will get answer.
  8. How can you remove an element from a list by its value ?
    1. list.remove(value)
    2. list.pop(value)
    3. list.del(value)
    4. list.exclude(value)
    Answer : A
    Explain : remove() - if element is known not index value.
  9. What does the list.sort() method do ?
    1. It reverses the order of elements in the list.
    2. It shuffles the elements randomly within the list.
    3. It sorts the elements of the list in decending order.
    4. It sorts the elements of the list in ascending order.
    Answer : D
    Explain : By default sort() sorts element in ascending order.
  10. Assume, q=[3,4,5,20,5,25,1,3], then what will be the items q list after q.pop(1)
    1. [3,4, 5, 20, 5, 25, 1, 3]
    2. [1,3,3, 5, 20, 5, 25, 1, 3]
    3. [3, 5, 20, 5, 25, 1, 3]
    4. [1, 5, 20, 5, 25, 1, 3]
    Answer : C
    Explain : q.pop(1) item delete which index value is 1
  11. How do you find the index of an element in a list ?
    1. list.get_index(element)
    2. list.index(element)
    3. list.locate(element)
    4. list.find_index(element)
    Answer : B
    Explain : index() function returns the index of first matched item from list.
  12. What is the output of the following code ?
    my_list = [1, 2, 3]
    new_list = my_list + [4, 5]
    print(new_list)
    
    1. [1, 2, 3, 4, 5]
    2. [1, 2, 3, [4, 5]]
    3. [1, 2, 3, [1, 2, 3, 4, 5]]
    4. [4, 5, 1, 2, 3]
    Answer : A
    Explain : [4,5] add to my_list only number not brackets.
  13. How do you reverse the elements of a list ?
    1. list.reorder()
    2. list.flip()
    3. list.sort()
    4. list.reverse()
    Answer : D
    Explain : list.reverse() is used to reverse the list.
  14. How can you clear all elements from a list ?
    1. list.clear()
    2. list.remove_all()
    3. list.delete_all()
    4. list.empty()
    Answer : A
    Explain : clear() function clear all elements in list.
  15. How can you create a copy of a list original_list ?
    1. new_list = original_list.copy()
    2. new_list = list(original_list)
    3. new_list = original_list[:]
    4. All of the above
    Answer : D
    Explain : We can use all of above to copy original list to make a new one. You should try to make you clear.

Next Set

2