Python objective interview questions and answers | Python MCQ (Multiple Choice Questions)

Python MCQ (Multiple Choice Questions)

Python objective interview questions and answers

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

a) underscore and ampersand are the only two special characters allowed
b) unlimited length
c) all private members must have leading and trailing underscores
d) none of the mentioned

Answer is (B) Explanation: Variable names can be of any length

2. What are the values of the following Python expressions?

 2**(3**2)
 (2**3)**2
 2**3**2

a) 512, 64, 512
b) 512, 512, 512
c) 64, 512, 64
d) 64, 64, 64

Answer is (A) Explanation: Expression 1 is evaluated as: 2**9, which is equal to 512. Expression 2 is evaluated as 8**2, which is equal to 64. The last expression is evaluated as 2**(3**2). This is because the associativity of ** operator is from right to left. Hence the result of the third expression is 512.

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

a) factorial()
b) print()
c) seed()
d) sqrt()

Answer is (B) Explanation: The function seed is a function which is present in the random module. The functions sqrt and factorial are a part of the math module. The print function is a built-in function which prints a value directly to the system output.

4. The following Python program can work with ____ parameters.

def f(x):
    def f1(*args, **kwargs):
           print("Sanfoundry")
           return x(*args, **kwargs)
    return f1

a) any number of
b) 0
c) 1
d) 2

Answer is (A) Explanation: The code shown above shows a general decorator which can work with any number of arguments.

5. Which of the following is not a core data type in Python programming?

a) Tuples
b) Lists
c) Class
d) Dictionary

Answer is (C) Explanation: Class is a user-defined data type.

6. Which of these is the definition for packages in Python?

a) A set of main modules
b) A folder of python modules
c) A number of files containing Python definitions and statements
d) A set of programs making use of Python modules

Answer is (B) Explanation: A folder of python programs is called as a package of modules.

7. What will be the output of the following Python function?

len(["hello",2, 4, 6])

a) Error
b) 6
c) 4
d) 3

Answer is (C) Explanation: The function len() returns the length of the number of elements in the iterable. Therefore the output of the function shown above is 4.

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

x = 'abcd'
for i in x:
    print(i.upper())

a) a B C D
b) a b c d
c) error
d) A B C D

Answer is (D) Explanation: The instance of the string returned by upper() is being printed.

9. What is the order of namespaces in which Python looks for an identifier?

a) Python first searches the built-in namespace, then the global namespace, and finally the local namespace
b) Python first searches the built-in namespace, then the local namespace, and finally the global namespace
c) Python first searches the local namespace, then the global namespace and finally the built-in namespace
d) Python first searches the global namespace, then the local namespace, and finally the built-in namespace

Answer is (C) Explanation: Python first searches for the local, then the global and finally the built-in namespace.

10. Which function is called when the following Python program is executed?

f = foo()
format(f)

a) str()
b) format()
c) __str__()
d) __format__()

Answer is (C) Explanation:Both str(f) and format(f) call f.__str__().

11. What will be the output of the following Python program?

def foo(x):
    x[0] = ['def']
    x[1] = ['abc']
    return id(x)
q = ['abc', 'def']
print(id(q) == foo(q))

a) Error
b) None
c) False
d) True

Answer is (D) Explanation: The same object is modified in the function.

12. Which module in the Python standard library parses options received from the command line?

a) getarg
b) getopt
c) main
d) os

Answer is (B) Explanation: getopt parses options received from the command line.

13. Who developed Python Programming Language?

a) Wick van Rossum
b) Rasmus Lerdorf
c) Guido van Rossum
d) Niene Stom

Answer is (C) Explanation: Python language is designed by a Dutch programmer Guido van Rossum in the Netherlands.

14. What will be the output of the following Python program?

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

a) error
b) 0 1 2 0
c) 0 1 2
d) none of the mentioned

Answer is (C) Explanation: The else part is not executed if control breaks out of the loop.

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

count = 0
while(True):
   if count % 3 == 0:
       print(count, end = " ")
   if(count > 15):
       break;
   count += 1

a)0 1 2 …. 15

b)infinite Loop

c)0 3 6 9 12 15

d)0 3 6 9 12

Answer is (C) Explanation: 0 3 6 12 15

16. What will be the output of the following code snippet?

numbers = (4, 7, 19, 2, 89, 45, 72, 22)
sorted_numbers = sorted(numbers)
even = lambda a: a % 2 == 0
even_numbers = filter(even, sorted_numbers)
print(type(even_numbers))

a) filter

b) int

c) list

d) tuple

Answer is (A) Explanation: Filter

17. What will be the output of the following code snippet?

s = {1, 2, 3, 3, 2, 4, 5, 5}
print(s)

a) {1, 2, 3, 3 , 4 ,5, 5}

b) {1, 2, 3, 4, 5}

c) None

d) {1, 5}

Answer is (B) Explanation: {1, 2, 3, 4, 5}

18. What will be the output of the following code snippet?

from math import *
a = 2.19
b = 3.999999
c = -3.30
print(int(a), floor(b), ceil(c), fabs(c))

a) 2 3 -3 3.3

b) 3 4 -3 3

c)2 3 -3 3

d) 2 3 -3 -3.3

Answer is (A) Explanation: 2 3 -3 3.3

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

a) pass
b) eval
c) assert
d) nonlocal

Answer is (B) Explanation: Aval

20. Which of the following is a feature of Python DocString?

a) In Python all functions should have a docstring
b) Docstrings can be accessed by the __doc__ attribute on objects
c) It provides a convenient way of associating documentation with Python modules, functions, classes, and methods
d) All of the mentioned

Answer is (D) Explanation: Python has a nifty feature called documentation strings, usually referred to by its shorter name docstrings. DocStrings are an important tool that you should make use of since it helps to document the program better and makes it easier to understand.

Previous articleWhat is the difference between C and C++? |c interview questions and answers for freshers
Next articlePython interview questions | Python interview questions and answers