Skip to main content

Exercises 9.16 Multiple Choice Questions

1.

    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.

2.

    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.

3.

    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.

4.

    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.

5.

    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.

6.

    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.

7.

    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.

8.

    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.

9.

    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?
    1. def find_sum():
          nums = [4, 5, 2, 93, 3, 5]
          s = 0
          for n in nums:
              s = s + 1
          return s
      
    2. def find_sum():
          nums = [4, 5, 2, 93, 3, 5]
          s = 0
          for n in nums:
              s = n + n
          return s
      
    3. def find_sum():
          nums = [4, 5, 2, 93, 3, 5]
          s = 0
          for n in nums:
              s = s + n
          return s
      
  • Pattern I
  • This pattern will only count how many items are in the list, not provide the total accumulated value.
  • Pattern II
  • 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.
  • Pattern III
  • Yes, this will solve the problem.
  • none of the above would be appropriate for the problem.
  • One of the patterns above is a correct way to solve the problem.

10.

    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?
    1. def num_of_strings():
          lst = ['plan', 'answer', 5, 9.29, 'order, items', [4]]
          s = 0
          for n in lst:
              s = s + n
          return s
      
    2. 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
      
    3. def num_of_strings():
          lst = ['plan', 'answer', 5, 9.29, 'order, items', [4]]
          s = ""
          for n in lst:
              s = s + n
          return s
      
    4. 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
      
  • Pattern 1.
  • How does this solution know that the element of lst is a string and that s should be updated?
  • Pattern 2.
  • What happens to s each time the for loop iterates?
  • Pattern 3.
  • Reread the prompt again. What do we want to accumulate?
  • Pattern 4.
  • Yes, this will solve the problem.
  • none of the above would be appropriate for the problem.
  • One of the patterns above is a correct way to solve the problem.

11.

    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.

12.

    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

13.

    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.

14.

    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

15.

    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.