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.

51.

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”
52.

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”
53.

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”
54.

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

Answer»

Answer:

List index (obj).

55.

Which function is use for removing object obj from list.

Answer»

Answer:

list.remove(obj)

56.

Give an example to access values in lists.

Answer»

To access value in lists, use the square brackets for slicking along with the index or indices to obtain value available at that index. Following is a simple example :

# !/user/bin/python

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

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

print “list1 [0]:”,

list1 [0] print “list2[1 :5]:”, list2[1:5]

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

list1 [0]: physics

list2[1:5] : [2, 3, 4, 5]

57.

Write a program that prints the maximum and minimum value in a dictionary?

Answer»

my_dict = {‘x’: 500, ‘y’: 5874, ‘z’: 560}

val = my_dict.values( )

print(‘max value’ , max(val))

print(‘min value’ , min(val))

Output:

max value 5874

min value 500

58.

Write a program that finds the sum of all the numbers in a Tuples using while loop?

Answer»

tuple = (1, 5, 12)

s = 0

i = 0

while(i < len (tuple)):

s = s + tuple[i]

i+ = 1

print(“Sum of elements in tuple is “ , s)

Output:

Sum of elements in tuple is 18

59.

Write a program that prints the maximum value in a Tuple?

Answer»

tuple = (456, 700, 200)

print(“max value : “ , max(tuple))

Output:

max value : 700

60.

Write a program to swap two values using tuple assignment Program to swap two values using tuple assignment

Answer»

a = int(input(“Enter value of A: “))

b = int(input(“Enter value of B: “))

print(“Value of A = “ , a, “\n Value of B = “ , b) (a, b) = (b, a)

print(“Value of A = “ , a, “\n Value of B = “ , b)

Output:

Enter value of A: 54

Enter value of B: 38

 Value of A = 54 

Value of B = 38 

Value of A = 38 

Value of B = 54

61.

Write a program to join two tuple assignment?

Answer»

# Program to join two tuples

Tup 1 = (2,4,6,8,10)

Tup2 = (1,3,5,7,9)

Tup3 = Tup1 + Tup2

print(Tup3)

Output

(2,4,6,8,10,1,3,5,7,9)

62.

Write note on list comprehensions?

Answer»

List comprehensions:

List comprehension is a simplest way of creating sequence of elements that satisfy a certain condition. 

Syntax:

List = [expression for variable in range]

63.

Write note on dictionary comprehensions?

Answer»

In Python, comprehension is another way of creating dictionary. The following is the syntax of creating such dictionary.

Syntax

Diet = { expression for variable in sequence [if condition] }

64.

How many values can be returned by the functions in python?(a) 1(b) 2 (c) 3 (d) many

Answer»

many values can be returned by the functions in python

65.

Which is true related to sets?(a) mutable(b) unordered(c) No duplicates(d) All are true

Answer»

(d) All are true

66.

Fill in the blanks.1. A tuple defined in another tuple is called as ……2. ………. feature is used to include membership testing and eliminate duplicate elements.3. A …… is a mutable and an unordered collection of elements without duplicates.

Answer»

1. Nested tuple

2. Set

3. Set

67.

Write a python program using list to read marks of six subjects and to print the marks scored in each subject and show the total marks

Answer»

marks=[ ]

subjects=[‘Tamir, ‘English’ , ‘Physics’ , ‘Chemistry’ , ‘Comp. Science’ , ‘Maths’]

for i in range(6):

m=int(input(“Enter Mark = “))

marks.append(m)

for j in range(len(marks)):

print(“{ }. { } Mark= { } “.format(jl+,subjects[j],marks[j]))

print(“Total Marks = “ , sum(marks))

Output

  • Enter Mark = 45
  • Enter Mark = 98
  • Enter Mark = 76
  • Enter Mark = 28
  • Enter Mark = 46
  • Enter Mark = 15

1. Tamil Mark = 45

2. English Mark = 98

3. Physics Mark = 76

4. Chemistry Mark = 28

5. Comp. Science Mark = 46

6. Maths Mark = 15

7. Total Marks = 308

68.

...... assignment is a powerful feature in python.

Answer»

Tuple assignment is a powerful feature in python

69.

Find the wrong tuple.(a) mytup = (10)(b) mytup = (10)(c) print(tup[: ])(d) tup(10, 20)

Answer»

(d) tup(10, 20)

70.

(x, y) = (3**2, 15%2)print(x,y) gives the answer (a) 6 1 (b) 6 7 (c) 9 1 (d) 9 7

Answer»

Answer is (c) 9 1

71.

Write a program using a function that returns the area and circumference of a circle whose radius is passed as an argument two values using tuple assignment. Program using a function that returns the area and circumference of a circle whose radius is passed as an argument. Assign two values using tuple assignment:

Answer»

pi = 3.14

def Circle(r):

return (pi*r*r, 2*pi*r)

radius = float(input(“Enter the Radius! “))

(area, circum) = Circle(radius)

print (“Area of the circle = “ , area)

print (“Circumference of the circle = “ , circum) 

Output:

Enter the Radius: 5

Area of the circle = 78.5

Circumference of the circle = 31.400000000000002

72.

…….. returns the index value of the first recurring element.

Answer»

index( ) returns the index value of the first recurring element.

73.

Fill in the blanks.1. ……… is a simplest way of creating sequence of elements that satisfy a certain conditions returns copy of the list.2. …….. returns copy of the list

Answer»

1. List comprehension

2. copy( )

74.

Write a program using list to generate the Fibonacci series and find sum. Program to generate in the Fibonacci series and store it in a list. Then find the sum of all values?

Answer»

a=-1

b=1

n=int(input(“Enter no. of terms: “))

i=0

sum=0

Fibo=[ ] while i<n:

s = a + b

Fibo.append(s)

sum+=s

a = b

b = s

i+=1

print(“Fibonacci series upto “+ str(n) +” terms is : ” + str(Fibo))

print(“The sum of Fibonacci series: “ ,sum)

Output

Enter no. of terms: 10

Fibonacci series upto 10 terms is : [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]

The sum of Fibonacci series: 88

75.

Pick odd one in connection with collection data type? (a) List (b) Tuple (c) Dictionary (d) Loop

Answer»

Answer is (d) Loop

76.

Which of the following Python function can be used to add more than one element within an existing list?(a) append( )(b) append_more( )(c) extend( )(d) more( )

Answer»

(c) extend( )

77.

What will be the value of x in following python code?

Answer»

List1=[2, 4, 6, [1, 3, 5]]

x=len(List1)

4

78.

What is the use of type( ) function in python?(a) To create a Tuple(b) To know the type of an element in tuple.(c) To know the data type of python object.(d) To create a list.

Answer»

(c) To know the data type of python object.

79.

Which of the following statement is not correct?(a) A list is mutable(b) A tuple is immutable.(c) The append( ) function is used to add an element. (d) The extend( ) function is used in tuple to add elements in a list.

Answer»

(d) The extend( ) function is used in tuple to add elements in a list.

80.

Which of the following set operation includes all the elements that are in two sets but not the one that are common to two sets?(a) Symmetric difference(b) Difference(c) Intersection(d) Union

Answer»

(a) Symmetric difference

81.

Let set A={3, 6, 9}, set B={1, 3, 9} What will be the result of the following snippet?print (setA|setB)(a) {3, 6, 9, 1, 3, 9}(b) {3, 9} (c) {1}(d) {1, 3, 6, 9}

Answer»

(d) {1, 3, 6, 9}

82.

If List=[17, 23, 41, 10] then List.append (32) will result –(a) [32, 17, 23, 41, 10](b) [17, 23, 41, 10, 32](c) [10, 17, 23, 32, 41](d) [41, 32, 23, 17, 10]

Answer»

(b) [17, 23, 41, 10, 32]

83.

How will you create a set in python?

Answer»

A set is created by placing all the elements separated by comma within a pair of curly brackets. The set( ) function can also used to create sets in Python. 

Syntax:

Set Variable = {El, E2, E3 ………………. En} 

Example:

>>> S1={1,2,3, ’A’ ,3.14}

>>> print(S1)

{1,2, 3, 3.14, ‘A’}

>>> S2={1,2,2, ’A’ ,3.14}

>>> print(S2)

{1,2, ’A’ , 3.14}

84.

If List=[10, 20, 30, 40, 50] then List[2]=35 will result – (a) [35, 10, 20, 30, 40, 50](b) [10, 20, 30, 40, 50, 35](c) [10, 20, 35, 40, 50](d) [10, 35, 30, 40, 50]

Answer»

(c) [10, 20, 35, 40, 50]

85.

Which of the following function is used to count the number of elements in a list?(a) Count( )(b) Find( )(c) Len( )(d) Index( )

Answer»

Answer is (c) Len( )

86.

x = mylist = [36, 12, 12]x = mylist.count(12) print(x) gives the vlaue as(a) 2(b) 3(c) 0(d) 1

Answer»

Answer is (a) 2

87.

Pick the odd one with deleting elements from a list. (a) del (b) remove( ) (c) pop( )(d) clear

Answer»

Answer is (d) clear

88.

Write note on nested list?

Answer»

Mylist = [ “Welcome” , 3.14, 10, [2, 4, 6] ]

In the above example, Mylist contains another list as an element. Nested list is a list containing another list as an element.

89.

The two ways of deleting elements from a list are …….. and ……

Answer»

del and remove( )

90.

How many ways of deleting the elements from a list are there?(a) 1 (b) 2 (c) 3 (d) 4

Answer»

There are 2 ways of deleting the elements from a list.

91.

What is meant by Reverse Indexing?

Answer»

Python enables reverse or negative indexing for the list elements. Thus, python lists index in opposite order. The python sets -1 as the index value for the last element in list and -2 for the preceding element and so on. This is called as Reverse Indexing.

92.

Write note on dictionaries. Give syntax?

Answer»

A dictionary is a mixed collection of elements. The dictionary type stores a key along with its element. The keys in a Python dictionary is separated by a colon ( : ) while the commas work as a separator for the elements. The key value pairs are enclosed with curly braces { }. Syntax of defining a dictionary:

Dictionary_Name =

{ Key_l: Value_1,

Key_2: Value_2, 

…………..

Key_n: Value_n

}

93.

Give the syntax for append, extend and insert?

Answer»

Syntax:

List.append (element to be added)

List, extend ( [elements to be added])

List, insert (position index, element)

94.

How will you find the length of the list. Give example?

Answer»

The len( ) function in Python is used to find the length of a list, (i.e., the number of elements in a list). Usually, the len( ) function is used to set the upper limit in a loop to read all the elements of a list. If a list contains another list as an element, len( ) returns that inner list as a single element. 

Example : Accessing single element

>>> MySubject = [“Tamil” , “English” , “Comp. Science” , “Maths”]

>>> len(MySubject)

4

95.

Give the syntax to access an element from a list?

Answer»

To access an element from a list, write the name of the list, followed by the index of the element enclosed within square brackets.

Syntax:

List_Variable = [El, E2, E3 …………….. En]

print (List_Variable [index ofa element])

96.

Creating a tuple with one element is called ….. tuple.

Answer»

Creating a tuple with one element is called Singleton tuple.

97.

Differentiate del with remove( ) function of List?

Answer»

There are two ways to delete an element from a list viz. del statement and remove( ) function, del statement is used to delete known elements whereas remove( ) function is used to delete elements of a list if its index is unknown. The del statement can also be used to delete entire list.

98.

What is set in Python?

Answer»

In python, a set is another type of collection data type. A Set is a mutable and an unordered collection of elements without duplicates. That means the elements within a set cannot be repeated. This feature used to include membership testing and eliminating duplicate elements.

99.

Write the syntax of creating a Tuple with n number of elements?

Answer»

# Tuple with n number elements

Tuple _ Name = (E1, E2, E2 ……… En)

# Elements of a tuple without parenthesis 

Tuple_Name = E1, E2, E3 ……. En

100.

Write a program that creates a list of numbers from 1 to 20 that are divisible by 4. Program to create a list of numbers from 1 to 20 that are divisible by 4

Answer»

divBy4=[ ]

for i in range(21):

if (i%4= =0):

divBy4.append(i)

print(divBy4)

Output

[0, 4, 8, 12, 16, 20]