CBSERanker

Loading

Worksheet on python Lists

Worksheet on python Lists

  1. How can you create an empty list in Python?
    • A) list = []
    • B) list = {}
    • C) list = ()
    • D) list = //
  2. Which function is used to add an element at the end of the list?
    • A) push()
    • B) append()
    • C) insert()
    • D) extend()
  3. What does the len() function return when used with a list?
    • A) The first element of the list
    • B) The last element of the list
    • C) The number of elements in the list
    • D) A boolean value indicating if the list is empty
  4. Which method inserts an item at a specified index in a list?
    • A) append()
    • B) extend()
    • C) insert()
    • D) add()
  5. How can you remove the last item from a list and return it?
    • A) remove()
    • B) pop()
    • C) delete()
    • D) cut()
  6. What does the count() method do?
    • A) Counts the total memory bytes consumed by the list
    • B) Returns the number of times a specified element appears in the list
    • C) Counts the number of sublists within the list
    • D) Returns the total number of characters in all the elements of the list
  7. Which method would you use to reverse the order of elements in a list in place?
    • A) reverse()
    • B) reversed()
    • C) invert()
    • D) backwards()
  8. If you want to find the largest number in a list of integers, which function would you use?
    • A) max()
    • B) min()
    • C) sum()
    • D) sort()
  9. What is the result of list("hello")?
    • A) ['h', 'e', 'l', 'l', 'o']
    • B) ['hello']
    • C) ['h', 'e', 'l', 'o']
    • D) ['e', 'l']
  10. How do you check if two lists l1 and l2 are equal?
  • A) l1 == l2
  • B) l1.equals(l2)
  • C) compare(l1, l2)
  • D) l1 and l2
  1. Which method adds elements of a list to another list?
  • A) append()
  • B) extend()
  • C) insert()
  • D) add()
  1. What will mylist[2:5] return if mylist = [10, 20, 30, 40, 50, 60]?
  • A) [30, 40, 50]
  • B) [20, 30, 40]
  • C) [30, 40]
  • D) [20, 30, 40, 50]
  1. What does the sort() method do to the list?
  • A) Sorts the list in descending order by default
  • B) Sorts the list in ascending order by default
  • C) Shuffles the list randomly
  • D) Reverses the list
  1. Which line of code will cause an error?
  • A) nums = list("1234")
  • B) nums = list(1234)
  • C) nums = [1, 2, 3, 4]
  • D) nums = list((1, 2, 3, 4))
  1. Which of the following is not a valid list?
  • A) a = [1, 2, 3]
  • B) b = (1, 2, 3)
  • C) c = [1, [2, 3], [4, 5]]
  • D) d = []
  1. How do you access the first element of a list named ‘items’?
  • A) items(0)
  • B) items[1]
  • C) items[0]
  • D) first(items)
  1. Which function will give the total sum of the numeric list numbers?
  • A) sum(numbers)
  • B) total(numbers)
  • C) add(numbers)
  • D) summarize(numbers)
  1. What will be the index of the first occurrence of 2 in the list [1, 2, 3, 2, 2]?
  • A) 0
  • B) 1
  • C) 2
  • D) 3
  1. Which list method is used to remove an item by its value?
  • A) delete()
  • B) remove()
  • C) pop()
  • D) discard()
  1. How can you combine two lists list1 and list2?
  • A) list1 + list2
  • B) list1.append(list2)
  • C) list1.extend(list2)
  • D) list1 and list2

  1. What does mylist[:3] return if mylist = [0, 1, 2, 3, 4, 5]?
  • A) [0, 1, 2]
  • B) [1, 2, 3]
  • C) [0, 1, 2, 3]
  • D) [1, 2, 3, 4]
  1. How can you slice a list to return every second item from the list mylist?
  • A) mylist[::2]
  • B) mylist[2::]
  • C) mylist[:2]
  • D) mylist[2]
  1. What will ['a', 'b', 'c', 'd', 'e'][1:-1] produce?
  • A) ['b', 'c', 'd']
  • B) ['a', 'b', 'c', 'd']
  • C) ['c', 'd', 'e']
  • D) ['b', 'c']
  1. If data = [10, 20, 30, 40, 50], what will data[-2:] return?
  • A) [30, 40, 50]
  • B) [40, 50]
  • C) [10, 20]
  • D) [20, 30]
  1. To reverse the list using slicing, what should you do?
  • A) list[::-1]
  • B) list[1::-1]
  • C) list[::-2]
  • D) list[1::1]
  1. Which slice operation returns the sublist without the first and last elements?
  • A) mylist[1:-1]
  • B) mylist[:-1]
  • C) mylist[1:]
  • D) mylist[::1]
  1. What will mylist[3:6] return if mylist = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h']?
  • A) ['d', 'e', 'f']
  • B) ['c', 'd', 'e']
  • C) ['d', 'e', 'f', 'g']
  • D) ['e', 'f', 'g']
  1. How can you slice a list to exclude the last item?
  • A) list[:-1]
  • B) list[-1:]
  • C) list[1:-1]
  • D) list[-1]
  1. Which slicing operation includes only the first three elements of a list?
  • A) list[3:]
  • B) list[:3]
  • C) list[0:3]
  • D) list[-3:]
  1. What does list[2:-2] do?
  • A) Returns all elements except the first two and last two
  • B) Returns only the middle two elements
  • C) Returns the

Leave a Reply

Your email address will not be published. Required fields are marked *