A. [39 points] Consider the following script*, and then answer the questions. Pu
ID: 3913022 • Letter: A
Question
A. [39 points] Consider the following script*, and then answer the questions. Put your answers into a file named hw07-answers.txt secondList -firstList thirdList_ ['p', 'y', 't', firstList.append 's') del secondList[3] thirdList . remove ( ·h' ) firstList[2,0,1,7,10,5] firstList[3:1 [0,0] secondList.clear () 'h', 'o', ' n'] 8 Note:the line numbers on the left side are not part of the script, they're just for reference 1. After line 4, what value is stored in the variable firstList and why? 2. After line 4, what value is stored in the variable secondList and why? 3. After line 4, what value is stored in the variable thirdList and why? 4. After line 5, what value is stored in the variable firstList and why? 5. After line 5, what value is stored in the variable secondList and why? 6. After line 5, what value is stored in the variable thirdList and why? 7. After line 6, what value is stored in the variable firstList and why? 8. After line 6, what value is stored in the variable secondList and why? 9. After line 6, what value is stored in the variable thirdList and why? 10. After line 7, what value is stored in the variable firstList and why? 11. After line 7, what value is stored in the variable secondList and why? 12. After line 8, what value is stored in the variable firstList and why? 13. After line 9, what value is stored in the variable secondList and why?Explanation / Answer
1)
ans = ['P', 'y', 't', 'h', 'o', 'n', 's']
Reason :- firstList.append('s') will add character 's' to the end of the list
2)
ans = ['P', 'y', 't', 'h', 'o', 'n', 's']
Reason :- secondList = firstList is done by copy-by-reference .So,any changes in firstList will be done in secondList and vice-versa
3)
ans = ['P', 'y', 't', 'h', 'o', 'n']
Reason :- thirdList is intialised by these values and no change is made separately
4)
ans = ['P', 'y', 't', 'h', 'n', 's']
Reason :- as del secondList[3] deletes the element with index 3 in the list.And,as the secondList and firstList shares the same reference .So,now changes will be made in both lists
5)
ans = ['P', 'y', 't', 'h', 'n', 's']
Reason :- as del secondList[3] deletes the element with index 3 in the list.And,as the secondList and firstList shares the same reference .So,now changes will be made in both lists
6)
ans = ['P', 'y', 't', 'h', 'o', 'n']
Reason :- thirdList is intialised by these values and no change is made separately
7)
ans = ['P', 'y', 't', 'h', 'n', 's']
Reason :- no change is done after line no. 5
8)
ans = ['P', 'y', 't', 'h', 'n', 's']
Reason :- Reason :- no change is done after line no. 5
9)
ans :- ['P', 'y', 't', 'o', 'n']
Reason :- thirdList.remove('h') will remove element with character 'h' from the thirdList only
10)
ans = [2, 0, 1, 7, 10, 5]
Reason :- New reference is provided to firstList . So, firstList gets a new reference and it won't effect secondList reference anymore
11)
ans = ['P', 'y', 't', 'h', 'n', 's']
Reason :- Now, firstList and secondList are referreing to different Lists so both are independent of each other
12)
ans = [2, 0, 1, 0, 0]
Reason :- firstList[3:] = [0,0] will replace the list of FirstList with [0,0] but from index no. 3
Therefore,
[2, 0, 1, 7, 10, 5] -> [2, 0, 1, 0, 0]
13)
ans = []
Reason :- secondList.clear() will clear the list completely
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.