Thursday 7 April 2022

Python Functions Interview questions

1. What will be the output of the following Python code?

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

for i in range(1, 6):

myList[i - 1] = myList[i]

for i in range(0, 6): 

print(myList[i], end = " ")

a) 2 3 4 5 6 1

b) 6 1 2 3 4 5

c) 2 3 4 5 6 6

d) 1 1 2 3 4 5


Answer: c

Explanation: Execute in the shell to verify.


2. What will be the output of the following Python code?

def narendra(values):

values[0] = 44


v = [1, 2, 3]

narendra(v)

print(v)

a) [1, 44]

b) [1, 2, 3, 44]

c) [44, 2, 3]

d) [1, 2, 3]


Answer: c

Explanation: Execute in the shell to verify.


3. What will be the output of the following Python code?

def narendra(i, values = []):

values.append(i)

return values

 

narendra(1)

narendra(2)

v = narendra(3)

print(v)

a) [1] [2] [3]

b) [1] [1, 2] [1, 2, 3]

c) [1, 2, 3]

d) 1 2 3


Answer: c

Explanation: Execute in the shell to verify


4. What will be the output of the following Python code?

def addItem(listParam):

listParam += [1]

 

mylist = [1, 2, 3, 4]

addItem(mylist)

print(len(mylist))

a) 1

b) 4

c) 5

d) 8


Answer: c

Explanation: + will append the element to the list.


5. What will be the output of the following Python code?

def increment_items(L, increment):

i = 0

while i < len(L):

L[i] = L[i] + increment

i = i + 1

 

values = [1, 2, 3]

print(increment_items(values, 2))

print(values)

a)    None

           [3, 4, 5]

b)    None

           [1, 2, 3]

c)    [3, 4, 5]

           [1, 2, 3]

d)    [3, 4, 5]

           None


Answer: a

Explanation: increment_items() function does not have a return keyword, so by default if any function does not have a return keyword, then that function will return None


6. What will be the output of the following Python code?

def example(L):

i = 0

result = []

while i < len(L):

result.append(L[i])

i = i + 3

return result

a) Return a list containing every third item from L starting at index 0

b) Return an empty list

c) Return a list containing every third index from L starting at index 0

d) Return a list containing the items from L starting from index 0, omitting every third item


Answer: a

Explanation: Run the code to get a better understanding of many arguments.


7. What will be the output of the following Python code?

     def maha(list):
            v = list[0]
        for e in list: # iterate inner list
        if v < e:
        v = e
        return v

        values = [[3, 4, 5, 1], [33, 6, 1, 2]]
        for row in values:
        print(maha(row), end=" ")
         a) 3 33

        b) 1 1

        c) 5 6

        d) 5 33


Answer: d

Explanation: Execute in the shell to verify.


8. What will be the output of the following Python code?

    def maha(m):
    v = m[0][0]
for row in m:
for element in row:
if v < element:
v = element
print(v)

    maha(data[0])

        a) 1

b) 2

c) 4

d) 5


Answer: c

Explanation: Execute in the shell to verify.


9.  What will be the output of the following Python code?

def unpack(a,b,c,d):

print(a+d)

x = [1,2,3,4]

unpack(*x)

a) Error

b) [1,4]

c) [5]

d) 5


Answer: d

Explanation: unpack(*x) unpacks the list into the separate variables. Now, a=1 and d=4. Thus 5 gets printed.


10. What will be the output of the following Python code?

def change(var, lst):

var = 1

lst[0] = 44

k = 3

a = [1, 2, 3]

change(k, a)

print(k)

print(a)


a) 3

[44, 2, 3]

b) 1

[1,2,3]

c) 3

[1,2,3]

d) 1

[44,2,3]


Answer: a

Explanation: A list is mutable, hence its value changes after the function call. However, the integer isn’t mutable. Thus its value doesn’t change


11. What will be the output of the following Python code snippet?

total={}

def insert(items):

if items in total:

total[items] += 1

else:

total[items] = 1

insert('Apple')

insert('Ball')

insert('Apple')

print (len(total))

a) 3

b) 1

c) 2

d) 0


Answer: c

Explanation: The insert() function counts the number of occurrences of the item being inserted into the dictionary. 

There are only 2 keys present since the key ‘Apple’ is repeated. Thus, the length of the dictionary is 2.


12. What will be the output of the following Python code?

a = [5,5,6,7,7,7]

b = set(a)

def test(lst):

if lst in b:

return 1

else:

return 0

for i in  filter(test, a):

print(i,end=" ")

a) 5 5 6

b) 5 6 7

c) 5 5 6 7 7 7

d) 5 6 7 7 7


Answer: c

Explanation: The filter function will return all the values from the list a which are true when passed to the function test.

No comments:

Post a Comment