Lists

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

MISSING

LIST 1 QUESTION 5 - 10

12. What is the output of the following code? def change(var, lst): var = 1 lst[0] = 44 k = 3 a = [1, 2, 3] change(k, a) print(k) print(a) a) 3 [44, 2, 3]. b) 1 [1,2,3]. c) 3 [1,2,3]. d) 1 [44,2,3].

Answer: a Explanation: A list is mutable, hence it's value changes after function call. However, integer isn't mutable. Thus its value doesn't change.

3. Suppose listExample is ['h','e','l','l','o'], what is len(listExample)? a) 5 b) 4 c) None d) Error

Answer: a Explanation: Execute in the shell and verify.

10. What will be the output? veggies = ['carrot', 'broccoli', 'potato', 'asparagus'] veggies.insert(veggies.index('broccoli'), 'celery') print(veggies) a) ['carrot', 'celery', 'broccoli', 'potato', 'asparagus'] b) ['carrot', 'celery', 'potato', 'asparagus']. c) ['carrot', 'broccoli', 'celery', 'potato', 'asparagus']. d) ['celery', 'carrot', 'broccoli', 'potato', 'asparagus'].

Answer: a Explanation: Execute in the shell to verify.

2. Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after listExample.extend([34, 5]) ? a) [3, 4, 5, 20, 5, 25, 1, 3, 34, 5]. b) [1, 3, 3, 4, 5, 5, 20, 25, 34, 5]. c) [25, 20, 5, 5, 4, 3, 3, 1, 34, 5]. d) [1, 3, 4, 5, 20, 5, 25, 3, 34, 5].

Answer: a Explanation: Execute in the shell to verify.

2. What is the output when we execute list("hello")? a) ['h', 'e', 'l', 'l', 'o']. b) ['hello']. c) ['llo']. d) ['olleh'].

Answer: a Explanation: Execute in the shell to verify.

4. What will be the output? values = [[3, 4, 5, 1], [33, 6, 1, 2]] v = values[0][0] for lst in values: for element in lst: if v > element: v = element print(v) a) 1 b) 3 c) 5 d) 6

Answer: a Explanation: Execute in the shell to verify.

6. What is the output when following code is executed ? >>>list("a#b#c#d".split('#')) a) ['a', 'b', 'c', 'd']. b) ['a b c d']. c) ['a#b#c#d']. d) ['abcd'].

Answer: a Explanation: Execute in the shell to verify.

7. To insert 5 to the third position in list1, we use which command ? a) list1.insert(3, 5) b) list1.insert(2, 5) c) list1.add(3, 5) d) list1.append(3, 5)

Answer: a Explanation: Execute in the shell to verify.

8. To remove string "hello" from list1, we use which command ? a) list1.remove("hello") b) list1.remove(hello) c) list1.removeAll("hello") d) list1.removeOne("hello")

Answer: a Explanation: Execute in the shell to verify.

8. What will be the output? def increment_items(L, increment): i = 0 while i < len(L): L[i] = L[i] + increment i = i + 1 values = [1, 2, 3] print(increment_items(values, 2)) print(values) a) None [3, 4, 5]. b) None [1, 2, 3]. c) [3, 4, 5]. [1, 2, 3]. d) [3, 4, 5]. None

Answer: a Explanation: Execute in the shell to verify.

7. What is the output when following code is executed ? myList = [1, 5, 5, 5, 5, 1] max = myList[0] indexOfMax = 0 for i in range(1, len(myList)): if myList[i] > max: max = myList[i] indexOfMax = i >>>print(indexOfMax) a) 1 b) 2 c) 3 d) 4

Answer: a Explanation: First time the highest number is encountered is at index 1.

9. What will be the output? def example(L): ''' (list) -> list ''' i = 0 result = [] while i < len(L): result.append(L[i]) i = i + 3 return result a) Return a list containing every third item from L starting at index 0 b) Return an empty list c) Return a list containing every third index from L starting at index 0 d) Return a list containing the items from L starting from index 0, omitting every third item

Answer: a Explanation: Run the code to get a better understanding with many arguments.

3. What is the output of the following code? a=[13,56,17] a.append([87]) a.extend([45,67]) print(a) a) [13, 56, 17, [87], 45, 67]. b) [13, 56, 17, 87, 45, 67]. c) [13, 56, 17, 87,[ 45, 67]]. d) [13, 56, 17, [87], [45, 67]].

Answer: a Explanation: The append function simply adds its arguments to the list as it is while extend function extends its arguments and later appends it.

9. What is the output of the following piece of code? x=[[1],[2]] print(" ".join(list(map(str,x)))) a) [1] [2]. b) [49] [50]. c) Syntax error d) [[1]] [[2]].

Answer: a Explanation: The elements 1 and 2 are first put into separate lists and then combined with a space in between using the join attribute.

6. What is the output of the following code? a="hello" b=list((x.upper(),len(x)) for x in a) print(b) a) [('H', 1), ('E', 1), ('L', 1), ('L', 1), ('O', 1)]. b) [('HELLO', 5)]. c) [('H', 5), ('E', 5), ('L', 5), ('L', 5), ('O', 5)]. d) Syntax error

Answer: a Explanation: Variable x iterates over each letter in string a hence the length of each letter is 1.

4. Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after listExample.pop()? a) [3, 4, 5, 20, 5, 25, 1]. b) [1, 3, 3, 4, 5, 5, 20, 25]. c) [3, 5, 20, 5, 25, 1, 3]. d) [1, 3, 4, 5, 20, 5, 25].

Answer: a Explanation: pop() by default will remove the last element.

5. What is the output when the following code is executed ? >>>"Welcome to Python".split() a) ["Welcome", "to", "Python"]. b) ("Welcome", "to", "Python") c) {"Welcome", "to", "Python"} d) "Welcome", "to", "Python"

Answer: a Explanation: split() function returns the elements in a list.

4. What will be the output? numbers = [1, 2, 3, 4] numbers.append([5,6,7,8]) print(len(numbers)) a) 4 b) 5 c) 8 d) 12

Answer: b Explanation: A list is passed in append so the length is 5.

1. What is the output of the following code? a=[1,2,3] b=a.append(4) print(a) print(b) a) [1,2,3,4]. [1,2,3,4]. b) [1, 2, 3, 4]. None c) Syntax error d) [1,2,3]. [1,2,3,4].

Answer: b Explanation: Append function on lists doesn't return anything. Thus the value of b is None.

5. What is the output when following code is executed ? >>>list1 = [11, 2, 23] >>>list2 = [11, 2, 2] >>>list1 < list2 is a) True b) False c) Error d) None

Answer: b Explanation: Elements are compared one by one.

1. What will be the output? >>>m = [[x, x + 1, x + 2] for x in range(0, 3)] a) [[1, 2, 3], [4, 5, 6], [7, 8, 9]]. b) [[0, 1, 2], [1, 2, 3], [2, 3, 4]]. c) [1, 2, 3, 4, 5, 6, 7, 8, 9]. d) [0, 1, 2, 1, 2, 3, 2, 3, 4].

Answer: b Explanation: Execute in the shell to verify.

2. What will be the output when executed in python shell? >>> a=[14,52,7] >>>> b=a.copy() >>> b is a a) True b) False

Answer: b Explanation: List b is just a copy of the original list. Any copy made in list b will not be reflected in list a.

9. What is the output when following code is executed ? >>>list1 = [1, 3] >>>list2 = list1 >>>list1[0] = 4 >>>print(list2) a) [1, 3]. b) [4, 3]. c) [1, 4]. d) [1, 3, 4].

Answer: b Explanation: Lists should be copied by executing [:] operation.

3. What is the output of the following code? import copy a=[10,23,56,[78]] b=copy.deepcopy(a) a[3][0]=95 a[1]=34 print(b) a) [10,34,56,[95]]. b) [10,23,56,[78]]. c) [10,23,56,[95]]. d) [10,34,56,[78]].

Answer: b Explanation: The above copy is deepcopy. Any change made in the original list isn't reflected.

5. What is the output of the following code? lst=[[1,2],[3,4]] print(sum(lst,[])) a) [[3],[7]]. b) [1,2,3,4]. c) Error d) [10].

Answer: b Explanation: The above piece of code is used for flattening lists.

8. What is the output of the following code? a=[[]]*3 a[1].append(7) print(a) a) Syntax error b) [[7], [7], [7]]. c) [[7], [], []]. d) [[],7, [], []].

Answer: b Explanation: The first line of the code creates multiple reference copies of sublist. Hence when 7 is appended, it gets appended to all the sublists.

14. What is the output of the following code? a=["Apple","Ball","Cobra"] <br class="blank" />a.sort(key=len) print(a) a) ['Apple', 'Ball', 'Cobra']. b) ['Ball', 'Apple', 'Cobra']. c) ['Cobra', 'Apple', 'Ball']. d) Invalid syntax for sort().

Answer: b Explanation: The syntax isn't invalid and the list is sorted according to the length of the strings in the list since key is given as len.

6. To add a new element to a list we use which command ? a) list1.add(5) b) list1.append(5) c) list1.addLast(5) d) list1.addEnd(5)

Answer: b Explanation: We use the function append to add an element to the list.

2. What is the output when following code is executed ? names1 = ['Amir', 'Bear', 'Charlton', 'Daman'] names2 = names1 names3 = names1[:] names2[0] = 'Alice' names3[1] = 'Bob' sum = 0 for ls in (names1, names2, names3): if ls[0] == 'Alice': sum += 1 if ls[1] == 'Bob': sum += 10 print sum a) 11 b) 12 c) 21 d) 22

Answer: b Explanation: When assigning names1 to names2, we create a second reference to the same list. Changes to names2 affect names1. When assigning the slice of all elements in names1 to names3, we are creating a full copy of names1 which can be modified independently.

8. What is the output of the following code? places = ['Bangalore', 'Mumbai', 'Delhi'] <br class="blank" />places1 = places places2 = places[:] <br class="blank" />places1[1]="Pune" places2[2]="Hyderabad" print(places) a) ['Bangalore', 'Pune', 'Hyderabad']. b) ['Bangalore', 'Pune', 'Delhi']. c) ['Bangalore', 'Mumbai', 'Delhi']. d) ['Bangalore', 'Mumbai', 'Hyderabad'].

Answer: b Explanation: places1 is an alias of the list places. Hence, any change made to places1 is reflected in places. places2 is a copy of the list places. Thus, any change made to places2 isn't reflected in places.

4. What is the output of the following piece of code? a=list((45,)*4) print((45)*4) print(a) a) 180 [(45),(45),(45),(45)]. b) (45,45,45,45). [45,45,45,45]. c) 180 [45,45,45,45]. d) Syntax error

Answer: c Explanation: (45) is an int while (45,) is a tuple of one element. Thus when a tuple is multiplied, it created references of itself which is later converted to a list.

7. What will be the output? def addItem(listParam): listParam += [1] mylist = [1, 2, 3, 4] addItem(mylist) print(len(mylist)) a) 1 b) 4 c) 5 d) 8

Answer: c Explanation: + will append the element to the list.

3. Suppose list1 is [1, 3, 2], What is list1 * 2 ? a) [2, 6, 4]. b) [1, 3, 2, 1, 3]. c) [1, 3, 2, 1, 3, 2] . D) [1, 3, 2, 3, 2, 1].

Answer: c Explanation: Execute in the shell and verify.

1. What will be the output? def f(i, values = []): values.append(i) return values f(1) f(2) v = f(3) print(v) a) [1] [2] [3]. b) [1] [1, 2] [1, 2, 3]. c) [1, 2, 3]. d) 1 2 3

Answer: c Explanation: Execute in the shell to verify

10. What is the output when following code is executed ? def f(values): values[0] = 44 v = [1, 2, 3] f(v) print(v) a) [1, 44]. b) [1, 2, 3, 44]. c) [44, 2, 3]. d) [1, 2, 3].

Answer: c Explanation: Execute in the shell to verify.

10. What will be the output? points = [[1, 2], [3, 1.5], [0.5, 0.5]] points.sort() print(points) a) [[1, 2], [3, 1.5], [0.5, 0.5]]. b) [[3, 1.5], [1, 2], [0.5, 0.5]]. c) [[0.5, 0.5], [1, 2], [3, 1.5]]. d) [[0.5, 0.5], [3, 1.5], [1, 2]].

Answer: c Explanation: Execute in the shell to verify.

2. How many elements are in m? m = [[x, y] for x in range(0, 4) for y in range(0, 4)] a) 8 b) 12 c) 16 d) 32

Answer: c Explanation: Execute in the shell to verify.

2. What will be the output? names1 = ['Amir', 'Bala', 'Chales'] if 'amir' in names1: print(1) else: print(2) a) None b) 1 c) 2 d) Error

Answer: c Explanation: Execute in the shell to verify.

4. Suppose list1 = [0.5 * x for x in range(0, 4)], list1 is : a) [0, 1, 2, 3]. b) [0, 1, 2, 3, 4]. c) [0.0, 0.5, 1.0, 1.5]. d) [0.0, 0.5, 1.0, 1.5, 2.0].

Answer: c Explanation: Execute in the shell to verify.

8. What is the output when following code is executed ? myList = [1, 2, 3, 4, 5, 6] for i in range(1, 6): myList[i - 1] = myList[i] for i in range(0, 6): print(myList[i], end = " ") a) 2 3 4 5 6 1 b) 6 1 2 3 4 5 c) 2 3 4 5 6 6 d) 1 1 2 3 4 5

Answer: c Explanation: Execute in the shell to verify.

9. What will be the output? data = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]] def ttt(m): v = m[0][0] for row in m: for element in row: if v < element: v = element return v print(ttt(data[0])) a) 1 b) 2 c) 4 d) 5

Answer: c Explanation: Execute in the shell to verify.

10. What is the output of the following code? a=165 b=sum(list(map(int,str(a)))) print(b) a) 561 b) 5 c) 12 d) Syntax error

Answer: c Explanation: First, map converts the number to string and then places the individual digits in a list. Then, sum finds the sum of the digits in the list. The code basically finds the sum of digits in the number.

4. Suppose list1 is [2445,133,12454,123], what is max(list1) ? a) 2445 b) 133 c) 12454 d) 123

Answer: c Explanation: Max returns the maximum element in the list.

1. What is the output of the following code? a=[10,23,56,[78]] b=list(a) a[3][0]=95 a[1]=34 print(b) a) [10,34,56,[95]]. b) [10,23,56,[78]]. c) [10,23,56,[95]]. d) [10,34,56,[78]].

Answer: c Explanation: The above copy is a type of shallow copy and only changes made in sublist is reflected in the copied list.

13. What is the output of the following code? a = [1, 5, 7, 9, 9, 1] <br class="blank" />b=a[0] <br class="blank" />x= 0 for x in range(1, len(a)): if a[x] > b: b = a[x] b= x print(b) a) 5 b) 3 c) 4 d) 0

Answer: c Explanation: The above piece of code basically prints the index of the largest element in the list.

9. What is the output of the following code? b=[2,3,4,5] a=list(filter(lambda x:x%2,b)) print(a) a) [2,4]. b) [ ]. c) [3,5]. d) Invalid arguments for filter function

Answer: c Explanation: The filter function gives value from the list b for which the condition is true, that is, x%2==1.

4. What is the output of the following piece of code? s="a@b@c@d" a=list(s.partition("@")) print(a) b=list(s.split("@",3)) print(b) a) ['a','b','c','d']. ['a','b','c','d']. b) ['a','@','b','@','c','@','d']. ['a','b','c','d']. c) ['a','@','b@c@d']. ['a','b','c','d']. d) ['a','@','b@c@d']. ['a','@','b','@','c','@','d'].

Answer: c Explanation: The partition function only splits for the first parameter along with the separator while split function splits for the number of times given in the second argument but without the separator.

3. Suppose listExample is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after listExample.pop(1) ? a) [3, 4, 5, 20, 5, 25, 1, 3]. b) [1, 3, 3, 4, 5, 5, 20, 25]. c) [3, 5, 20, 5, 25, 1, 3]. d) [1, 3, 4, 5, 20, 5, 25].

Answer: c Explanation: pop() removes the element at the position specified in the parameter.

6. What will be the output? list1 = [1, 2, 3, 4] list2 = [5, 6, 7, 8] print(len(list1 + list2)) a) 2 b) 4 c) 5 d) 8

Answer: d Explanation: + appends all the elements individually into a new list.

9. Suppose list1 is [3, 4, 5, 20, 5], what is list1.index(5) ? a) 0 b) 1 c) 4 d) 2

Answer: d Explanation: Execute help(list.index) to get details.

1. Which of the following commands will create a list? a) list1 = list() b) list1 = []. c) list1 = list([1, 2, 3]) d) all of the mentioned

Answer: d Explanation: Execute in the shell to verify

1. Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is list1 after list1.reverse() ? a) [3, 4, 5, 20, 5, 25, 1, 3]. b) [1, 3, 3, 4, 5, 5, 20, 25]. c) [25, 20, 5, 5, 4, 3, 3, 1]. d) [3, 1, 25, 5, 20, 5, 4, 3].

Answer: d Explanation: Execute in the shell to verify.

1. What is the output when following code is executed ? >>>names = ['Amir', 'Bear', 'Charlton', 'Daman'] >>>print(names[-1][-1]) a) A b) Daman c) Error d) n

Answer: d Explanation: Execute in the shell to verify.

10. Suppose list1 is [3, 4, 5, 20, 5, 25, 1, 3], what is list1.count(5) ? a) 0 b) 4 c) 1 d) 2

Answer: d Explanation: Execute in the shell to verify.

3. What will be the output? values = [[3, 4, 5, 1], [33, 6, 1, 2]] v = values[0][0] for row in range(0, len(values)): for column in range(0, len(values[row])): if v < values[row][column]: v = values[row][column] print(v) a) 3 b) 5 c) 6 d) 33

Answer: d Explanation: Execute in the shell to verify.

5. What will be the output? values = [[3, 4, 5, 1 ], [33, 6, 1, 2]] for row in values: row.sort() for element in row: print(element, end = " ") print() a) The program prints two rows 3 4 5 1 followed by 33 6 1 2 b) The program prints on row 3 4 5 1 33 6 1 2 c) The program prints two rows 3 4 5 1 followed by 33 6 1 2 d) The program prints two rows 1 3 4 5 followed by 1 2 6 33

Answer: d Explanation: Execute in the shell to verify.

6. What is the output? matrix = [[1, 2, 3, 4], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]] for i in range(0, 4): print(matrix[i][1], end = " ") a) 1 2 3 4 b) 4 5 6 7 c) 1 3 8 12 d) 2 5 9 13

Answer: d Explanation: Execute in the shell to verify.

7. What will be the output? def m(list): v = list[0] for e in list: if v < e: v = e return v values = [[3, 4, 5, 1], [33, 6, 1, 2]] for row in values: print(m(row), end = " ") a) 3 33 b) 1 1 c) 5 6 d) 5 33

Answer: d Explanation: Execute in the shell to verify.

8. What will be the output? data = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]] print(data[1][0][0]) a) 1 b) 2 c) 4 d) 5

Answer: d Explanation: Execute in the shell to verify.

5. To which of the following the "in" operator can be used to check if an item is in it? a) Lists b) Dictionary c) Set d) All of the mentioned

Answer: d Explanation: In can be used in all data structures.

6. What is the output of the following code? word1="Apple" word2="Apple" list1=[1,2,3] list2=[1,2,3] print(word1 is word2) print(list1 is list2) a) True True b) False True c) False False d) True False

Answer: d Explanation: In the above case, both the lists are equivalent but not identical as they have different objects.

3. What will be the output? names1 = ['Amir', 'Bala', 'Charlie'] names2 = [name.lower() for name in names1] print(names2[2][0]) a) None b) a c) b d) c

Answer: d Explanation: List Comprehension are a shorthand for creating new lists.

5. What is the output of the following code? a=[1,2,3,4] b=[sum(a[0:x+1]) for x in range(0,len(a))] print(b) a) 10 b) [1,3,5,7]. c) 4 d) [1,3,6,10].

Answer: d Explanation: The above code returns the cumulative sum of elements in a list.

7. What is the output of the following code? a=[1,2,3,4] b=[sum(a[0:x+1]) for x in range(0,len(a))] print(b) a) 10 b) [1,3,5,7]. c) 4 d) [1,3,6,10].

Answer: d Explanation: The above code returns the cumulative sum of elements in a list. IT'S OK I REALIZED IT MIGHT BE TWICE THE SAME QUESTION.

11. What is the output of the following code? a= [1, 2, 3, 4, 5] for i in range(1, 5): a[i-1] = a[i] for i in range(0, 5): print(a[i],end = " ") a) 5 5 1 2 3 b) 5 1 2 3 4 c) 2 3 4 5 1 d) 2 3 4 5 5

Answer: d Explanation: The items having indexes from 1 to 4 are shifted forward by one index due to the first for-loop and the item of index four is printed again because of the second for-loop.

2. What does the following piece of code do? print(list(zip((1,2,3),('a'),('xxx','yyy')))) print(list(zip((2,4),('b','c'),('yy','xx')))) a) [(1,2,3),('a'),('xxx','yyy')]. [(2,4),('b','c'),('yy','xx')]. b) [(1, 'a', 'xxx'),(2,' ','yyy'),(3,' ',' ')]. [(2, 'b', 'yy'), (4, 'c', 'xx')]. c) Syntax error. d) [(1, 'a', 'xxx')]. [(2, 'b', 'yy'), (4, 'c', 'xx')].

Answer: d Explanation: The zip function combines the individual attributes of the lists into a list of tuples.

15. What is the output of the following code? num = ['One', 'Two', 'Three'] for i, x in enumerate(num): print('{}: {}'.format(i, x),end=" ") a) 1: 2: 3: b) Exception is thrown c) One Two Three d) 0: One 1: Two 2: Three

Answer: d Explanation: enumerate(iterator,start=0) is a built-in function which returns (0,lst[0]),(1,lst[1]) and so on where lst is a list(iterator).

7. What is the output of the following code? def unpack(a,b,c,d): print(a+d) x = [1,2,3,4] unpack(*x) a) Error b) [1,4]. c) [5]. d) 5

Answer: d Explanation: unpack(*x) unpacks the list into the separate variables. Now, a=1 and d=4. Thus 5 gets printed.

10. What is the output of the following code? lst=[3,4,6,1,2] lst[1:2]=[7,8] print(lst) a) [3, 7, 8, 6, 1, 2]. b) Syntax error c) [3,[7,8],6,1,2]. d) [3,4,6,7,8].

Explanation: In the piece of code, slice assignment has been implemented. The sliced list is replaced by the assigned elements in the list. Type in python shell to verify.


संबंधित स्टडी सेट्स

Leadership 101 Conventional NCTI Exam

View Set

Pharm WK 7 Heart Failure/Diuretics

View Set

BUS 100 CHAPTER 13, 11 PRE LECTURE QUIZZES

View Set

Sports and Entertainment Marketing- FINALS Review MASTER SET

View Set

_______ is indicated for which of the following?

View Set