Python interview questions part 25

Python interview questions part 25

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? print([if i%2==0: i; else: i+1; for i in range(4)])

Correct Answer is : error

2. Which of the following is the same as list(map(lambda x: x**-1, [1, 2, 3]))?

Correct Answer is : [1/x for x in (1, 2, 3)].

3. What is the output of the code shown? l=[1,2,3,4,5] [x&1 for x in l]

Correct Answer is : [1, 0, 1, 0, 1]

4. What is the output of the code shown below? l1=[1,2,3] l2=[4,5,6] [x*y for x in l1 for y in l2]

Correct Answer is : [4, 5, 6, 8, 10, 12, 12, 15, 18]

5. Write the list comprehension to pick out only negative integers from a given list ‘l’.

Correct Answer is : [x for x in l if x<0]

6. What is the output of the code shown? s=["pune", "mumbai", "delhi"] [(w.upper(), len(w)) for w in s]

Correct Answer is : [(‘PUNE’, 4), (‘MUMBAI’, 6), (‘DELHI’, 5)]

7. What is the output of the code shown below? l1=[2,4,6] l2=[-2,-4,-6] for i in zip(l1, l2): print(i)

Correct Answer is : (2, -2)

8. What is the output of the following code? l1=[10, 20, 30] l2=[-10, -20, -30] l3=[x+y for x, y in zip(l1, l2)] l3

Correct Answer is : [0, 0, 0]

9. Write a list comprehension for number and its cube for l=[1, 2, 3, 4, 5, 6, 7, 8, 9].

Correct Answer is : [x**3 for x in l]

10. What is the output of the code shown below? l=[[1 ,2, 3], [4, 5, 6], [7, 8, 9]] [[row[i] for row in l] for i in range(3)]

Correct Answer is : [[1, 4, 7], [2, 5, 8], [3, 6, 9]]

11. What is the output of the code shown below? import math [str(round(math.pi)) for i in range (1, 6)]

Correct Answer is : [‘3’, ‘3’, ‘3’, ‘3’, ‘3’]

12. What is the output of the code shown below? l1=[1,2,3] l2=[4,5,6] l3=[7,8,9] for x, y, z in zip(l1, l2, l3): print(x, y, z)

Correct Answer is : 39086

13. Read the information given below carefully and write a list comprehension such that the output is: [‘e’, ‘o’] w="hello" v=('a', 'e', 'i', 'o', 'u')

Correct Answer is : [x for x in w if x in v]

14. What is the output of the code shown below? [ord(ch) for ch in 'abc']

Correct Answer is : [97, 98, 99]

15. What is the output of the code shown below? t=32.00 [round((x-32)*5/9) for x in t]

Correct Answer is : Error

16. Write a list comprehension for producing a list of numbers between 1 and 1000 that are divisible by 3.

Correct Answer is : [x for x in range(1000) if x%3==0]

17. Write a list comprehension equivalent for the code shown below: for i in range(1, 101): if int(i*0.5)==i*0.5: print(i)

Correct Answer is : [i for i in range(1, 101) if int(i*0.5)==(i*0.5)]

18. What is the list comprehension equivalent for: list(map(lambda x:x**-1, [1, 2, 3]))

Correct Answer is : [x**-1 for x in [1, 2, 3]]

19. Write a list comprehension to produce the list: [1, 2, 4, 8, 16……212].

Correct Answer is : [(2**x) for x in range(0, 13)]

20. What is the list comprehension equivalent for: {x : x is a whole number less than 20, x is even} (including zero)

Correct Answer is : [x for x in range(0, 20) if (x%2==0)]

Similar Interview Questions

    Search for latest jobs

    Icon
    Icon