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:
<br />
>>> list_a = ["a","b","c","d"]<br />
>>> list_b = []<br />
>>> while len(list_a) >= len(list_b) :<br />
>>> list_b.append(list_a.pop())<br />
What will the contents of both list_a and list_b be in the end of the while loop?
- list_a = [“a”], list_b = [“d”, “c”, “b”] (Correct)
- list_a = [“a”, “b”], list_b =[“d”, “c”] (Wrong – did not understand the while stopping condition)
- list_a = [“a”, “b”, “c”, “d”], list_b = [“a”, “b”, “c”, “d”] (Wrong – does not understand pop?)
- list_a = [“”], list_b = [“d”, “c”, “b”, “a”] (Wrong – did not understand the while stopping condition??)
**Question 2 – Consider the following sequence of commands: **
<br />
>>> list_a = ["a", "b", "c", "d"]<br />
>>> list_b = list_a<br />
>>> list_a.pop(len(list_b) -1);<br />
What will the output of the following command be?
<br />
print list_a.pop(len(list_b) -1)<br />
- c (correct option)
- d (Wrong answer – probably assuming that removed the item number 3 both times)
- b (Wrong answer – recognising that len(list_b) is 3, but assuming that the array index starts from 1)
- 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)