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.

101.

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

Answer» Output:

A List: abc B List: zara
102.

Write a program to find all duplicates in a list.

Answer»

a =[1,2,3,2,1,5,6,5,5,5] 

d={ } 

for elem in a: 

if elem in 

d: d[elem] + = 1 

else: 

d[elem] = 1 

print elem 

print [x for x, y in d.items() if y > 1], 

print “have duplicates” 

print d 

Output screenshot:

[1,2,5] have duplicates 

{1:2,2: 2,3:1,5:4, 6: 1}

103.

What is string in Python and how can we access values in list.

Answer»

The most basic data structure in Python is the sequence. Each element of a sequence is assigned a number – its position or index. The first index is zero, the second index is one, and so forth. Python has six built -in types of sequences, but the most common ones are lists and tuples. 

There are certain things you can do with all sequence types. These operations include indexing, slicing, adding, multiplying, and checking for membership. In addition, Python has built-in functions for finding the length of a sequence and for finding its largest and smallest elements.

Python Lists:

The list is a most versatile datatype available in Python which can be written as a list of comma- seprated values (items) between 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”];

Like string indices, list indices start at 0, and lists can be sliced, concatenated and so on.

Accessing Values in Lists :

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

# !/user/bin/python 

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

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

print “list1 [0]:”, list1 [0] 

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

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

104.

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
105.

Write a program to calculate and display the sum of all the odd numbers in the list.

Answer»

pos = 0 

sum = 0 

while pos < len (L): 

if L[pos] %2 = = 1 : 

sum = sum + L [pos] 

pos = pos + 1 

print sum 

106.

Write a program to calculate and display the sum of all the odd numbers in the list.

Answer» pos = 0

sum = 0

while pos < len (L):

if L[pos] %2 = = 1 :

sum = sum + L [pos]

pos = pos + 1

print sum
107.

…………. is used to separate the elements in the dictionary

Answer»

Comma is used to separate the elements in the dictionary

108.

The range( ) function has ……arguments.(a) 1(b) 2(c) 3(d) 4

Answer»

The range( ) function has 3 arguments.

109.

The ……… function is used to create list in python

Answer»

The list( ) function is used to create list in python

110.

If the range is specified as [1 : 5], it will update the elements from ………..(a) 2 to 4 (b) 1 to 5 (c) 1 to 4 (d) 2 to 5

Answer»

If the range is specified as [1 : 5], it will update the elements from 1 to 4

111.

To delete an entire tuple, ………command is used. (a) del (b) delete (c) clear (d) remove

Answer»

To delete an entire tuple, del command is used.

112.

Which operator is used to do difference in set?(a) + (b) – (c) : (d) &amp;

Answer»

 is used to do difference in set

113.

Which operator joins two sets?(a) + (b) | (c) || (d) &amp;

Answer»

| operator joins two set

114.

The position of an element is indexed with numbers beginning with ………(a) n(b) n-1 (c) 0(d) 1

Answer»

The position of an element is indexed with numbers beginning with 0

115.

…… statement deletes the entire list.

Answer»

del statement deletes the entire list.

116.

Write a program that finds sum of all even numbers in a list?

Answer»

numlist = [ ]

evensum = 0

number = int(input(“Please enter the total no of list elements”))

for i in range(1, number +1):

value = int(input(“Please enter the value “)) 

numlist.append(value)

for j in range(number):

if(numlist[j]% 2 == 0):

even_sum = even_sum + numlist[j]

print(“Sum of even no. in this list = “ , even_sum) 

Output:

Please enter the total no of list elements : 5

Please enter the value : 10

Please enter the value : 11

Please enter the value : 12

Please enter the value : 13

Please enter the value : 14

The sum of even no. in this list = 60

117.

How will you create a list in python? Explain with syntax and examples?

Answer»

In python, a list is simply created by using square bracket. The elements of list should be specified within square brackets. The following syntax explains the creation of list.

Syntax:

Variable = [element – 1, element – 2, element – 3 element – n]

Example:

Marks = [10, 23, 41, 75]

118.

What is List in Python?

Answer»

A list in Python is known as a “sequence data type” like strings. It is an ordered collection of values enclosed within square brackets [ ]. Each value of a list is called as element.

119.

Write a program that reverse a list using a loop?

Answer»

def reverse(list):

list.reverse( )

return list list = [10, 11, 12, 13, 14, 15] 

print(reverse(list))

Output:

15, 14, 13, 12, 11, 10

120.

Match the following(1) mylist[ ] – (i) tuple(2) mylist[10,[2,4,6]] – (ii) Empty tuple(3) t=(23,56,89) – (iii) Nested list(4) lis=( ) – (iv) empty list(a) 1-(iv), 2-(iii), 3-(i), 4-(ii) (b) 1-(i), 2-(ii), 3-(iii), 4-(iv) (c) 1-(iv), 2-(ii), 3-(i), 4-(iii) (d) 1-(i), 2-(iii), 3-(iv), 4-(ii)

Answer»

(a) 1-(iv), 2-(iii), 3-(i), 4-(ii)

121.

list = [34, 45, 48]list.extend([71, 32, 29]) results in ………

Answer»

[35, 45, 48, 71, 32, 29]

122.

How will you create a list with series of value?

Answer»

Using the range( ) function, you can create a list with series of values. To convert the result of range( ) function into list, we need one more function called list( ).

The list( ) function makes the result of range( ) as a list.

Syntax:

List_Varibale = list (range ( ))

123.

Fill the tablemarks = [10, 23, 41, 75]marks10234175Positive IndexNegative Index

Answer»

marks = [10, 23, 41, 75]

marks10234175
Positive Index0123
Negative Index-4-3-2-1
124.

……….. function is used to add a single element in the list.

Answer»

append( ) function is used to add a single element in the list.

125.

How will you access the list elements in reverse order?

Answer»
FunctionDescriptionSyntaxExample
reverse()Reverses the order of the element in the list.List.reverse()MyList = [36,23,12]
My List.reverse()
Print(MyList)
Output:
[12,23,36]
126.

…………… function is used to generate a series of values in python(a) range (b) series (c) Fill series (d) Auto fill

Answer»

range function is used to generate a series of values in python

127.

Differentiate clear( ) and del in list?

Answer»

The function clear( ) is used to delete all the elements in list, it deletes only the elements and retains the list. Remember that, the del statement deletes entire list.

128.

The keys in Python, dictionary is specified by ........(a) =(b) ;(c) +(d) :

Answer»

Answer is (d) :

129.

Find the correct statement from the following(a) when new element is inserted in the list, the existing elements shift one position to the right(b) when a new element is inserted in the list, the existing element shifts one position to the left.

Answer»

(a) when new element is inserted in the list, the existing elements shift one position to the right

130.

…………. are used to access all elements from a list. (a) If (b) loop (c) array (d) tuple

Answer»

loop are used to access all elements from a list.

131.

Fill in the blanks.(i) ….. function deletes the element using the given index value.(ii) When you try to print the list which is already cleared, ……. is display without any elements

Answer»

(i) pop( )

(ii) [ ] or empty square bracket

132.

To access the list elements in reverse order, ……. value have to be given(a) 0 (b) positive(c) imaginery(d) negative

Answer»

(d) negative

133.

Join is called as ……… in sets(a) union(b) intersection(c) difference(d) symmetric difference

Answer»

Join is called as union in sets

134.

What is nested tuple? Explain with an example.?

Answer»

In Python, a tuple can be defined inside another tuple; called Nested tuple. In a nested tuple, each tuple is considered as an element. The for loop will be useful to access all the elements in a nested tuple.

Example:

Toppers = ((“Vinodini” , “XII-F”, 98.7), (“Soundarya” , “XII-H” , 97.5),

(“Tharani” , “XII-F”, 95.3), (“Saisri” , “XII-G” , 93.8)) 

for i in Toppers:

print(i)

Output:

(‘Vinodini’ , ‘XII-F’, 98.7)

(‘Soundarya’ , ‘XII-H’ , 97.5)

(‘Tharani’ , ‘XII-F’, 95.3)

(‘Saisri’ , ‘XII-G’ , 93.8)

135.

Write the output, list = [34, 45, 48] list.append(90)(a) [34, 45, 48, 90](b) [90, 34, 45, 48](c) [34, 90, 45, 48](d) [34, 45, 90, 48]

Answer»

(a) [34, 45, 48, 90]

136.

Explain the difference between del and clear( ) in dictionary with an example?

Answer»

In Python dictionary, del keyword is used to delete a particular element. The clear( ) function is used to delete all the elements in a dictionary. To remove the dictionary, we can use del keyword with dictionary name.

Dict={‘Roll No’: 12001, ‘SName’: ‘Meena’ , ‘Mark1’: 98, ‘Mar12’: 86}

print(“Dictionary elements before deletion: \n” , Dict) del Dict[‘Mark1’]

# Deleting a particular element Dict.clear( )

# Deleting all elements

137.

Find the Output:marks = [10, 23, 41, 75]i = -1while i &gt;= – 4:print(marks[i]) i = i + – 1(a) 1 2 3 4 (b) 10, 23, 41, 75 (c) 75, 41, 23, 10 (d) 0, 41, 23, 0

Answer»

(c) 75, 41, 23, 10

138.

Index value can be positive or negative in the list. True / false

Answer»

Answer is True

139.

……… function is used to insert an element at any position of a list.

Answer»

insert( ) function is used to insert an element at any position of a list.

140.

……… is used to access an element in a list(a) element(b) i(c) index(d) tuple

Answer»

index is used to access an element in a list

141.

Write a program to insert a value in a list at the specified location?

Answer»

vowel = [‘a’ , ‘e’ , ‘i’ , ‘u’]

vowel.insert(3, ‘o’)

print(‘updated list’ , vowel)

Output:

updated list [‘a’ , ‘e’ , ‘i’ , ‘o’, ‘u’]

142.

What is the purpose of range( )? Explain with an example?

Answer»

(i) The range( ) is a function used to generate a series of values in Python. Using range( ) function, you can create list with series of values. The range( ) function has three arguments.

Syntax of range( ) function: 

range (start value, end value, step value) where,

  • start value – beginning value of series. Zero is the default beginning value.
  • end value – upper limit of series. Python takes the ending value as upper limit – 1.
  • step value – It is an optional argument, which is used to generate different interval of values.

Example: Generating whole numbers upto 10 for x in range (1, 11):

print(x)

Output

1

2

3

4

5

6

7

8

9

10

(ii) Creating a list with series of values Using the range( ) function, you can create a list with series of values. To convert the result of range( ) function into list, we need one more function called list( ). The list( ) function makes the result of range( ) as a list.

Syntax:

List_Varibale = list ( range ( ) )

Note

The list( ) function is all so used to create list in python.

Example

>>> Even_List = list(range(2,11,2))

>>> print(Even_List)

[2, 4, 6, 8, 10]

In the above code, list( ) function takes the result of range( ) as Even List elements. Thus, Even _List list has the elements of first five even numbers.

(iii) We can create any series of values using range( ) function. The following example explains how to create a list with squares of first 10 natural numbers.

Example: Generating squares of first 10 natural numbers

squares = [ ]

for x in range(1,11):

s = x ** 2

squares.append(s)

print (squares)

Output

[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]

143.

Identify the intersection operator.(a) + (b) – (c) . (d) &amp;

Answer»

& is intersection operator

144.

Write a program that creates a list of numbers from 1 to 50 that are either divisible by 3 or divisible by 6?

Answer»

n = [ ]

s = [ ]

for x in range(1, 51):

n.append(x)

for x in range(1, 51):

if(x%3 == 0) or (x % 6 == 0):

s.append(x)

print(“The numbers divisible by 3 or 6 is “ , s)

Output:

The numbers divisible by 3 or 6 is

[3, 6, 9, 12, 15, 18, 21, 24, 27, 30, 33, 36, 39, 42, 45, 48]

145.

Write a program that counts the number of times a value appears in the list. Use a loop to do the same?

Answer»

a = [ ]

n = int(input”Enter number of elements :”))

for i in range(l, n+1):

b = int(input(“Enter element”))

a.append(b)

k = 0

num = int(input(“Enter the number to be counted : “)) for j in a:

if(j == num):

k = k+1

print(“Number of times” , num, “appears is” , k)

Output:

Enter number of elements : 4

Enter element: 23

Enter element: 45

Enter element: 23

Enter element: 67

Enter the number to be counted : 23

Number of times 23 appears is 2

146.

…….. operator is used to change the list of elements (a) = (b) + (c) += (d) *=

Answer»

= operator is used to change the list of elements

147.

What will be the output of the following code?

Answer»

list=[2**x for x in range(5)

print(list)

[1, 2, 4, 8, 16]

148.

A list in python is denoted by ………(a) [ ] (b) { } (c) &lt;&gt; (d) #

Answer»

A list in python is denoted by [ ]

149.

Each value of a list is called as –(a) Set(b) Dictionary(c) Element(d) Strings

Answer»

Each value of a list is called as Element

150.

What the different ways to insert an element in a list. Explain with suitable example. Inserting elements in a list?

Answer»

append( ) function in Python is used to add more elements in a list. But, it includes elements at the end of a list. If you want to include an element at your desired position, you can use insert () function is used to insert an element at any position of a list.

Syntax:

List, insert (position index, element)

Example:

>>> MyList=[34,98,47, ‘Kannan’ , ‘Gowrisankar’ , ‘Lenin’ , ‘Sreenivasan’ ]

>>> print(MyList)

[34, 98, 47, ‘Kannan’ , ‘Gowrisankar’ , ‘Lenin’ , ‘Sreenivasan’]

>>> MyList.insert(3, ‘Ramakrishnan’)

>>> print(MyList)

[34, 98, 47, ‘Ramakrishnan’ , ‘Kannan’ , ‘Gowrisankar’ , ‘Lenin’ , ‘Sreenivasan’]

In the above example, insertf) function inserts a new element ‘Ramakrishnan’ at the index value 3, ie. at the 4th position. While inserting a new element in between the existing elements, at a particular location, the existing elements shifts one position to the right.