python mcq interview questions




1. How many CPUs will the threading library use?

A. Zero. threading does not make use of CPUs.

B. One

C. Two

D. All available CPUs

2.def REVERSE(L):

    L.reverse()

    return (L)

def YKNJS(L)

    list = []

    list.extend(Rev(L))

    print(list)


L = [1,3.1,5,31,7.531]

YKNJS(L)

A.[1,4.1,2.31,7.531]

B.[1,3.1,5.31,7.531]

C.[7.531,5.31,3.1,1]

D. None of these

3.Which of the following codes raises an exception?

 A. #test

 B. a={1, 2, \ #comment 3, 4}

 C. a={1, 2, #comment 3, 4}

4.Which collection type is used to associate values with unique keys?

A.slot 

B.dictionary 

3.queue

4.sorted list 

5.What will this code print?

>>> a=7

>>>     print(a+1)

 A. 8

 B. a + 1

 C. This code will raise an exception

 D. None of the above

6.What is key difference between a set and a list ?

A. A set is an ordered collection unique items.A list is an unordered collectio of non-unique items 

B. Elements can be retrived from a list but they cannot be retrived from a set.

C. A set is an ordered collection of non-unique items.A list is an unordered collection of unique items.

D.A set is an unordered collection unique items.A list is an ordered collection of non-unique items.


7.What is the maximum value for an integer in Python 3?

A. 2^63-1

B. 32767

C. 65536

D. No limit

8.How do you denote the end of a block in Python 3?

 A. By indenting the next line less than the current

 B. With a semicolon }

 C. With the end keyword

 D. With a backslash \

9.def addToList(listcontainer):

     listcontainer+=[10]

mylistContainer = [10,20,30,40]

addToList(mylistContainer)

print(len(mylistContainer))

A.4

B.5

C.6

D.Compiler Error 

10.According to the PEP 8 coding style guidelines,how should constant values be named in Python?

A.in camel case without using underscores to separate words --eg.maxValue = 255

B. in lowercase with underscores to separate words --e.g.max_value=255

C.in all caps with underscores separating words --eg. MAX_VALUE=255

D.in mixed case without using underscores to seperate words --eg.MaxValue=255

11.In this list, how will you access the letter ‘t’ in ‘bat’?

list = [1, [‘a’, ‘b’, [‘kill’, ‘bat’, ‘cup’], ‘c’], 3]

A. list[1, 2, 1, 2]

B. list[2][1][2]

C. list[1][2][1][2]

D. list[1][2][1]

12.How will you turn the list a = [1, 2, 7, 8] into [1, 2, 3, 4, 5, 6, 7, 8]?

Please select 2 correct answers

A. a[2:2] = [3, 4, 5, 6]

B. a[2:3] = [3, 4, 5, 6, 7]

C. a[2:2] = [3, 4, 5, 6, 7]

D. a[2:3] = [3, 4, 5, 6]

13.What is the value of y?

>>> x, y, z = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)[2::3]

A. 2

B. 5

C. 6

D. This code raises an exception

14.What is the result of {1, 2, 3} & {4, 5, 6}?

 A. {}

 B. set()

 C. {1, 2, 3, 4, 5, 6}

 D. None of the above

15.what is the correct syntax for creating a variable that is bound to a set?

A.myset = {0,'apple',3.5}

B.myset = to_set(0,'apple',3.5)

C.myset = (0,'apple',3.5).to_set()

D.myset = (0,'apple',3.5).set()


16.What statement about static methods is  true?

A. Static methods are called static because they always return None.

B. Static methods can be bound to either a class or an instance of a class.

C.static methods server mostly as utility methods or helper methods,since they can't access or modify a class's state.

D.Static methods can access and modify the state of a class or an instance of a class.


17.Which statements delete the value ‘milk’ from the set s = {‘milk’, ‘cheese’, ‘butter’, ‘buttermilk’}?

Please select 4 correct answers

 A. s.discard(‘milk’)

 B. del s[‘milk’]

 C. s -= ‘milk’

 D. s -= {‘milk’}

 E. s &= {‘cheese’, ‘butter’, ‘buttermilk’}

 F. s.difference_update({‘milk’})

 G. s.pop()

18.In the following code, how many objects and references does Python create?

name = ‘Ayushi’

fname = name

 A. One object, one reference

 B. One object, two references

 C. Two objects, one reference

 D. Two objects, two references

19.According to PEP 8, what case should you use for variable names with more than one word?

A. Camel case

B. Pascal case

C. Snake case

D. Kebab case

E. Upper case with snake case

20.x = ['ab','cd']

for i in x:

  i.upper()

print(x)

A. Error 

B. ['cd','ab']

C. ['ab','cd']

D. None of these


21.Which of these is NOT a characteristics of namedtuples?

A. You can assign a name to each of the namedtuple members and refer to them that way,similarly to how you would access keys in dictionary.

B.Each member of a namedtuple object can be indexed to directly,just like in a regular tuple

C.namedtuples are just as memory efficient as regular tuples 

D.No import is needed to use namedtuples because they are available in the standard library.


22.What is the output of the following code?

>>> nums = [[val for val in range(num)] for num in range(3)]

>>> for num in nums:

            for val in num:

                if val < 2:

                    print(‘*’, end=”)


 A. *

 B. **

 C. ***

 D. ****

23.What is the output of this code?

a,b=1,0

a=a^b

b=a^b

a=a^b

print(a)

 A. 0

B. 1

C. 2

D. This code will raise a runtime error

24.from math import * 

a = 2.13 

b = 3.7777

c = -3.12 

print(int(a),floor(b),ceil(c),fabs(c))

A. 24-3 3

B. 2 3-3 3.12

C. 2 2-3 3.12

D.Error 

25.What is the value of this expression?

2**2**3**1

 A. 12

 B. 64

 C. 128

 D. 256

26.Why would you use a virtual environment?

A. Virtual environment create a "bubble" around your project so that any libraries or packages you install within it don't affect your entire machine.

B.Teams with remote employees use virtual environments  so they can share code,do code reviews,and collaorate remotely.

C. Virtual environments were common in Python 2 because they augmented missing features in the language.Virtual environments are not necessary in Python 3 due to advancement in the language.

D.Virtual environments are tied to your Github or Bitbucket account,allowing you to access of your repos virtually from any machine 

27.What is the  output of the following Python Programs 

D = dict()

for x in enumerate(range(2)):

  D[x[0]] = x[1]

  D[x[1]+7]=x[0]

print(D)

A. {1:1,7:2,0:1,8:1}

B. KeyError 

C. {0:0,7:0,1:1,8:1}

D. {0:1,7:0,1:1,8:0}

28.How will you open a file for reading as a text file?

Please select 2 correct answers

 A. open('file.txt')

 B. open('file.txt', 'r’)

 C. open('file.txt', w’)

 D. open('file.txt', 'b’)

29.Suppose you have two sets s1 = {1, 2, 3} and s2 = {3, 4, 5}. Which of the following statements give us their union?

Please select 5 correct answers

 s1.union({3, 4, 5})

 s1 | {3, 4, 5}

 s1.union([3, 4, 5])

 s1 | [3, 4, 5]

 s1.union(set([3, 4, 5]))

 s1 | set([3, 4, 5])

30.Polymorphism is when a subclass can modify the behavior of its superclass.

A. False

B. True

C. Depends on the contents of the class

D. This is an invalid question

31.data = [x for x in range(5)]
     temp = [x for x in range(7) if x in data and x%2==0]
     print(temp)

A. [0,2,4,6]
B. [0,1,2,3,4,5]
C. [0,2,4]
D. Compiler Error 

32.What will be the output of the following code?
>>> scores = {‘Ayushi’: 97, ‘Megha’: 98}
>>> if scores[‘Ayushi’]>97:
        print(“Scholar”)
elif scores[‘Megha’]>97:
        print(“Topper”)
elif scores[‘Melanie’]>97:
        print(“Teacher”)
else: print(“Nobody topped”)

 A. Scholar
 B. Topper
 C. Teacher
 D. Nobody topped
 E. This code raises a KeyError

33.What is the output of the following code?
>>> list = [‘a’, ‘b’, ‘c’]
>>> list += ‘de’
>>> print(list)

 A. [‘a’, ‘b’, ‘c’, ‘d’, ‘e’]
 B. [‘a’, ‘b’, ‘c’, ‘de’]
 C. [‘ade’, ‘bde’, ‘cde’]
 D. This raises an exception because we cannot add a string to a list

34.What is the output of the following code?
>>> val = 154
>>> while(not(val)):
        val**=2
else:
        val//=2

>>> print(val)

 A. 77
 B. 154
 C. 11858
 D. 23716
 E. This code will raise an exception

35.What is the output of the following Python Programs?

L = [1,3,5,7,9]
print(L.pop(-3),end = '  ')
print(L.remove(L[0]),end = '  ')
print(L)

A. 5 1[3,7,9]
B. 5 None[3,7,9]
C. 5 1[3,7,9]
D. Compiler Error

36.List = [True,50,10]
     List.insert(2,5)
     print(List,"Sum is:",sum(List))

A. [True,50,10,5]Sum is:66
B. TypeError:unsupported operand type(s) for +:'int' and 'str'
C. [True,50,5,10]Sum is:65
D. [True,50,5,10] Sum is:66


37.What is the output of this code?
def func(val1, val2=2, val3=7, val4=1):
    return val1**val2**val3

print(func(val2=2, val1=2, val3=4))

 A. 256
 B. 32768
 C. 65536
 D. This code raises an exception

38.In the following code, how will you get to the value 7?

list = [

    ‘milk’,

    ‘cheese’,

    {

        ‘block’: 1,

        ‘utensils’:

        {

            ‘cups’ : 5,

            ‘spoons’ : 7

        },

        ‘color’: ‘yellow’

    },

    ‘butter’,

    ‘ghee’

]

 A. list[‘cheese’][‘spoons’]
 B. list[2][‘utensils’][‘spoons’]
 C. list[‘spoons’]
 D. list[‘utensils’][‘spoons’]

39.What does the following code print?
a = [1, 2, 3]
a is a[:]

 A. True
 B. False
 C. This code raises an exception

40.In the following code, what is the output?
a = 2
b = 3
a and b

 A. 3
 B.True
 C. False
 D. 1

41.def test():
        "I love Articleworld.com"
         return 1
print(test.__doc__[17:21])

A. d.co
B. d.com
C. Compiler Error 
D. Aticleworld

42.What value would be returned by this check for equality ?

5!=6 

A. Yes 
B. False 
C. True 
D. None 

43.Which of the following return True for a string?
Please select 3 correct answers

A. s[:] == s
B. s[:] is s
C. s[::-1][::-1] == s
D. s[::-1][::-1] is s

44.What is the value of round(12.5) – round(11.5)?

 A. 0
 B. 1
 C. 2
 D. 3
 E. This code will raise an exception

45.Select the correct statements:
I. We define classes and functions with the def keyword
II. A class can define an object
III. A class can define a function
IV. A function can define a class

 A. II and III
 B. III and IV
 C. II, III and IV
 D. All of them

46.Select the correct output for this code:
try:
    raise Exception
    print(2/0)
except Exception as e:
    print(e)

 A. Another exception is raised in the except block
 B. Exception
 C. This code prints a blank line

47.What is the size of an empty tuple in Python?

 A. 0 bytes
 B. 8 bytes
 C. 32 bytes
 D. 48 bytes

48.Assuming the node is in a singly linked list,what is the runtime complexity of searching for a specific node within a singly linked list?

A. The runtime is O(n) because in the worst case,the node you are searching for is the last node,and every node in the linked list must be visited.
B. The runtime is O(nk),with n representing the number of nodes and k representing the amount of time it takes to access each node in memory.
C. The runtime cannot be determined unless you know how many nodes are in the singly linked list.
D. The runtime is O(1) because you can index directly to a node in a singly linked list.

49.What is the runtime complexity of searching for an item in a binary search tree?

A. The runtime for searching in a binary search tree is O(1) because each node acts as a key,similar to a dictionary.
B. The runtime for searching in a binary search tree  is O(n!) because every node must be compared to every other node.
C. The runtime for searching in a binary search tree is generally O(h),where h is the height of the tree.
D. The runtime for searching in a binary search tree is O(n) because every node in the tree must be visited.

50.What is the value of this expression?
2**2**3**1

 A. 12
 B. 64
 C. 128
 D. 256
 E. This code will raise an exception

Question NoAnswer
1B
2C
3B
4B
5C
6D
7D
8A
9B
10C
11C
12A,B
13C
14B
15A
16C
17A,C,D,E
18B
19C
20C
21D
22C
23A
24B
25C
26A
27C
28A,B
29A,C,D,E
30B
31C
32B
33A
34A
35B
36D
37C
38B
39B
40A
41A
42C
43A,B,C
44A
45C
46C
47D
48A
49C
50D

Post a Comment

Previous Post Next Post