Explore topic-wise InterviewSolutions in .

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.

1.

What do you mean by list.append(obj)

Answer»

Answer:

Appends object obj to list

2.

Which function is use for removing object obj from list.

Answer»

list.remove(obj)

3.

What do you mean by list.append(obj)

Answer»

Appends object obj to list

4.

Which function is use for returning the lowest index in list that obj appears.

Answer»

List index (obj).

5.

What do you mean by list.count(obj)

Answer»

Returns count how many times obj occurs in list

6.

Which function is used to reverse objects of list in place

Answer»

list.reverse ()

7.

Explain list.extend(seq)

Answer»

Appends the contents of seq to list.

8.

Which method is used to sort objects of list.

Answer»

Answer:

sort ()

9.

Which function is used to reverse objects of list in place.

Answer»

Answer:

list.reverse ()

10.

Write the output of the given Python code :#!/user/bin/python aList = [123, ‘xyz’, ‘zara’, ‘abc’, 123]; bList = [2009, ‘manni’]; aList.extend (bList) print “Extended List :”, aList;

Answer»

This will produce the following result: 

Extended List : [123, ‘xyz’, ‘zara’, ‘abc’, 123, 2009, ‘manni’] 

11.

Is list.reverse() method return a value?

Answer»

This method does not return any value but reverse the given object from the list

12.

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»

list1[0]: physics

13.

Describe min(list) method.

Answer»

It returns item from the list with min value. 

14.

Describe max(list) method

Answer»

It returns item from the list with max value.

15.

How many types of built-in types of sequences in Python.

Answer»

Python has six built-in types of sequences. 

16.

How many types of built-in types of sequences in Python.

Answer» Python has six built-in types of sequences.
17.

What is the use of len(list)) in Python?

Answer»

Answer:

It gives the total length of the list.

18.

What is the use of len(list)) in Python? 

Answer»

It gives the total length of the list.

19.

Write the output of the following codeA = [2, 4, 6, 8,10] L = len (A)S = o for I in range (1, L, 2): S + = A[I] print “Sum=”, S

Answer»

Sum = 12 

Justification: 

A[1] = 4 step size = 2 

A[3] = 8 

S = 4 + 8 = 12

20.

How are lists different from strings when both are sequences ?

Answer» The lists and strings are different in following ways :

(a) The lists are mutable sequences while strings are immutable.

(b) Strings store single type of elements, all characters while lists can store elements belonging to different types.

(c) In consecutive locations, strings store the individual characters while list stores the references of its elements.
21.

Write the output of the following code A = [2, 4, 6, 8,10]L = len (A)S = ofor I in range (1, L, 2):S + = A[I]print “Sum=”, S

Answer» Sum = 12

Justification:

A[1] = 4 step size = 2

A[3] = 8

S = 4 + 8 = 12
22.

Give an example to update single or multiple elements of lists

Answer»

You can update single or multiple elements of lists by giving the slice on the lefthand side of the assignment operator, and you can add elements in a list with the append( ) method. Following is a simple example:

# !/user/bin/python

listl = [‘physics’, ‘chemistry’, 1997, 2000];

print “Value available at index 2 :”

print list[2];

list[2] = 2001;

print “New value available at index 2 :”

print list [2];

Note: append)) method is discussed in subsequent section.

When the above code is executed, it produces the following result:

Value available at index 2 :

1997

New value available at index 2 :

2001

23.

How can we update and delete list in python.

Answer»

Updating Lists:

You can update single or multiple elements of lists by giving the slice on the lefthand side of the assignment operator, and you can add to elements in a list with the append() method. Following is a simple example:

# !/user/bin/python

list = [‘physics’, ‘chemistry’, 1997, 2000];

print “Value available at index 2 :”

print list[2];

list [2] = 2001;

print “New value available at index 2 :”

print list [2];

When the above code is executed it produces the following result:

Value available at index 2 :

1997

New value available at index 2 :

2001

Delete List Elements:

To remove a list element, you can use either the del statement if you know exactly which element(s) you are deleting of the remove() 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, 2001]

After deleting value at index 2 :

[‘physics’, ‘chemistry’, 2000]

24.

Define a function overlapping ( ) that takes two lists and returns True if they have at least one member in common, False otherwise.

Answer»

def overlapping (a, b): 

11 = len (a) 

12 = len (b) 

for i in range (11): 

for j in range (12): 

if a[i] == b[j] : 

return True 

else 

return False

25.

Write a program to input any matrix and print both diagonal values of the matrix. 

Answer»

Import random 

m = input (“Enter the number of rows”) 

n = input (“Enter the number of columns”) 

a = [[random.random () for row in range (m)] for cal in mange (n)] 

if i in range (m):

for j in range (n): 

a[i][j] = input () 

print “First diagonal” 

for i in range (m):

print a[i][j], ‘\t’ 

k = m – 1 

print “second diagonal” 

for j in range (m):

print a[j][k], ‘\t’ 

k- = 1 

else: 

print “Diagonal values are not possible”

26.

Write a program to input n x m matrix and find the sum of all numbers

Answer»

def summat (a, m, n): 

s = 0 

for i in range (m): 

for j in range (n): 

s + = a[i][j] 

return s

27.

Give an example to update single or multiple elements of lists

Answer»

You can update single or multiple elements of lists by giving the slice on the lefthand side of the assignment operator, and you can add elements in a list with the append( ) method. Following is a simple example: 

# !/user/bin/python

 listl = [‘physics’, ‘chemistry’, 1997, 2000]; 

print “Value available at index 2 :” 

print list[2]; list[2] = 2001; 

print “New value available at index 2 :” 

print list [2];

Note: append)) method is discussed in subsequent section.

When the above code is executed, it produces the following result:

Value available at index 2 : 

1997 

New value available at index 2 : 

2001

28.

Write the output of the given python code :#!/user/bin/python aList1 = [123, ‘xyz’, ‘zara’, ‘abc’]; aList.insert (3,2009) print “Final Lista List

Answer»

Output: 

Final List: [123, ‘xyz’, ’zara1, 2009,’abc1’]

29.

Write the output of the given python code :# !/user/bin/python aList1 = [123, ‘xyz’, ‘zara’, ‘abc’]; print “A List:”, aList.pop() print “B List:”, aList.pop(2)

Answer»

Output: 

A List: abc B List: zara

30.

How can we update and delete list in python.

Answer»

You can update single or multiple elements of lists by giving the slice on the left-hand side of the assignment operator, and you can add to elements in a list with the append() method. Following is a simple example:

# !/user/bin/python 

list = [‘physics’, ‘chemistry’, 1997, 2000]; 

print “Value available at index 2 :” 

print list[2]; 

list [2] = 2001; 

print “New value available at index 2 :” 

print list [2];

When the above code is executed it produces the following result:

Value available at index 2 : 

1997 New value available at index 2 : 

2001

Delete List Elements:

To remove a list element, you can use either the del statement if you know exactly which element(s) you are deleting of the remove() 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, 2001] 

After deleting value at index 2 : 

[‘physics’, ‘chemistry’, 2000]

31.

Explain list.extend(seq)

Answer»

Answer:

Appends the contents of seq to list.

32.

Is list.reverse() method return a value?

Answer»

Answer:

This method does not return any value but reverse the given object from the list

33.

What do you mean by list.count(obj)

Answer»

Answer:

Returns count how many times obj occurs in list

34.

Define list in Python.

Answer»

The list is a most versatile datatype available in Python which can be written as a list of comma- separated values (items) between square brackets. Good thing about a list is that items in a list need not all have the same type. Creating a list is as simple as putting different comma-separated values between square brackets.

For example :

list1 = [‘physics’, ‘chemistry’, 1997,2000];

list2 = [1,2, 3,4, 5];

list3 = ’[‘a”, “b”, “c”, “d”];

35.

Write the output of the given Python code :#!/user/bin/pythonlist1, list2 = [123, ‘xyz’], [456, ‘abc’]print cmpt(list1, list2);print cmp(list2, list1);list3 = list2 + [786];print cmp(list2, list3)

Answer» This will produce the following result:

-1

1

-1
36.

Write a program to perform various list operations after displaying menu.

Answer» ch = 0

list = []

while true :

print “List Menu”

print “1. Adding”

print “2.Modify”

print “3.Delete”

print “4.Sort list”

print “5.Display list”

print “6.Exit”

ch = int (raw_input (“Enter your choice :”)) if ch = = 1 :

print “1.Add element”

print “2.Add a List”

ch1 = int (raw_input

(“Enter choice 1 or 2:”))

if chi = = 1:

item = int

(raw_input (“Enter element:”))

pos = int (raw_input

(“Enter position to add : “))

list.insert (pos, item)

elif chi = = 2 :

list2 = eval (raw_input (“Enter list:”))

list.extend (lst2)

else:

print “Valid choices are 1 or 2”

print “Successfully added” elif ch = = 2 :

pos = int (raw_input

(“Enter position :”))

intem = int (raw_input

(“Enter new value :”))

old = list[pos]

list[.pos] = item

print old, ‘modified with vlaue”, item elif ch = = 3:

print “1.Delete element by position”

print “2.Delete element by value”

ch1 = int (raw_input (“Enter you choice 1 or 2’))

if chi = = 1:

pos = int (raw_input (“Enter position :”))

item = list.pop (pos)

print item, “deleted”

elif ch1 = = 2:

item = int (raw_input (“Enter element:”))

pos = list.remove (item)

print “Succcessfully delted”

else :

print “Valid choices are 1 or 2”

elif ch = = 4 :

print “l.Ascending”

print “2.Descending”

chi = int (raw input (“Enter choice 1 or 2”))

if chi = = 1:

list. sot () elif ch1 = = 2:

list.sort (reverse = True)

else :

print “Valid choices are 1 or 2”

elif ch = = 5:

print list elif ch = = 6:

break

else :

print “valid choice 1 to 6”
37.

Can we remove individual tuple elements ?

Answer» No, we cannot remove individual tuple elements.
38.

Write a program to perform various list operations after displaying menu.

Answer»

ch = 0 

list = [] 

while true : 

print “List Menu” 

print “1. Adding” 

print “2.Modify” 

print “3.Delete” 

print “4.Sort list” 

print “5.Display list” 

print “6.Exit”

ch = int (raw_input (“Enter your choice :”)) if ch = = 1 : 

print “1.Add element” 

print “2.Add a List” 

ch1 = int (raw_input 

(“Enter choice 1 or 2:”)) 

if chi = = 1: 

item = int

(raw_input (“Enter element:”)) 

pos = int (raw_input 

(“Enter position to add : “)) 

list.insert (pos, item) 

elif chi = = 2 : 

list2 = eval (raw_input (“Enter list:”))

list.extend (lst2) 

else: 

print “Valid choices are 1 or 2” 

print “Successfully added” elif ch = = 2 : 

pos = int (raw_input 

(“Enter position :”)) 

intem = int (raw_input 

(“Enter new value :”)) 

old = list[pos] 

list[.pos] = item 

print old, ‘modified with vlaue”, item elif ch = = 3: 

print “1.Delete element by position” 

print “2.Delete element by value”

ch1 = int (raw_input (“Enter you choice 1 or 2’)) 

if chi = = 1: 

pos = int (raw_input (“Enter position :”)) 

item = list.pop (pos)

print item, “deleted” 

elif ch1 = = 2: 

item = int (raw_input (“Enter element:”)) 

pos = list.remove (item) 

print “Succcessfully delted”

else : 

print “Valid choices are 1 or 2” 

elif ch = = 4 : 

print “l.Ascending” 

print “2.Descending” 

chi = int (raw input (“Enter choice 1 or 2”))

 if chi = = 1: 

list. sot () elif ch1 = = 2: 

list.sort (reverse = True) 

else :

print “Valid choices are 1 or 2” 

elif ch = = 5: 

print list elif ch = = 6: 

break

else : 

print “valid choice 1 to 6”

39.

How are lists different from strings when both are sequences ?

Answer»

The lists and strings are different in following ways : 

(a) The lists are mutable sequences while strings are immutable. 

(b) Strings store single type of elements, all characters while lists can store elements belonging to different types. 

(c) In consecutive locations, strings store the individual characters while list stores the references of its elements.

40.

Write the output of the given python code : #!/user/bin/python aList = [123, ‘xyz’, ‘zara’, ‘abc’];aList.insert (3,2009) print “Final List:”, aList

Answer»

Output: 

Final List: [123, ‘xyz’, ‘zara’, 2009, ‘abc’] 

41.

What is the use of list(seq) in Python ? 

Answer»

It converts a tuple into list.

42.

What is the use of list(seq) in Python ?

Answer»

Answer:

It converts a tuple into list.

43.

Write the output of the given python code :# ! ‘user/bin’pvthon aList1 = [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

44.

Describe max(list) method.

Answer»

Answer:

It returns item from the list with max value.

45.

Which method is used to sort objects of list.

Answer»

The method is used to sort objects of list is-

sort ()

46.

Describe min(list) method.

Answer»

Answer:

It returns item from the list with min value.

47.

Write the output of the given Python code : #!/user/bin/python list1, list2 = [123, ‘xyz’], [456, ‘abc’]print cmpt(list1, list2); print cmp(list2, list1); list3 = list2 + [786]; print cmp(list2, list3)

Answer»

This will produce the following result: 

-1 

-1

48.

Write a program to input n x m matrix and find the sum of all numbers.

Answer» def summat (a, m, n):

s = 0

for i in range (m):

for j in range (n):

s + = a[i][j]

return s
49.

Write a program to input any two matrices and print product of matrices

Answer»

import random 

m1 = input (“Enter number of rows in first matrix”) 

n1 = input (“Enter number of columns in first matrix”) 

a = [[random.random () for row in range (m1)] for col in range (n1)] 

for i in range (m1): 

for j in range (n1): 

a[i][j] = input () 

m2 = input (“Enter the number of rows in the second matrix”) 

n2 = input (“Enter the number of columns in the second matrix”) 

b = [[random.random () for row in range (m2)] for cal in range (n2)]

for i in range (m2): 

for j in range (n2): 

b[i][j] = input ()

c = [[random.random () for row in range (ml)] for col in range (n2)] 

if (n1 = = m2): 

for i in range (m1): 

for j in range (n2):

c[i][j] = 0 for kin range (n1): 

c[i][j] + = a[i][k]*b[k][j] 

print c[i][j], ‘\t’, 

print 

else: 

print “Multiplication is not possible” 

50.

Write a program to input any two matrices and print sum of matrices

Answer»

import random 

m1 = input (“Enter total number of rows in the first matrix”) 

n1 = input (“Enter total number of columns in the first matrix”) 

a = [[random.random() for row in range(m1) for col in range (n1)] 

for i in range (m1) 

for j in range (n1): 

a[i][j] = input()

m2 = input (“Enter total number of rows in the second matrix”) 

n2 = input (“Enter total number of columns in the second matrix”) 

b = [[random.random () for row in range (m2)] for col in range (n2)]

for i in range (m2):

 for j in rnage (n2):

b [i][j] = input () 

c = [[random.random () for row in range (ml)] for col in range (n1)]

 if ((m1 = m2) and (n1 = = b)): 

print “Output is” 

for i in range (m1):

for j in range (n1)] 

c[i][j] = a[i][j] + b[i][j] 

print c[i][j], ‘it’, 

print 

else : 

print “Matrix addition is not possible”