Peer Instruction: Files Multiple Choice QuestionsΒΆ
- [['a', 'b'], ['c', 'd', 'e'], ['f']]
- Try again. Python is passed by object reference. Therefore, anything changes in the small list will be reflected in the big list. The final small list is ['a', 'b', 'c', 'd', 'e', 'f']. There are two blanks in the file, therefore the small list is appended to the big list twice.
- [['a', 'b'], ['c', 'd', 'e']]
- Try again. Python is passed by object reference. Therefore, anything changes in the small list will be reflected in the big list. The final small list is ['a', 'b', 'c', 'd', 'e', 'f']. There are two blanks in the file, therefore the small list is appended to the big list twice.
- [['a', 'b', 'c', 'd', 'e', 'f'], ['a', 'b', 'c', 'd', 'e', 'f']]
- Correct! Python is passed by object reference. Therefore, anything changes in the small list will be reflected in the big list.
- Nothing returned; infinite loop
- Try again. The loop will end after all lines in the fine are read.
Q-1:
def build_grocery_list(f):
line = f.readline()
big = []
small = []
while line:
if line != '\n':
small.append(line.strip())
else:
big.append(small)
line = f.readline()
return big
Here is a sample file. <blank> means that the line is blank.
a
b
<blank>
c
d
e
<blank>
f
What does the function return for the sample file?
- [['a', 'b'], ['c', 'd', 'e'], ['f']]
- Try again. When the line 'f' is read, the list small turns into 'f' but it is not appended to the big list.
- [['a', 'b'], ['c', 'd', 'e']]
- Correct! When the line 'f' is read, the list small turns into 'f' but it is not appended to the big list.
- [['a', 'b', 'c', 'd', 'e', 'f'], ['a', 'b', 'c', 'd', 'e', 'f'], ['a', 'b', 'c', 'd', 'e', 'f']]
- Try again. After small list is given to a new value [ ], the small list does not point to the list saved into the list big anymore. Therefore, the change does not change the value of the previous small list saved in the big list.
- Nothing returned; infinite loop
- Try again. The loop will end after all lines in the fine are read.
Q-2:
def build_grocery_list(f):
line = f.readline()
big = []
small = []
while line:
if line != '\n':
small.append(line.strip())
else:
big.append(small)
small = []
line = f.readline()
return big
Here is a sample file. <blank> means that the line is blank.
a
b
<blank>
c
d
e
<blank>
f
What does the function return for the sample file?
- [['a', 'b'], ['c', 'd', 'e'], ['f']]
- Correct! When the line 'f' is read, the list small turns into 'f'. And the small list is appended to the big list after the loop ends. So 'f' is appended.
- [['a', 'b'], ['c', 'd', 'e']]
- Try again. When the line 'f' is read, the list small turns into 'f'. And the small list is appended to the big list after the loop ends. So 'f' is appended.
- [['a', 'b', 'c', 'd', 'e', 'f'], ['a', 'b', 'c', 'd', 'e', 'f'], ['a', 'b', 'c', 'd', 'e', 'f']]
- Try again. After small list is given to a new value [ ], the small list does not point to the list saved into the list big anymore. Therefore, the change does not change the value of the previous small list saved in the big list.
- Nothing returned; infinite loop
- Try again. The loop will end after all lines in the fine are read.
Q-3:
def build_grocery_list(f):
line = f.readline()
big = []
small = []
while line:
if line != '\n':
small.append(line.strip())
else:
big.append(small)
small = []
line = f.readline()
big.append(small)
return big
Here is a sample file. <blank> means that the line is blank.
a
b
<blank>
c
d
e
<blank>
f
What does the function return for the sample file?
- [['a', 'b'], ['c', 'd', 'e'], ['f']]
- Try again. In the inner loop, after all lines are read, it would move to an empty string. Since it does not equal to '/n', the loop keeps running.
- [['a', 'b'], ['c', 'd', 'e']]
- Try again. In the inner loop, after all lines are read, it would move to an empty string. Since it does not equal to '/n', the loop keeps running.
- [['a', 'b', 'c', 'd', 'e', 'f'], ['a', 'b', 'c', 'd', 'e', 'f'], ['a', 'b', 'c', 'd', 'e', 'f']]
- Try again. In the inner loop, after all lines are read, it would move to an empty string. Since it does not equal to '/n', the loop keeps running.
- Nothing returned; infinite loop
- Correct! In the inner loop, after all lines are read, it would move to an empty string. Since it does not equal to '/n', the loop keeps running.
Q-4:
def build_grocery_list(f):
big = []
line = f.readline()
while line:
small = []
while line != '\n':
small.append(line.strip())
line = f.readline()
big.append(small)
line = f.readline()
return big
Here is a sample file. <blank> means that the line is blank.
a
b
<blank>
c
d
e
<blank>
f
What does the function return for the sample file?
You have attempted of activities on this page