Python interview questions part 14

Python interview questions part 14

Take as many assements as you can to improve your validate your skill rating

Total Questions: 20

1. What is the output of the following? try: if '1' != 1: raise "someError" else: print("someError has not occurred") except "someError": print ("someError has occurred")

Correct Answer is : invalid code

2. What happens when ‘1’ == 1 is executed?

Correct Answer is : we get a False

3. The code shown below will result in an error if the input value is entered as -5. State whether this statement is true or false. assert False, 'Spanish'

Correct Answer is : TRUE

4. What is the output of the code shown below? x=10 y=8 assert x>y, 'X too small'

Correct Answer is : No output

5. What is the output of the code shown below? #generator def f(x): yield x+1 g=f(8) print(next(g))

Correct Answer is : 9

6. What is the output of the code shown below? def f(x): yield x+1 print("test") yield x+2 g=f(9)

Correct Answer is : No output

7. What is the output of the code shown below? def f(x): yield x+1 print("test") yield x+2 g=f(10) print(next(g)) print(next(g))

Correct Answer is : 11

8. What is the output of the following code? def a(): try: f(x, 4) finally: print('after f') print('after f?') a()

Correct Answer is : error

9. What is the output of the code shown? def f(x): for i in range(5): yield i g=f(8) print(list(g))

Correct Answer is : [0, 1, 2, 3, 4]

10. The error displayed in the code shown below is: import itertools l1=(1, 2, 3) l2=[4, 5, 6] l=itertools.chain(l1, l2) print(next(l1))

Correct Answer is : ‘tuple’ object is not iterator

11. Which of the following is not an exception handling keyword in Python?

Correct Answer is : accept

12. What is the output of the code shown below? g = (i for i in range(5)) type(g)

Correct Answer is : class <’generator’>

13. What happens if the file is not found in the code shown below? a=False while not a: try: f_n = input("Enter file name") i_f = open(f_n, 'r') except: print("Input file not found")

Correct Answer is : No error

14. What is the output of the code shown below? lst = [1, 2, 3] lst[3]

Correct Answer is : IndexError

15. What is the output of the code shown below? t[5]

Correct Answer is : NameError

16. What is the output of the following code, if the time module has already been imported? 4 + '3'

Correct Answer is : TypeError

17. The output of the code shown below is: int('65.43')

Correct Answer is : ValueError

18. Compare the two codes shown below and state the output if the input entered in each case is -6? CODE 1 import math num=int(input("Enter a number of whose factorial you want to find")) print(math.factorial(num))   CODE 2 num=int(input("Enter a number of whose factorial you want to find")) print(math.factorial(num))

Correct Answer is : ValueError, NameError

19. What is the output of the code shown below? def getMonth(m): if m<1 or m>12: raise ValueError("Invalid") print(m) getMonth(6)

Correct Answer is : 6

20. What is the output of the code shown below if the input entered is 6? valid = False while not valid: try: n=int(input("Enter a number")) while n%2==0: print("Bye") valid = True except ValueError: print("Invalid")

Correct Answer is : Bye (printed infinite number of times)

Similar Interview Questions

    Search for latest jobs

    Icon
    Icon