Python Multiple Choice Questions and Answers

1. Python is a

a. High-level programming language

b. Low-level scripting language

c. Middle-level programming language

d. High-level scripting language

Answer: (d) High-level scripting language

2. What will be the output of this statement?

    i = 0
    while i < 5:
        print(i)
        i += 1
        if i == 3:
            break
    else:
        print(0)  

a. 1 2 3

b. 3 2 1

c. 0 1 2

d. 0 1 2 3

Answer: (c) 0 1 2

3. What will be the output of the following program?

    i = 1:
    while True:
        if i%3 == 0:
            break
        print(i)

a. 3 2 1

b. 1 2

c. 1 2 3

d. Invalid syntax

Answer: (d) 1 2

Explanation: because, the declaration (i = 1:) is wrong.

4. Which one of the following has the same precedence level?

a. Subtraction and Division

b. Power and Division

c. Division and Multiplication

d. Division, Power, Multiplication, Addition and Subtraction

Answer: (c) Division and Multiplication

5. Which of the following operators is the correct option for power(xy)?

a. x**y

b. x ^ ^ y

c. x ^ * y

d. x ^ y

Answer: (a) x**y

Explanation: To find the power of a number we use power operator a**b, i.e., 2**3=8.

6. Which of the following words cannot be used as a variable name in python?

a. var

b. _try_

c. _var

d. try

Answer: (d) try

Explanation: because “try” is a keyword.

7. Which of the following is not a keyword in Python language?

a. with

b. raise

c.val

d. try

Answer: (c) val

Explanation: because “val” is not a keyword in python.

8. What will be the output of this statement?

 >>>"x"+"yz"

a. xyz

b. x yz

c. x+yz

d. x

Answer: (a) xyz

Explanation: The “+” operator is used to concatenation two strings.

9. Set members must not be hashable.

a. True

b. False

Answer: (b)

Explanation: Set members must always be hashable.

10. What will be the output of the following Python code?

x = 2
for i in range(x):
    x -= 2
    print (x)

a. 0

b. 0 1 2 3 4 …

c. 0 -2

d. error

Answer: (c) 0 -2

Explanation: The loop is entered twice.

11. What is the maximum possible length of an identifier?

a. 79 characters

b. 35 characters

c. 22 characters

d. none of the mentioned

Answer: (d) none of the mentioned

Explanation: Identifiers can be of any length.

12. Which of the following declarations is incorrect?

a. _a = 4

b. __abc__ = 9

c. __a = 1

d. None of these

Answer: (d) None of these

13. What will be the output of this code?

    x = 1
    while True:
        if x % 5 = = 0:
            break
        print(x)
        x + = 1  

a. 2 1

b. 0 3 1

c. error

d. None of these

Answer: (c) error

Explanation: Syntax error, there should not be a space between + and =.

14. All keywords in Python are in

a. UPPER CASE

b. lower case

c. Capitalized

d. None of the mentioned

Answer: (d) None of the mentioned

Explanation: True, False and None are capitalized while the others are in lower case.

15. What will be the output of the following code?

>>> a=("NeuralBeast")*3
>>> a

a. Operator not valid for tuples

b. (‘NeuralBeastNeuralBeastNeuralBeast’)

c. (‘NeuralBeast’,’NeuralBeast’,’NeuralBeast’)

d. Syntax error

Answer: (b) (‘NeuralBeastNeuralBeastNeuralBeast’)

Explanation: Here (“NeuralBeast”) is a string not a tuple because there is no comma after the element.

16. Which of the following cannot be a variable?

a. in

b. it

c. on

d. __init__

Answer: (a) in

Explanation: in is a keyword.

17. Which of the following functions is a built-in function in python language?

a. print()

b. val()

c. num()

d. None of these

Answer: (a) print()

Explanation: The print() function is a built-in function in python that prints a value directly on the screen.

18. Which of the following precedence order is correct in Python?

a. Multiplication, Division, Addition, Subtraction, Parentheses, Exponential

b. Division, Multiplication, Addition, Subtraction, Parentheses, Exponential

c. Parentheses, Exponential, Multiplication, Division, Addition, Subtraction

d. Exponential, Parentheses, Multiplication, Division, Addition, Subtraction

Answer: (c) Parentheses, Exponential, Multiplication, Division, Addition, Subtraction

Explanation: PEMDAS (similar to BODMAS).

19. What will be the output of this function?

round(12.638)

a. 13

b. 12.69

c. 12

d. 127

Answer: (a) 13

Explanation: The round function is a built-in function in the Python language that round-off the value.

20. What will be the output of the following code?

    x = ['xy', 'yz']
    for i in a:
        i.upper()
    print(a)  

a. [xyxy]

b. [‘XY’, ‘YZ’]

c. [‘xy’, ‘yz’]

d. None of these

Answer: (c) [‘xy’, ‘yz’]

21. What will be the output of this statement?

>>> print(0xA + 0xB + 0xC) 

a. 0xA + 0xB + 0xC

b. 63

c. 33

d. None of these

Answer: (c) 33

Explanation: A, B and C are hexadecimal integers with values 10, 11 and 12 respectively, so the sum of A, B and C is 33.

22. What will be the output of this code?

any([5>8, 6>3, 3>1])  

a. True

b. False

c. Invalid code

d. None of these

Answer: (a) True

23. What will be displayed by print(ord(‘b’) – ord(‘a’))?

a. 0

b. -1

c. 1

d. 2

Answer: (c) 1

Explanation: ASCII value of b is one more than a. Hence the output of this code is 98-97, which is equal to 1.

24. Which of the following is not a declaration of the dictionary?

a. dict([[1,”A”],[2,”B”]])

b. {1,”A”,2”B”}

c. {1: ‘A’, 2: ‘B’}

d. { }

Answer: (b) {1,”A”,2”B”}

Explanation: Option c is a set, not a dictionary.

25. Is Python case sensitive when dealing with identifiers?

a. machine dependent

b. yes

c. no

d. none of the mentioned

Answer: (b) yes

26. What will be the output of this function?

all([2,4,0,6]) 

a. 0

b. True

c. False

d. Invalid code

Answer: (c) False

Explanation: If any element is zero, it returns a false value, and if all elements are non-zero, it returns a true value. Hence, the output of this “all([2,4,0,6])” function will be false.

27. What will be the output of the following code?

>>>chr(ord('A'))

a. a

b. A

c. B

d. Error

Answer: (b) A

28. What is the syntax of the following code for creating a frozenset?

>>> a=frozenset(set([5,6,7]))
>>> a

a. frozenset({5,6,7})

b. {5,6,7}

c. Error, not possible to convert set into frozenset

d. Syntax error

Answer: (a) frozenset({5,6,7})

Explanation: frozenset({5,6,7}) code is the correct syntax for creating a frozenset.

29. The format function, when it applied on a string returns

a. int

b. bool

c. str

d. Error

Answer: (c) str
Explanation: Format function will returns a string.

30. Which of the following is correctly evaluated for the following function?

pow(x,y,z)

a. (x / y) * z

b. (x**y) / z

c. (x / y) / z

d. (x**y) % z

Answer: (d) (x**y) % z

31. What will be the output of the following code?

x = ['ab', 'cd']
for i in x:
    x.append(i.upper())
print(x)

a. [‘ab’, ‘cd’]

b. [‘ab’, ‘cd’, ‘AB’, ‘CD’]

c. [‘AB’, ‘CD’]

d. none of the mentioned

Answer: (d) none of the mentioned

Explanation: The loop does not terminate as new elements are being added to the list in each iteration.

32. What will be the output of the following code?

print("D", end = ' ')
print("Z", end = ' ')
print("Y", end = ' ')
print("X", end = ' ')

a. XYZA

b. AZYX

c. A Z Y X

d. A, Z, Y, X will be displayed on four lines

Answer: (c) A Z Y X

33. Which one of the following has the same precedence level?

a. Multiplication, Division and Addition

b. Multiplication, Division, Addition and Subtraction

c. Addition and Multiplication

d. Addition and Subtraction

Answer: (d) Addition and Subtraction

Explanation: “Addition and Subtraction” are at the same precedence level.

34. What will be the output of this statement?

    d = {0, 1, 2}
    for x in d:
        print(x) 

a. 0 1 2

b. {0, 1, 2} {0, 1, 2} {0, 1, 2}

c. Syntax_Error

d. None of these above

Answer: (a) 0 1 2

35. What will be the output of this code?

    import math
    abs(math.sqrt(36))  

a. 6

b. -6

c. 6.0

d. Error

Answer: (c) 6.0

Explanation: This sqrt() function will prints the square of the value.

36. What will be the output of the following code?

x = 2
for i in range(x):
    x += 1
    print (x)

a. 3 4

b. 0 1

c. 0 1 2 3 4 …

d. 0 1 2 3

Answer: (a) 3 4

Explanation: Variable x is incremented and printed twice.

37. Which of the following commands will create a list?

a. mylist = list([1, 2, 3])

b. mylist = []

c. mylist = list()

d. all of the mentioned

Answer: (d) all of the mentioned

38. Is the following Python code valid or not?

a={1,2,3}
b={1,2,3,4}
c=a.issuperset(b)
print(c)

a. True

b. False

c. Syntax error for issuperset() method

d. Error, no method called issuperset() exists

Answer: (b) False

Explanation: The method issubset() returns True if b is a proper subset of a.

39. What will be the output of the following code?

i = 5
while True:
    if i%0O11 == 0:
        break
    print(i)
    i += 1

a. 5 6

b. 5 6 7 8 9 10

c. 5 6 7 8

d. error

Answer: (c) 5 6 7 8

Explanation: here, 0O11 is an octal number.

40. What will be the output of this statement?

str1 = "neural"
str2 = ":"
str3 = "beast"
print(str1[-1:])

a. n

b. l

c. neural

d. beast

Answer: (b) l

Explanation: The output of this program is “l” because -1 corresponds to the last index.

41. Which of the following is true for variable names in Python?

a. underscore and ampersand are the only two special characters allowed

b. all private members must have leading and trailing underscores

c. unlimited length

d. none of the mentioned

Answer: (c) unlimited length

Explanation: Variable names can be of any length.

42. Which of the following is the correct output of this program?

    x = 'abcd'
    for i in range(len(x)):
        x[i].upper()
    print (x) 

a. bcd

b. ABCD

c. abcd

d. None of these

Answer: (c) abcd

43. What will be the output of the following code?

>>> a=(2,3,4)
>>> sum(a,3)

a. The method sum() doesn’t exist for tuples

b. Too many arguments for sum() method

c. 9

d. 12

Answer: (d) 12

Explanation: In the above case, 3 is the starting value to which the sum of the tuple is added to.

44. Lambda is a statement.

a. True

b. False

Answer: (b) False

Explanation: lambda is an anonymous function in Python.

45. Which of the following functions accepts only integers as arguments?

a. min()

b. ord()

c. chr()

d. any()

Answer: (c) chr()

Explanation: The function chr() accepts only integers as arguments. The functions min() and max() can accept floating point as well as integer arguments. The function ord() accepts only strings.

46. What is the method inside the class in python language?

a. Function

b. Object

c. Argument

d. Attribute

Answer: (a) Function

Explanation: In python function is also known as the method.

47. What will be the output of the following Python code?

>>> a={1,2,3}
>>> {x*2 for x in a|{4,5}}

a. {8,10}

b. {8, 2, 10, 4, 6}

c. {2,4,6}

d. Error, set comprehensions aren’t allowed

Answer: (b) {8, 2, 10, 4, 6}

Explanation: Set comprehensions are allowed.

48. What will be the output of the following code?

>>> b={1:"A",2:"B",3:"C"}
>>> b.items()

a. dict_items([(1,2,3)])

b. dict_items([(‘A’), (‘B’), (‘C’)])

c. dict_items([(1, ‘A’), (2, ‘B’), (3, ‘C’)])

d. Syntax error

Answer: (c) dict_items([(1, ‘A’), (2, ‘B’), (3, ‘C’)])

Explanation: The method items() returns list of tuples with each tuple having a key-value pair.

49. What will be the output of this statement?

    z = "xyz"
    j = "j"
    while j in z:
        print(j, end=" ") 

a. x y z

b. xyz

c. j j j j j j j..

d. No output

Answer: (d) No output

Explanation: “j” is not in “xyz”.

50. What will be the output of the following code?

for i in [1, 2, 3, 4][::-1]:
    print (i)

a. 4 3 2 1

b. 1 2 3 4

c. error

d. none of the mentioned

Answer: (a) 4 3 2 1

More Reading

Post navigation

11 Comments

Leave a Reply

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