Multiple choice questions on lists and loops

Feb 11, 2013 • Luis Figueira

Hi all,

here are a couple of multiple choice questions on lists. I had a bit of a hard time finding questions that only deal with a single subject. I’m also not completely sure that the possible “diagnosis” are the most accurate.

Let me know what you think!

Question 1 — Consider the following sequence of commands:

`

list_a = [“a”,”b”,”c”,”d”]
list_b = []
while len(list_a) >= len(list_b) :
list_b.append(list_a.pop())
`

What will the contents of both list_a and list_b be in the end of the while loop?

  1. list_a = [“a”], list_b = [“d”, “c”, “b”] (Correct)
  2. list_a = [“a”, “b”], list_b =[“d”, “c”] (Wrong — did not understand the while stopping condition)
  3. list_a = [“a”, “b”, “c”, “d”], list_b = [“a”, “b”, “c”, “d”] (Wrong — does not understand pop?)
  4. list_a = [””], list_b = [“d”, “c”, “b”, “a”] (Wrong — did not understand the while stopping condition??)

**Question 2 — Consider the following sequence of commands: **
`

list_a = [“a”, “b”, “c”, “d”]
list_b = list_a
list_a.pop(len(list_b) -1);
`
What will the output of the following command be?
<br /> print list_a.pop(len(list_b) -1)<br />

  1. c (correct option)
  2. d (Wrong answer — probably assuming that removed the item number 3 both times)
  3. b (Wrong answer — recognising that len(list_b) is 3, but assuming that the array index starts from 1)
  4. Python will return IndexError: pop index out of range (Wrong answer: probably assuming that len(list_b) is 4, when in fact it is 3)