Which of the following are true of Python lists Mcq?

Python List MCQs

This section focuses on the "list" in Python. These Multiple Choice Questions (mcq) should be practiced to improve the Python skills required for various interviews (campus interview, walk-in interview, company interview), placement, entrance exam and other competitive examinations.

1.Which of the following would give an error?

A. list1=[]
B. list1=[]*3
C. list1=[2,8,7]
D. None of the above

View Answer

Ans : D

Explanation: None of the above will result in error.


2.Which of the following is True regarding lists in Python?

A. Lists are immutable.
B. Size of the lists must be specified before its initialization
C. Elements of lists are stored in contagious memory location.
D. size(list1) command is used to find the size of lists.

View Answer

Ans : C

Explanation: Elements of lists are stored in contagious memory location is True regarding lists in Python.


3.What will be the output of below Python code?

list1=[8,0,9,5]
print(list1[::-1])

A. [5,9,0,8]
B. [8,0,9]
C. [8,0,9,5]
D. [0,9,5]

View Answer

Ans : A

Explanation: [5,9,0,8] will be the output of below Python code.


4.Which of the following will give output as [23,2,9,75] ?

If list1=[6,23,3,2,0,9,8,75]

A. print(list1[1:7:2])
B. print(list1[0:7:2])
C. print(list1[1:8:2])
D. print(list1[0:8:2])

View Answer

Ans : C

Explanation: print(list1[1:8:2]) of the following will give output as [23,2,9,75].


5.The marks of a student on 6 subjects are stored in a list, list1=[80,66,94,87,99,95]. How can the student's average mark be calculated?

A. print(avg(list1))
B. print(sum(list1)/len(list1))
C. print(sum(list1)/sizeof(list1))
D. print(total(list1)/len(list1))

View Answer

Ans : B

Explanation: the student's average mark be calculated through print(sum(list1)/len(list1)).


6.What will be the output of following Python code?

list1=["Python","Java","c","C","C++"]
print(min(list1))

A. c
B. C++
C. C
D. min function cannot be used on string elements

View Answer

Ans : C

Explanation: C will be the output of following Python code.


7.The elements of a list are arranged in descending order. Which of the following two will give same outputs?
i. print(list_name.sort())
ii. print(max(list_name))
iii. print(list_name.reverse())
iv. print(list_name[-1])

A. i, ii
B. i, iii
C. ii, iii
D. iii, iv

View Answer

Ans : B

Explanation: print(list_name.sort()) and print(list_name.reverse()) will give same outputs.


8. What will be the result after the execution of above Python code?

list1=[3,2,5,7,3,6]
list1.pop(3)
print(list1)

A. [3,2,5,3,6]
B. [2,5,7,3,6]
C. [2,5,7,6]
D. [3,2,5,7,3,6]

View Answer

Ans : A

Explanation: [3,2,5,3,6] will be the result after the execution of above Python code.


9.What will be the output of below Python code?

list1=[1,3,5,2,4,6,2]
list1.remove(2)
print(sum(list1))

A. 18
B. 19
C. 21
D. 22

View Answer

Ans : C

Explanation: 21 will be the result after the execution of above Python code.


10.What will be the output of below Python code?

list1=["tom","mary","simon"]
list1.insert(5,8)
print(list1)

A. ["tom", "mary", "simon", 5]
B. ["tom", "mary", "simon", 8]
C. [8, "tom", "mary", "simon"]
D. Error

View Answer

Ans : B

Explanation: ["tom", "mary", "simon", 8] will be the result after the execution of above Python code.






Discussion



* You must be logged in to add comment.

vanur swamy

it is very easy to understand


Q1. Which of the following statement will create list?

a. L1=list( )

b. L1=[1,2,3,4]

c. Both of the above

d. None of the above

Ans. c. Both of the above

9.16. Multiple Choice Questions¶

    Q-1: What is returned by the following function?

    def slice_exercise(): alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False] print(alist[2:4])

  • [ [ ], 3.14, False]
  • Incorrect! This slice starts at index 4 and goes up to and includes the last item.
  • ["cat", [56, 57, "dog"]]
  • Correct! The word "cat" is at index 2 and [56, 57, "dog"] is what you get when index 4 is exclusive.
  • [ [56, 57, "dog"], [ ], 3.14, False]
  • Incorrect! Remember that index values start at 0!
  • [27, "cat"]
  • Incorrect! This slice starts at index 4 and goes up to and includes the last item.

    Q-2: What is returned by the following function?

    def len_of_list(): alist = [3, 67, "cat", 3.14, False] return len(alist)

  • 4
  • Incorrect! The built in function, len, returns the actual number of items in the list, not the maximum index value.
  • 5
  • Correct! There are 5 items in this list.
  • False
  • Incorrect! The built in function, len, returns the actual number of items in the list.
  • 3.14
  • Incorrect! The built in function, len, returns the actual number of items in the list.

    Q-3: What is returned by the following function?

    def indexing_and_upper(): alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False] return alist[2].upper()

  • Error, you cannot use the upper method on a list.
  • Incorrect! alist[2] is the string cat so the upper method is legal
  • 2
  • Incorrect! 2 is the index. We want the item at that index.
  • CAT
  • Correct! The string cat is upper cased to become CAT.
  • FALSE
  • Incorrect! False is at the maximum index, not the second index.

    Q-4: What is returned by the following function?

    def list_within_list(): alist = [3, 67, "cat", [56, 57, "dog"], [ ], 3.14, False] return alist[2][0]

  • 56
  • Indexes start with 0, not 1.
  • c
  • Yes, the first character of the string at index 2 is c
  • cat
  • cat is the item at index 2 but then we index into it further.
  • Error, you cannot have two index values unless you are using slicing.
  • Using more than one index is fine. You read it from left to right.

    Q-5: What is returned by the following function?

    def list_transformation(): alist = [4, 2, 8, 6, 5] blist = [alist] * 2 alist[3] = 999 return blist

  • [4, 2, 8, 999, 5, 4, 2, 8, 999, 5]
  • [alist] * 2 creates a list containing alist repeated 2 times
  • [[4, 2, 8, 999, 5], [4, 2, 8, 999, 5]]
  • Yes, blist contains two references, both to alist.
  • [4, 2, 8, 6, 5]
  • print(blist)
  • [[4, 2, 8, 999, 5], [4, 2, 8, 6, 5]]
  • blist contains two references, both to alist so changes to alist appear both times.

    Q-6: What is returned by the following function?

    def list_transformation(): alist = [4, 2, 8, 6, 5] blist = [ ] for item in alist: blist.append(item+5) return blist

  • [4, 2, 8, 6, 5]
  • 5 is added to each item before the append is performed.
  • [4, 2, 8, 6, 5, 5]
  • There are too many items in this list. Only 5 append operations are performed.
  • [9, 7, 13, 11, 10]
  • Yes, the for loop processes each item of the list. 5 is added before it is appended to blist.
  • Error, you cannot concatenate inside an append.
  • 5 is added to each item before the append is performed.

    Q-7: Which method would you use to figure out the position of an item in a list?

  • .pop()
  • pop removes and returns items (default is to remove and return the last item in the list)
  • .insert()
  • insert will add an item at whatever position is specified.
  • .count()
  • count returns the number of times something occurs in a list
  • .index()
  • Yes, index will return the position of the first occurrence of an item.

    Q-8: Which method is best to use when adding an item to the end of a list?

  • .insert()
  • While you can use insert, it is not the best method to use because you need to specify that you want to stick the new item at the end.
  • .pop()
  • pop removes an item from a list
  • .append()
  • Yes, though you can use insert to do the same thing, you don't need to provide the position.
  • .remove()
  • remove gets rid of the first occurrence of any item that it is told. It does not add an item.

    Q-9: Given that we want to accumulate the total sum of a list of numbers, which of the following accumulator patterns would be appropriate?

      def find_sum(): nums = [4, 5, 2, 93, 3, 5] s = 0 for n in nums: s = s + 1 return s

        def find_sum(): nums = [4, 5, 2, 93, 3, 5] s = 0 for n in nums: s = n + n return s

          def find_sum(): nums = [4, 5, 2, 93, 3, 5] s = 0 for n in nums: s = s + n return s

        1. I.
        2. This pattern will only count how many items are in the list, not provide the total accumulated value.
        3. II.
        4. This would reset the value of s each time the for loop iterated, and so by the end s would be assigned the value of the last item in the list plus the last item in the list.
        5. III.
        6. Yes, this will solve the problem.
        7. none of the above would be appropriate for the problem.
        8. One of the patterns above is a correct way to solve the problem.

          Q-10: Given that we want to accumulate the total number of strings in the list, which of the following accumulator patterns would be appropriate?

            def num_of_strings(): lst = ['plan', 'answer', 5, 9.29, 'order, items', [4]] s = 0 for n in lst: s = s + n return s

              def num_of_strings(): lst = ['plan', 'answer', 5, 9.29, 'order, items', [4]] for item in lst: s = 0 if isinstance(item, str): s = s + 1 return s

                def num_of_strings(): lst = ['plan', 'answer', 5, 9.29, 'order, items', [4]] s = "" for n in lst: s = s + n return s

                  def num_of_strings(): lst = ['plan', 'answer', 5, 9.29, 'order, items', [4]] s = 0 for item in lst: if isinstance(item, str): s = s + 1

                1. 1.
                2. How does this solution know that the element of lst is a string and that s should be updated?
                3. 2.
                4. What happens to s each time the for loop iterates?
                5. 3.
                6. Reread the prompt again, what do we want to accumulate?
                7. 4.
                8. Yes, this will solve the problem.
                9. none of the above would be appropriate for the problem.
                10. One of the patterns above is a correct way to solve the problem.

                  Q-11: What will the following code print?

                  def mystery(num_list): index = 0 while index < len(num_list): num = num_list[index] if num == 0: num_list.pop(index) index += 1 list1 = [3, 0, 2, 0, 0] mystery(list1) print(list1)

                • [3, 0, 2, 0, 0]
                • The list is modified by the pop.
                • [3, 0, 2]
                • It will pop 0's except for the last one.
                • [3, 2]
                • This would be true if it didn't always increment the index.
                • [3, 2, 0]
                • Since it always increments the index it will miss the last zero.

                  Q-12: What will the following code print?

                  def mystery(num_list): sum = 0 for i in range(0, len(num_list), 2): num = num_list[i] sum += num return sum list1 = [1, 2, 3, 4, 5] print(mystery(list1))

                • 1
                • It adds 1 to the sum and then returns.
                • 9
                • This would be true if it didn't return until after the loop finished
                • 15
                • This would be true if it didn't return until after the loop finished and the range incremented by 1 rather than 2
                • None
                • This would be true if there wasn't a return statement

                  Q-13: What will the following code print?

                  def mystery(num_list): for num in num_list: if num < 0: return False else: return True print(mystery([3, -1, 2]))

                • True
                • It returns true after checking the first num.
                • False
                • This would be true if the first number in the list was negative.
                • None
                • This would be true if there wasn't a return statement.
                • It will not compile
                • This would be true if there was a syntax error.

                  Q-14: What will the following code print?

                  def mystery(num_list): out = [] for num in num_list: if num > 10: out.append(num) return out print(mystery([5, 10, 15, 20]))

                • [10, 15, 20]
                • It only adds numbers that are greater than 10
                • [20, 15]
                • This would be true if append added at the front, but it adds at the end
                • [15, 20]
                • It adds all numbers greater than 10 in order.
                • [20, 15, 10]
                • This would be true if append added at the front, but it adds at the end and it won't add the 10

                  Q-15: What will the following code print?

                  def mystery(num_list): out = [] for i in range(len(num_list) - 1,0,-1): num = num_list[i] out.append(num) return out print(mystery([5, 10, 15, 20]))

                • [5, 10, 15, 20]
                • It adds the numbers in reverse order
                • [20, 15, 10, 5]
                • This would be true if the end for range was less than 0
                • [5, 10, 15]
                • It adds the numbers in reverse order
                • [20, 15, 10]
                • It adds the numbers in reverse order, but stops before the item at index 0.

                Python Lists and Tuples Quiz

                Interactive Quiz ⋅ 11 Questions
                By John Sturtz

                Test your understanding of Python lists and tuples.

                Take this quiz after reading our Lists and Tuples in Python tutorial.

                The quiz contains 11 questions and there is no time limit. You’ll get 1 point for each correct answer. At the end of the quiz, you’ll receive a total score. The maximum score is 100%. Good luck!

                Start the Quiz »

                « Browse All Python Quizzes