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
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
3. Which of the following functions is a built-in function in python?
a) factorial()
b) print()
c) seed()
d) sqrt()
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
5. Which of the following is not a core data type in Python programming?
a) Tuples
b) Lists
c) Class
d) Dictionary
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
7. What will be the output of the following Python function?
len(["hello",2, 4, 6])
a) Error
b) 6
c) 4
d) 3
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
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
10. Which function is called when the following Python program is executed?
f = foo()
format(f)
a) str()
b) format()
c) __str__()
d) __format__()
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
12. Which module in the Python standard library parses options received from the command line?
a) getarg
b) getopt
c) main
d) os
13. Who developed Python Programming Language?
a) Wick van Rossum
b) Rasmus Lerdorf
c) Guido van Rossum
d) Niene Stom
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
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
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
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}
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
19. Which one of the following is not a keyword in Python language?
a) pass
b) eval
c) assert
d) nonlocal
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