InterviewSolution
This section includes InterviewSolutions, each offering curated multiple-choice questions to sharpen your knowledge and support exam preparation. Choose a topic below to get started.
| 151. |
Which function is used to delete elements of a list if its index is unknown?(a) del(b) delete(c) remove( )(d) backspace |
|
Answer» (c) remove( ) |
|
| 152. |
Which statement is used to delete known elements? (a) del (b) delete (c) remove (d) rem |
|
Answer» del is used to delete known elements |
|
| 153. |
Which is the symmetric difference operator?(a) + (b) – (c) ^ (d) & |
|
Answer» ^ is the symmetric difference operator |
|
| 154. |
Write a short note about sort( )? |
|
Answer» sort( ) function sorts the element in list. Syntax: List.sort (reverse = True/False, Key = myFunc) Both arguments are optional
Example My List=[‘Thilothamma’ , ’Tharani’ , ‘Anitha’ , ‘SaiSree’ , ‘Lavanya’] MyList.sort( ) print(MyList) Output: [‘Anitha’ , ‘Lavanya’ , ‘SaiSree’ , ‘Tharani’ , ‘Thilothamma’] |
|
| 155. |
In changing list elements, ……. is the upper limit of this range.(a) Index from(b) Index to(c) Index with(d) Index |
|
Answer» (b) Index to |
|
| 156. |
A …… is a sequence data type like strings.(a) List(b) Tuples(c) Set(d) Dictionary |
|
Answer» A List is a sequence data type like strings. |
|
| 157. |
What are the difference between List and Dictionary? |
|
Answer» Difference between List and Dictionary 1. List is an ordered set of elements. But, a dictionary is a data structure that is used for matching one element (Key) with another (Value). 2. The index values can be used to access a particular element. But, in dictionary key represents index. Remember that, key may be a number of a string. 3. Lists are used to look up a value whereas a dictionary is used to take one value and look up another value. |
|
| 158. |
Write a program to create a list of numbers in the range 1 to 20. Then delete all the numbers from the list that are divisible by 3? |
|
Answer» num = [ ] for x in range(1, 21): num.append(x) print(“The list of numbers from 1 to 20 = ” , num) for index, i in enumerate(num): if(i % 3 == 0) del num[index] print(“The list after deleting numbers” , num) Output: The list of numbers from 1 to 20 = [1,2,3,4… 20] The list after deleting numbers[1, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 20] |
|
| 159. |
A list or tuples can be converted as set by using ……. function?(a) set (b) create set(c) change(d) alter |
|
Answer» A list or tuples can be converted as set by using set function |
|
| 160. |
What are the advantages of Tuples over a list? |
|
Answer» 1. The elements of a list are changeable (mutable) whereas the elements of a tuple are unchangeable (immutable), this is the key difference between tuples and list. 2. The elements of a list are enclosed within square brackets. But, the elements of a tuple are enclosed by paranthesis. 3. Iterating tuples is faster than list. |
|
| 161. |
Which operator is used to join two tuples?(a) – (b) _ (c) +(d) +: |
|
Answer» + is used to join two tuples. |
|
| 162. |
How many arguments are there in the sort( ) function?(a) 1(b) 2(c) 3(d) 4 |
|
Answer» There are 2 arguments in the sort( ) function |
|
| 163. |
The term ……. in latin represents an abstraction of the sequence of numbers.(a) list(b) tuples(c) set(d) dictionary |
|
Answer» The term tuples in latin represents an abstraction of the sequence of numbers. |
|
| 164. |
Identify the correct statement.(a) The dictionary type stores a index along with its element(b) The dictionary type stores a key along with its element |
|
Answer» (a) The dictionary type stores a index along with its element |
|
| 165. |
Which is an optional argument in range( ) function ……..(a) start value(b) end value(c) step value(d) default |
|
Answer» (c) step value |
|
| 166. |
The ……. function is used to create tuples from a list. |
|
Answer» The tuple( ) function is used to create tuples from a list. |
|
| 167. |
……… consists of a number of values separated by comma and enclosed within parenthesis(a) list(b) tuples(c) dictionary(d) sets |
|
Answer» tuples consists of a number of values separated by comma and enclosed within parenthesis |
|
| 168. |
The mixed collection of elements are called as ………(a) list (b) tuples (c) sets (d) dictionary |
|
Answer» The mixed collection of elements are called as dictionary |
|
| 169. |
Which part is optional in dictionary comprehension? (a) If (b) expression (c) var (d) sequences |
|
Answer» If is optional in dictionary comprehension |
|
| 170. |
Identify the wrong statement from the following:(a) The elements of the tuple are enclosed by parenthesis.(b) The elements of a tuple can be even defined without parenthesis(c) The list elements have to be given in square brackets(d) Iterating list is faster than tuples. |
|
Answer» (d) Iterating list is faster than tuples. |
|
| 171. |
The key value pairs are enclosed with ……(a) <> (b) [ ] (c) { } (d) ( ) |
|
Answer» The key value pairs are enclosed with { } |
|
| 172. |
Give the syntax for remove, pop and clear? |
|
Answer» Syntax: List.remove(element) # to delete a particular element List.pop(index of an element) List, clear( ) |
|
| 173. |
Find the statement which is wrong. When you assign a value to the key(a) it will be appended(b) it will overwrite the old data |
|
Answer» (a) it will be appended |
|
| 174. |
Give the 3 different syntax formats for deleting the elements from a list? |
|
Answer» Syntax: del List [index of an element] # to delete a particular element del List [index from : index to] # to delete multiple elements del List # to delete entire list |
|
| 175. |
Give the syntax for changing list elements? |
|
Answer» Syntax: List_Variable [index of an element] = Value to be changed List_Variable [index from : index to] = Values to changed Where, index from is the beginning index of the range; index to is the upper limit of the range which is excluded in the range. |
|
| 176. |
Pick odd one with including elements in list.(a) append( )(b) extend( )(c) insert( )(d) include |
|
Answer» Answer is (d) include |
|
| 177. |
What will be the result of the following Python code? S = [x**2 for x in range(5)]print(S)(a) [0, 1, 2, 4, 5] (b) [0, 1, 4, 9, 16](c) [0, 1, 4, 9, 16, 25](d) [1, 4, 9, 16, 25] |
|
Answer» (b) [0, 1, 4, 9, 16] |
|
| 178. |
Let list1=[2 ,4, 6, 8, 10], then print (List1[-2]) will result in ……(a) 10(b) 8(c) 4(d) 6 |
|
Answer» Let list1=[2 ,4, 6, 8, 10], then print (List1[-2]) will result in 8 |
|
| 179. |
Which one of the following is the tuple assignment operator?(a) += (b) = (c) == (d) *= |
|
Answer» = is the tuple assignment operator |
|
| 180. |
Define singleton tuple? |
|
Answer» While creating a tuple with a single element, add a comma at the end of the element. In the absence of a comma, Python will consider the element as an ordinary data type; not a tuple. Creating a Tuple with one element is called “Singleton” tuple. MyTup5 = (10,) |
|
| 181. |
Write the output for the following codes.A= (10:1000,20:2000,30:3000,40:4000, 50:5000}print A.items()print A.keys()print A.values() |
|
Answer» [(40, 4000), (10, 1000), (20, 2000), (50, 5000), (30, 3000)] [40,10, 20, 50, 30] [4000,1000, 2000,5000,3000] |
|
| 182. |
Write the output for the following codes.A=(10:1000,20:2000,30:3000,40:4000, 50:5000} print A.items() print A.keys() print A.values() |
|
Answer» [(40, 4000), (10, 1000), (20, 2000), (50, 5000), (30, 3000)] [40,10, 20, 50, 30] [4000,1000, 2000,5000,3000] |
|
| 183. |
Write a program to pass any list and to arrange all numbers in descending order. |
|
Answer» def arrange (1, n) for i in range (n – 1): for j in range (n – i – 1): if l[j] > l[j +1] : temp = l[j] l[j] = l[j+l] l[j+l] = temp |
|
| 184. |
Write a program to pass any list and to arrange all numbers in descending order. |
|
Answer» def arrange (1, n) for i in range (n – 1): for j in range (n – i – 1): if l[j] > l[j +1] : temp = l[j] l[j] = l[j+l] l[j+l] = temp |
|
| 185. |
Write the output of the given python code :# ! ‘user/bin’pvthonaList1 = [123, ‘xvz’, zara’, abc’];print “Index for xyz : ” aList. index) ‘xyz’);print “Index for zara :”, aList. index(‘zara’); |
|
Answer» This will produce the following result: Index for xyz : 1 Index for xxx : 2 |
|
| 186. |
Write the output of the given Python code.# !/user/bin/python list1 = [‘physics’, ‘chemistry’, 1997, 2000];list2 = [1,2,3,4,5,6, 7];print “list1[0]”, list1[0] |
|
Answer» Answer: list1[0]: physics |
|
| 187. |
Describe list() with its syntax. |
|
Answer» The method list() takes sequence types and converts them to lists. It is used toconvert a given tuple into list. Syntax Following is the syntax for list() method : list (seq) |
|
| 188. |
Write the output of the given Python code.# !/user/bin/python listl = [‘physics’, ‘chemistry’, 1997,2000]; list2 = [1,2, 3,4,5, 6, 7]; print “list2[l:5[ :”, list2[l:5] |
|
Answer» list2[1:5[: [2,3,4,5] |
|
| 189. |
How can we remove list element ? |
|
Answer» To remove a list element, you can use either the del statement or the remove]) method |
|
| 190. |
Give an example to remove list element. |
|
Answer» To remove a list element, you can use either I the del statement if you know exactly which ‘ element(s) you are deleting or the remove I ( ) method if you do not know. Following is a simple example: # !/user/bin/python list1 = [‘physics’, ‘chemistry’, 1997, 2000]; print list1; del list1 [2]; print “After deleting value at index 2 :” print list1; When the above code is executed, it produces the following result: [‘physics’, ‘chemistry’, 1997, 2000]; After deleting value at index 2; [‘physics’, ‘chemistry’, 2000] |
|
| 191. |
Give an example to remove list element. |
|
Answer» To remove a list element, you can use either I the del statement if you know exactly which ‘ element(s) you are deleting or the remove I ( ) method if you do not know. Following is a simple example: # !/user/bin/python list1 = [‘physics’, ‘chemistry’, 1997, 2000]; print list1; del list1 [2]; print “After deleting value at index 2 :” print list1; When the above code is executed, it produces the following result: [‘physics’, ‘chemistry’, 1997, 2000]; After deleting value at index 2; [‘physics’, ‘chemistry’, 2000] |
|
| 192. |
How can we remove list element ? |
| Answer» To remove a list element, you can use either the del statement or the remove]) method | |
| 193. |
Why we use cmp(listl, list2) in Python? |
| Answer» It compares elements of both lists. | |
| 194. |
Write the output of the given Python code.# !/user/bin/pythonlist1 = [‘physics’, ‘chemistry’, 1997,2000]; list2 = [1,2, 3,4,5, 6, 7];print “list2[1:5[ :”, list2[1:5] |
|
Answer» Answer: list2[1:5[: [2,3,4,5] |
|
| 195. |
Why we use cmp(listl, list2) in Python? |
|
Answer» It compares elements of both lists. |
|