Python Programming Questions.Let’s begin with lists in Python.Q-1. What will be the output of the following code snippet?a=[1,2,3,4,5,6,7,8,9] print(a[::2])A. [1,2] Show Click here to view the answer. Answer. C Q-2. What will be the output of the following code snippet?a=[1,2,3,4,5,6,7,8,9] a[::2]=10,20,30,40,50,60 print(a)A. ValueError: attempt to assign sequence of size 6 to extended slice of size 5 Click here to view the answer. Answer. A Q-3. What will be the output of the following code snippet?a=[1,2,3,4,5] print(a[3:0:-1])A. Syntax error Click here to view the answer. Answer. B Q-4. What will be the output of the following code snippet?def f(value, values): v = 1 values[0] = 44 t = 3 v = [1, 2, 3] f(t, v) print(t, v[0])A. 1 44 Click here to view the answer. Answer. C Q-5. What is the correct command to shuffle the following list?fruit=['apple', 'banana', 'papaya', 'cherry']A. fruit.shuffle() Click here to view the answer. Answer. C Q-6. What will be the output of the following code snippet?data = [[[1, 2], [3, 4]], [[5, 6], [7, 8]]] def fun(m): v = m[0][0] for row in m: for element in row: if v < element: v = element return v print(fun(data[0]))A. 1 Click here to view the answer. Answer. D Q-7. What will be the output of the following code snippet?arr = [[1, 2, 3, 4], [4, 5, 6, 7], [8, 9, 10, 11], [12, 13, 14, 15]] for i in range(0, 4): print(arr[i].pop())A. 1 2 3 4 Click here to view the answer. Answer. C Q-8. What will be the output of the following code snippet?def f(i, values = []): values.append(i) print (values) return values f(1) f(2) f(3)A. [1] [2] [3] Click here to view the answer. Answer. C Q-9. What will be the output of the following code snippet?arr = [1, 2, 3, 4, 5, 6] for i in range(1, 6): arr[i - 1] = arr[i] for i in range(0, 6): print(arr[i], end = " ")A. 1 2 3 4 5 6 Click here to view the answer. Answer. D Q-10. What will be the output of the following code snippet?fruit_list1 = ['Apple', 'Berry', 'Cherry', 'Papaya'] fruit_list2 = fruit_list1 fruit_list3 = fruit_list1[:] fruit_list2[0] = 'Guava' fruit_list3[1] = 'Kiwi' sum = 0 for ls in (fruit_list1, fruit_list2, fruit_list3): if ls[0] == 'Guava': sum += 1 if ls[1] == 'Kiwi': sum += 20 print (sum)A. 22 Click here to view the answer. Answer. A 💡 Recommended –Essential Python Coding Tips and Tricks for Programmers. Let’s begin with tuples in Python.Q-1. What will be the output of the following code snippet?init_tuple = () print (init_tuple.__len__())A.None Click here to view the answer. Answer. C Q-2. What will be the output of the following code snippet?init_tuple_a = 'a', 'b' init_tuple_b = ('a', 'b') print (init_tuple_a == init_tuple_b)A.0 Click here to view the answer. Answer. D Q-3. What will be the output of the following code snippet?init_tuple_a = '1', '2' init_tuple_b = ('3', '4') print (init_tuple_a + init_tuple_b)A. (1, 2, 3, 4) Click here to view the answer. Answer. B Q-4. What will be the output of the following code snippet?init_tuple_a = 1, 2 init_tuple_b = (3, 4) [print(sum(x)) for x in [init_tuple_a + init_tuple_b]]A.Nothing gets printed. Click here to view the answer. Answer. C Q-5. What will be the output of the following code snippet?init_tuple = [(0, 1), (1, 2), (2, 3)] result = sum(n for _, n in init_tuple) print(result)A.3 Click here to view the answer. Answer. B Q-6. Which of the following statements given below is/are true?A. Tuples have structure, lists have an order. Click here to view the answer. Answer. A & C Q-7. What will be the output of the following code snippet?l = [1, 2, 3] init_tuple = ('Python',) * (l.__len__() - l[::-1][0]) print(init_tuple)A. () Click here to view the answer. Answer. A Q-8. What will be the output of the following code snippet?init_tuple = ('Python') * 3 print(type(init_tuple))A. <class ‘tuple’> Click here to view the answer. Answer. B Q-9. What will be the output of the following code snippet?init_tuple = (1,) * 3 init_tuple[0] = 2 print(init_tuple)A. (1, 1, 1) Click here to view the answer. Answer. D Q-10. What will be the output of the following code snippet?init_tuple = ((1, 2),) * 7 print(len(init_tuple[3:8]))A.Exception Click here to view the answer. Answer. C 💡 Recommended –Essential Python Optimization Tips and Tricks for Programmers. Let’s begin with dictionaries in Python.Q-1. What will be the output of the following code snippet?a = {(1,2):1,(2,3):2} print(a[1,2])A. Key Error Click here to view the answer. Answer. B Q-2. What will be the output of the following code snippet?a = {'a':1,'b':2,'c':3} print (a['a','b'])A. Key Error Click here to view the answer. Answer. A Q-3. What will be the output of the following code snippet?fruit = {} def addone(index): if index in fruit: fruit[index] += 1 else: fruit[index] = 1 addone('Apple') addone('Banana') addone('apple') print (len(fruit))A. 1 Click here to view the answer. Answer. C Q-4. What will be the output of the following code snippet?arr = {} arr[1] = 1 arr['1'] = 2 arr[1] += 1 sum = 0 for k in arr: sum += arr[k] print (sum)A. 1 Click here to view the answer. Answer. D Q-5. What will be the output of the following code snippet?my_dict = {} my_dict[1] = 1 my_dict['1'] = 2 my_dict[1.0] = 4 sum = 0 for k in my_dict: sum += my_dict[k] print (sum)A. 7 Click here to view the answer. Answer. D Q-6. What will be the output of the following code snippet?my_dict = {} my_dict[(1,2,4)] = 8 my_dict[(4,2,1)] = 10 my_dict[(1,2)] = 12 sum = 0 for k in my_dict: sum += my_dict[k] print (sum) print(my_dict)A. Syntax error Click here to view the answer. Answer. B Q-7. What will be the output of the following code snippet?box = {} jars = {} crates = {} box['biscuit'] = 1 box['cake'] = 3 jars['jam'] = 4 crates['box'] = box crates['jars'] = jars print (len(crates[box]))A. 1 Click here to view the answer. Answer. D Q-8. What will be the output of the following code snippet?dict = {'c': 97, 'a': 96, 'b': 98} for _ in sorted(dict): print (dict[_])A.96 98 97 Click here to view the answer. Answer. A Q-9. What will be the output of the following code snippet?rec = {"Name" : "Python", "Age":"20"} r = rec.copy() print(id(r) == id(rec))A.True Click here to view the answer. Answer. B Q-10. What will be the output of the following code snippet?rec = {"Name" : "Python", "Age":"20", "Addr" : "NJ", "Country" : "USA"} id1 = id(rec) del rec rec = {"Name" : "Python", "Age":"20", "Addr" : "NJ", "Country" : "USA"} id2 = id(rec) print(id1 == id2)A.True Click here to view the answer. Answer. A 31. Modify an existing list with a lambda functionLet’s take the previous map function we wrote and turn it into a one-liner with a lambda. #=> [50, 100, 150, 200, 250] I could have left it as a map object until I needed to iterate over it but I converted to a list to show the elements inside. |