What is the value of xs[-1] after this code is executed? xs = [4, 5, 6] xs = xs
ID: 3815388 • Letter: W
Question
What is the value of xs[-1] after this code is executed? xs = [4, 5, 6] xs = xs + [2] xs = xs + 2 a) 0 b) 6 c) 4 d) 12 e) 2*** What does the following expression evaluate to? list range (1, 11, 2)) (2:] a) [5, 7, 9] b)[3, 5, 7, 9) c) [3, 5, 7] d) [3, 5, 7, 9, 11] e) [1, 3, 5, 7] What will funA ([2, 3]) return? deaf funA (a); for x in range (3); a. append (funB (a)) return a def fun B (y): return y[-2] + y[-1] a) [2, 3, 4, 5, 6] b) [2, 3, 5, 8] c) [2, 5, 8, 13]*** d) [2, 3, 5, 10, 20] e) [2, 3, 5, 10]Explanation / Answer
7) xs = [4, 5, 6]
xs = xs + [2]
xs = xs*2
print xs[-1]
Answer is c : 2
First line : initalizes the list xs with items [4, 5, 6]
second line : adds 2 to the list xs i.e [4,5,6,2]
third line : replicate the list items two times i.e xs = [4,5,6,2,4,5,6,2]
fourth line : xs[-1] will print the last item in the list i.e 2
9)
def funA(a):
for x in range(3):
a.append(funB(a))
return a
def funB(y):
return y[-2] + y[-1]
b= funA([2,3])
print b
Answer is c = [2, 3, 5, 8, 13]
seventh line : we call funA([2,3]) i.e we pass list [2,3] to fucntion funA and store the return value in b
first line : it is the defination of funA where a is list having items [2,3]
second line : for loop will execute 3 times
Loop 1:
third line : we will append the return value from funB to the list a .so we pass [2,3] in funB.
fifth line : it will add the second last and last item in the list and return it i. e 2+3 =5
third line : now 5 will be appeneded to the list a i.e a= [2, 3,5]
Loop 2:
third line : we will append the return value from funB to the list a .so we pass [2,3,5] in funB.
fifth line : it will add the second last and last item in the list and return it i. e 3+5 =8
third line : now 8 will be appeneded to the list a i.e a= [2, 3,5,8]
Loop 3:
third line : we will append the return value from funB to the list a .so we pass [2,3,5,8] in funB.
fifth line : it will add the second last and last item in the list and return it i. e 5+8 =13
third line : now 13 will be appeneded to the list a i.e a= [2, 3,5,8,13]
so now b will have [2, 3, 5, 8, 13]
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.