Python Tricky Interview Questions and Answers

 
 

1.

    try:
          print("try " , end = '')
    finally:
          print("finally ", end = '')
    except :
         print("except ", end = '')

 Answer: syntax error

Explanation: This code will result in a syntax error because there is a statement after the finally block, which is not allowed in Python.

2. What is the output of the below Python Code. 

s = { 23, 34, 12, 54, 32, 21}
print( s[0] )

Answer – The code you provided will result in a TypeError because you cannot access elements in a set using indexing, as sets are unordered and unindexed collections in Python.

3. What is the output of the below Python code?

a = ["Mohan", "Jyothi", "Kavitha"]
b = a
a.append("Sikander")
print(len(a) , len(b) )

Output – 4 4

Explanation – 

The code creates a list ‘a’ with three string elements: “Gajanan”, “Samir”, and “Prashant”.

The code references ‘b’ to the same list object as ‘a’. This means that ‘a’ and ‘b’ point to the same list in memory.

The code appends a new element “Pradip” to the end of the list ‘a’ using the ‘append()’ method.

The ‘len(a)’ and ‘len(b)’ functions are called, which return the number of elements in lists ‘a’ and ‘b’, respectively. 

Since ‘a’ and ‘b’ reference the same list object, the length of both lists will be the same, which is 4.

4. What is the output of the below Python code?

nums = [12, 21, 5, 2, 8]
print(nums.sort())

Answer – None

Explanation – 

The code creates a list ‘nums’ with five integer elements.

The ‘sort()’ method is called on the list ‘nums’. This method sorts the list in ascending order.

The ‘sort()’ method does not return a new sorted list. Instead, it sorts the list in place and returns ‘None’.

The ‘print()’ function is called with the return value of ‘nums.sort()’. Since ‘nums.sort()’ returns ‘None’, 

the output of the ‘print()’ function is ‘None’.

5. How many times does this loop repeat.

x = 0
while x < 10:
print(x)
++x

Explanation – Python does not support increment operators.

In the code, ++x does not give an error and will be treated as unary + applied twice, which does not increment the value of x.

Answer:

The loop runs infinite times.

6. What is the output of the below Python Code?

data ="GAJAnan"
data.upper()
print(data )

Answer – GAJAnan

Explanation – Strings in Python are immutable, 

data.upper() does not modify the string, instead, it returns a new string “GAJANAN” which is not captured in any variable.

Read This –

7. What is the output of the below Python Code?

x = [a, b, c, d]
for x[-1] in x:
    print(x[-1], end="")

Output – abcc

Explanation –

The code initializes a list ‘x’ with four elements a, b, c, and d.

The for loop iterates over each element in the list x. On each iteration, the last element of the list x[-1] is updated to the current element of the iteration.

Inside the loop, the print() function is called with the end parameter set to an empty string, which means that each value printed to the console will be concatenated with an empty string instead of a newline character.

On the first three iterations of the loop, the print() function outputs the current value of the last element of the list x[-1] to the console, which is updated to the current element of the iteration on each loop. So, the first three values output are a, b, and c.

On the last iteration of the loop, the current element of the iteration is also the last element of the list, which means that the last element of the list x[-1] is updated to itself. Since the last element of the list was c before the update, it remains c after the update. So, the print() function outputs c again on the last iteration.

At the end of the loop, the last element of the list x[-1] will be equal to the last element in the original list x, which is the element c.

So, the output of the print() function will be the concatenation of the first three elements in the list x, which are a, b, c, and the last updated 

value of the last element in the list x[-1], which is also c, resulting in the string “abcc”

8. From a list of numbers, move zero to the end of the list.

List = [1, 0, 2, 0, 4, 6]
Code  - for item in list:
                   if item == 0:
                          list.remove(item)
                          list.append(item)
            print(list)

Output – [1, 2, 4, 6, 0, 0]

9. Write code for the given Output

Input : “sky is blue”
Output : “blue is sky”

Code –

str1 = "sky is blue"
mylist = str1.split()
mylist = mylist[::-1]
str2 = " ".join(mylist)
print(str2)

10. Write code for the given Output

List : [1, 2, 2, 3, 3, 4, 5, 5, 5, 6, 6]
Output : [1, 4]

Code – 

nylist = [1, 2, 2, 3, 3, 4, 5, 5, 5, 6, 6]
new_list = []
for num in mylist:
     if mylist.count(num)==1:
                new_list.append(num)
print(new_list)

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top