Programming and Problem Solving Through Python Language Theory Question

List in Python - Set 2

  1. Solve the code :
    matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
    result = matrix[0] + matrix[1]
    print(result)
    
    1. [1, 2, 3] + [4, 5, 6]
    2. [1, 2, 3, 4, 5, 6]
    3. [[1, 2, 3], [4, 5, 6]]
    4. Error
    Answer : B
    Explain : matirx[0] is [1,2,3], matrix[1] is [4,5,6]. These both concatenation with each other and create new list.
  2. Solve the code ?
    list1 = [1, 2, 3]
    list2 = [3, 4, 5]
    result = [x * y for x in list1 for y in list2 if x + y > 5]
    print(result)
    
    1. [15, 18, 20]
    2. [5,8,10],[9,12,15]
    3. [5, 8, 10, 9, 12, 15]
    4. [9,12,15]
    Answer :C
    Explain :
    This code  uses a list comprehension to iterate over the elements of list1 and list2.
    
    For each combination of x from list1 and y from list2, then it calculates x * y and adds it to the result list if the sum of x and y is greater than 5. Breakdown of the pairs where x + y > 5: For x = 1 and y = 4, x + y = 5, which is not greater than 5. For x = 1 and y = 5, x + y = 6, which is greater than 5. So, x * y = 1 * 5 = 5 is added to the result. For x = 2 and y = 3, x + y = 5, which is not greater than 5. For x = 2 and y = 4, x + y = 6, which is greater than 5. So, x * y = 2 * 4 = 8 is added to the result. For x = 2 and y = 5, x + y = 7, which is greater than 5. So, x * y = 2 * 5 = 10 is added to the result. For x = 3 and y = 3, x + y = 6, which is greater than 5. So, x * y = 3 * 3 = 9 is added to the result. For x = 3 and y = 4, x + y = 7, which is greater than 5. So, x * y = 3 * 4 = 12 is added to the result. For x = 3 and y = 5, x + y = 8, which is greater than 5. So, x * y = 3 * 5 = 15 is added to the result. Thus, the resulting list is [5, 8, 10, 9, 12, 15]
  3. What is the output when we execute list("hello") ?
    1. ['h','e','l','l','o']
    2. ['hello']
    3. ['llo']
    4. ['olleh']
    Answer : A
    Explain : When we execute this string value convert to list value.
  4. What is the correct way to access the third element of a list 'my_list' in Python ?
    1. my_list(2)
    2. my_list[2]
    3. my_list.third
    4. my_list{3}
    Answer : B
    Explain : To access a certain item we use slicing. Syntax- listname[ start:stop:step ]
  5. What is the output of the following code ?
    l1 = [1, 2, 3]
    l2 = l1 * 3
    print(l2)
    
    
    1. [1, 2, 3, 1, 2, 3, 1, 2, 3]
    2. [1, 1, 1, 2, 2, 2, 3, 3, 3]
    3. [3, 2, 1, 3, 2, 1, 3, 2, 1]
    4. [1, 2, 3, 3, 2, 1, 1, 2, 3]
    Answer : A
    Explain : In this repeating method is use.
  6. List, String and tuple are which type of data type ?
    1. Sequence Types
    2. Binary Types
    3. Boolean Types
    4. None of the above
    Answer : A
    Explain : List, string and tuple are sequence data types. Sequence data types in Python are containers that hold an ordered collection of items or elements. Each element in a sequence is assigned an index, starting from 0 for the first element, 1 for the second element, and so on.
  7. What will be the output of the following code snippet ?
    a=[1,2,3,4,5,6,7,8,9]
    a[::2]=10,20,30,40,50
    print(a)
    1. ValueError
    2. [1, 10, 3, 20, 5, 30, 7, 40, 9, 50, 60]
    3. [10, 2, 20, 4, 30, 6, 40, 8, 50]
    4. [1, 2, 10, 20, 30, 40, 50, 60]
    Answer : C
    Explain : The value add at the index value 0,2,4,6,8.
  8. What is the correct command to shuffle the following list?
    fruit=['apple', 'banana', 'papaya', 'cherry']
    1. fruit.shuffle()
    2. shuffle(fruit)
    3. random.shuffle(fruit)
    4. random.shuffleList(fruit)
    Answer : C
    Explain : To shuffle a list in Python, we can use the shuffle() method from the random module.
  9. Which statement is correct?
    1. List is mutable & Tuple is immutable
    2. List is immutable & Tuple is mutable
    3. Both are mutable
    4. Both are immutable
    Answer : A
    Explain : List is mutable which can be modify and change after declare.
  10. colors = ["red", "green", "burnt sienna", "blue"]
    Which list index would select the value 'red' from the above list
    1. 'red'
    2. 0
    3. 1
    4. -5
    Answer : B
    Explain : The index value of red is 0.
  11. What is the output when we execute list('hello') ?
    1. ['h', 'e', 'l', 'l', 'o']
    2. ['hello']
    3. ['llo']
    4. ['olleh']
    Answer : A
    Explain : When we execute the list('hello'), it convert the string in list (every letter).
  12. 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 : i.lower() function convert the value into lower case but this value is not store any where so it return the value in x.
  13. What is the data type of following object ?
    A = [5,‟abc‟,3.2,6]
    1. tuple
    2. array
    3. list
    4. dictionary
    Answer : C
  14. What does the append() method do for lists in Python ?
    1. It removes the last element of the list.
    2. It adds an element to the end of the list.
    3. It adds an element to the beginning of the list.
    4. It reverses the order of the list.
    Answer : B
    Explain : It adds the element at the end of list.
  15. Which of the following methods is used to remove and return the last element from a list ?
    1. pop()
    2. remove()
    3. delete()
    4. pop_last()
    Answer : A
    Explain : pop() element remove the last element by default from the list.

Next Set

1