Python interview questions part 12

Python interview questions part 12

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 piece of code? >>>import collections >>> b=collections.Counter([2,2,3,4,4,4]) >>> b.most_common(1)

Correct Answer is : [(4, 3)].

2. What is the output of the following piece of code? >>> import collections >>> a=collections.Counter([2,2,3,3,3,4]) >>> b=collections.Counter([2,2,3,4,4]) >>> a|b

Correct Answer is : Counter({3: 3, 2: 2, 4: 2})

3. What is the output of the following piece of code? >>> import collections >>> a=collections.Counter([3,3,4,5]) >>> b=collections.Counter([3,4,4,5,5,5]) >>> a&b

Correct Answer is : Counter({3: 1, 4: 1, 5: 1})

4. The following piece of code is invalid. True or False? class demo(dict): def __test__(self,key): return [] a = demo() a['test'] = 7 print(a)

Correct Answer is : FALSE

5. What is the output of the following code? count={} count[(1,2,4)] = 5 count[(4,2,1)] = 7 count[(1,2)] = 6 count[(4,2,1)] = 2 tot = 0 for i in count: tot=tot+count[i] print(len(count)+tot)

Correct Answer is : 16

6. What is the output of the following code? a={} a[2]=1 a[1]=[2,3,4] print(a[1][1])

Correct Answer is : 3

7. What is the output of the following piece of code? >>> a={'B':5,'A':9,'C':7} >>> sorted(a)

Correct Answer is : [‘A’,’B’,’C’].

8. What is the output of the following snippet of code? >>> a={i: i*i for i in range(6)} >>> a

Correct Answer is : {0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 25}

9. What is the output of the following piece of code? >>> a={} >>> a.fromkeys([1,2,3],"check")

Correct Answer is : {1:”check”,2:”check”,3:”check”}

10. What is the output of the following snippet of code? >>> b={} >>> all(b)

Correct Answer is : TRUE

11. If b is a dictionary, what does any(b) do?

Correct Answer is : Returns True if any key of the dictionary is true

12. What is the output of the following code? >>> a={"a":1,"b":2,"c":3} >>> b=dict(zip(a.values(),a.keys())) >>> b

Correct Answer is : {1: ‘a’, 2: ‘b’, 3: ‘c’}

13. What is the output of the following piece of code when executed in Python shell? >>> a={i: 'A' + str(i) for i in range(5)} >>> a

Correct Answer is : {0: ‘0’, 1: ‘1’, 2: ‘2’, 3: ‘3’, 4: ‘4’}

14. What is the output of the following piece of code when executed in Python shell? >>> a=dict() >>> a[1]

Correct Answer is : An exception is thrown since the dictionary is empty

15. What is the output of the following piece of code when executed in Python shell? >>> import collections >>> a=dict() >>> a=collections.defaultdict(int) >>> a[1]

Correct Answer is : 0

16. What is the output of the following piece of code when executed in Python shell? >>> import collections >>> a=dict() >>> a=collections.defaultdict(str) >>> a['A']

Correct Answer is : ‘ ‘

17. What is the output of the following piece of code when executed in Python shell? >>> import collections >>> b=dict() >>> b=collections.defaultdict(lambda: 7) >>> b[4]

Correct Answer is : 7

18. What is the output of the following piece of code when executed in Python shell? >>> import collections >>> a=collections.OrderedDict((str(x),x) for x in range(3)) >>> a

Correct Answer is : OrderedDict([(‘0’, 0), (‘1’, 1), (‘2’, 2)])

19. Which of these is not a fundamental features of OOP?

Correct Answer is : Instantiation

20. Which of the following is the most suitable definition for encapsulation?

Correct Answer is : Means of bundling instance variables and methods in order to restrict access to certain class members

Similar Interview Questions

    Search for latest jobs

    Icon
    Icon