1000 Python MCQ (Multiple Choice Questions) - Sanfoundry
1000 Python MCQ (Multiple Choice Questions) - Sanfoundry
b) Rasmus Lerdorf
View Answer
Answer: c
Explanation: Python language is designed by a Dutch programmer Guido van Rossum in the
Netherlands.
advertisement
View Answer
Answer: d
Python is an interpreted programming language, which supports object-oriented,
Explanation:
structured, and functional programming.
https://www.sanfoundry.com/1000-python-questions-answers/ 1/21
12/30/21, 11:12 AM 1000 Python MCQ (Multiple Choice Questions) - Sanfoundry
b) yes
c) machine dependent
d) none of the mentioned
View Answer
Answer: a
Case is always significant.
Explanation:
b) .pl
c) .py
d) .p
View Answer
Answer: c
Explanation: ‘.py’ is the correct extension of the Python file. Python programs can be written in any
text editor. To save these programs we need to save in files with file extension ‘.py’.
View Answer
Answer: b
Many languages have been implemented using both compilers and interpreters,
Explanation:
including C, Pascal, and Python.
advertisement
https://www.sanfoundry.com/1000-python-questions-answers/ 2/21
12/30/21, 11:12 AM 1000 Python MCQ (Multiple Choice Questions) - Sanfoundry
b) lower case
c) UPPER CASE
d) None of the
mentioned
View Answer
Answer: d
Explanation: True, False and None are capitalized while the others are in lower case.
4 + 3 % 5
a) 7
b) 2
c) 4
d) 1
View Answer
Answer: a
Explanation: The order of precedence is: %, +. Hence the expression above, on simplification results
in 4 + 3 = 7. Hence the result is 7.
advertisement
https://www.sanfoundry.com/1000-python-questions-answers/ 3/21
12/30/21, 11:12 AM 1000 Python MCQ (Multiple Choice Questions) - Sanfoundry
View Answer
Answer: a
In Python, to define a block of code we use indentation. Indentation refers to
Explanation:
whitespaces at the beginning of the line.
b) Def
c) Fun
d) Define
View Answer
Answer: b
Explanation: None.
10. Which of the following character is used to give single-line comments in Python?
a) //
b) #
c) !
d) /*
View Answer
Answer: c
To write single-line comments in Python use the Numero sign (#) at the beginning of the
Explanation:
line. To write multi-line comments, close the text between triple quotes.
Example: “”” comment
text “””
i = 1
while True:
if i%3 == 0:
break
print(i)
i + = 1
https://www.sanfoundry.com/1000-python-questions-answers/ 4/21
12/30/21, 11:12 AM 1000 Python MCQ (Multiple Choice Questions) - Sanfoundry
a) 1 2 3
b) error
c) 1 2
d) none of the mentioned
View Answer
Answer: b
SyntaxError, there shouldn’t be a space between + and = in +=.
Explanation:
advertisement
12. Which of the following functions can help us to find the version of python that we are currently
working on?
a) sys.version(1)
b) sys.version(0)
c) sys.version()
d) sys.version
View Answer
Answer: a
Explanation: The function sys.version can help us to find the version of python that we are currently
working on. For example, 3.5.2, 2.7.3 etc. this function also returns the current date, time, bits etc
along with the version.
13. Python supports the creation of anonymous functions at runtime, using a construct called __________
a) pi
b) anonymous
c) lambda
https://www.sanfoundry.com/1000-python-questions-answers/ 5/21
12/30/21, 11:12 AM 1000 Python MCQ (Multiple Choice Questions) - Sanfoundry
View Answer
Answer: c
Python supports the creation of anonymous functions (i.e. functions that are not bound
Explanation:
to a name) at runtime, using a construct called lambda. Lambda functions are restricted to a single
expression. They can be used wherever normal functions can be used.
View Answer
Answer: d
Explanation: For order of precedence, just remember this PEMDAS (similar to BODMAS).
15. What will be the output of the following Python code snippet if x=1?
advertisement
x<<2
a) 4
b) 2
c) 1
https://www.sanfoundry.com/1000-python-questions-answers/ 6/21
12/30/21, 11:12 AM 1000 Python MCQ (Multiple Choice Questions) - Sanfoundry
d) 8
View Answer
Answer: a
The binary form of 1 is 0001. The expression x<<2 implies we are performing bitwise left
Explanation:
shift on x. This shift yields the value: 0100, which is the binary form of the number 4.
View Answer
Answer: c
Explanation: Variable names can be of any length.
View Answer
Answer: b
Variable names can be of any length.
Explanation:
2**(3**2)
(2**3)**2
2**3**2
View Answer
Answer: a
Expression 1 is evaluated as: 2**9, which is equal to 512. Expression 2 is evaluated as
Explanation:
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.
https://www.sanfoundry.com/1000-python-questions-answers/ 7/21
12/30/21, 11:12 AM 1000 Python MCQ (Multiple Choice Questions) - Sanfoundry
View Answer
Answer: b
// is the operator for truncation division. It is called so because it returns only the integer
Explanation:
part of the quotient, truncating the decimal part. For example: 20//3 = 6.
list(filter(bool, l))
View Answer
Answer: c
The code shown above returns a new list containing only those elements of the list l
Explanation:
which do not amount to zero. Hence the output is: [1, 2, ‘hello’].
b) print()
c) seed()
d) sqrt()
View Answer
Answer: 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.
View Answer
https://www.sanfoundry.com/1000-python-questions-answers/ 8/21
12/30/21, 11:12 AM 1000 Python MCQ (Multiple Choice Questions) - Sanfoundry
Answer: b
Explanation: Each object in Python has a unique id. The id() function returns the object’s id.
23. The following python program can work with ____ parameters.
def f(x):
print("Sanfoundry")
return f1
a) any number of
b) 0
c) 1
d) 2
View Answer
Answer: a
Explanation: The code shown above shows a general decorator which can work with any number of
arguments.
min(max(False,-3,-4), 2,7)
a) -4
b) -3
c) 2
d) False
View Answer
Answer: d
Explanation: The function max() is being used to find the maximum value from among -3, -4 and
false. Since false amounts to the value zero, hence we are left with min(0, 2, 7) Hence the output is 0
(false).
25. Which of the following is not a core data type in Python programming?
a) Tuples
b) Lists
c) Class
d) Dictionary
View Answer
https://www.sanfoundry.com/1000-python-questions-answers/ 9/21
12/30/21, 11:12 AM 1000 Python MCQ (Multiple Choice Questions) - Sanfoundry
Answer: c
Explanation: Class is a user-defined data type.
26. What will be the output of the following Python expression if x=56.236?
print("%.2f"%x)
a) 56.236
b) 56.23
c) 56.0000
d) 56.24
View Answer
Answer: d
Explanation: The expression shown above rounds off the given number to the number of decimal
places specified. Since the expression given specifies rounding off to two decimal places, the output
of this expression will be 56.24. Had the value been x=56.234 (last digit being any number less than
5), the output would have been 56.23.
View Answer
Answer: b
A folder of python programs is called as a package of modules.
Explanation:
len(["hello",2, 4, 6])
a) Error
b) 6
c) 4
d) 3
View Answer
Answer: c
The function len() returns the length of the number of elements in the iterable.
Explanation:
Therefore the output of the function shown above is 4.
https://www.sanfoundry.com/1000-python-questions-answers/ 10/21
12/30/21, 11:12 AM 1000 Python MCQ (Multiple Choice Questions) - Sanfoundry
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
View Answer
Answer: d
Explanation: The instance of the string returned by upper() is being printed.
30. 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
View Answer
Answer: c
Python first searches for the local, then the global and finally the built-in namespace.
Explanation:
31. What will be the output of the following Python code snippet?
print (i)
a) 4 3 2 1
b) error
c) 1 2 3 4
d) none of the mentioned
View Answer
Answer: a
[::-1] reverses the list.
Explanation:
1. >>>"a"+"bc"
a) bc
b) abc
c) a
d) bca
View Answer
Answer: b
+ operator is concatenation operator.
Explanation:
33. Which function is called when the following Python program is executed?
f = foo()
format(f)
a) str()
b) format()
c) __str__()
d) __format__()
View Answer
Answer: c
Both str(f) and format(f) call f.__str__().
Explanation:
b) eval
c) assert
d) nonlocal
View Answer
Answer: b
Explanation: eval can be used as a variable.
1. class tester:
2. def __init__(self, id):
3. self.id = str(id)
4. id="224"
5.
6. >>>temp = tester(12)
7. >>>print(temp.id)
https://www.sanfoundry.com/1000-python-questions-answers/ 12/21
12/30/21, 11:12 AM 1000 Python MCQ (Multiple Choice Questions) - Sanfoundry
a) 12
b) 224
c) None
d) Error
View Answer
Answer: a
Explanation: Id in this case will be the attribute of the class.
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
View Answer
Answer: d
Explanation: The same object is modified in the function.
37. Which module in the python standard library parses options received from the command line?
a) getarg
b) getopt
c) main
d) os
View Answer
Answer: b
getopt parses options received from the command line.
Explanation:
z=set('abc')
z.add('san')
z.update(set(['p', 'q']))
https://www.sanfoundry.com/1000-python-questions-answers/ 13/21
12/30/21, 11:12 AM 1000 Python MCQ (Multiple Choice Questions) - Sanfoundry
View Answer
Answer: c
The code shown first adds the element ‘san’ to the set z. The set z is then updated and
Explanation:
two more elements, namely, ‘p’ and ‘q’ are added to it. Hence the output is: {‘a’, ‘b’, ‘c’, ‘p’, ‘q’, ‘san’}
b) –
c) +
of the mentioned
d) All
View Answer
Answer: b
Explanation: + is used to concatenate and * is used to multiply strings.
print("abc. DEF".capitalize())
a) Abc. def
b) abc. def
c) Abc. Def
d) ABC. DEF
View Answer
Answer: a
Explanation: The first letter of the string is converted to uppercase and the others are converted to
lowercase.
41. Which of the following statements is used to create an empty set in Python?
a) ( )
b) [ ]
c) { }
d) set()
View Answer
Answer: d
{ } creates a dictionary not a set. Only set() creates an empty set.
Explanation:
https://www.sanfoundry.com/1000-python-questions-answers/ 14/21
12/30/21, 11:12 AM 1000 Python MCQ (Multiple Choice Questions) - Sanfoundry
list1 = [1,2,3,4]
list2 = [2,4,5,6]
list3 = [2,6,7,8]
result = list()
a) [1, 3, 5, 7, 8]
b) [1, 7, 8]
c) [1, 2, 4, 7, 8]
d) error
View Answer
Answer: a
Explanation: Here, ‘result’ is a list which is extending three times. When first time ‘extend’ function is
called for ‘result’, the inner code generates a generator object, which is further used in ‘extend’
function. This generator object contains the values which are in ‘list1’ only (not in ‘list2’ and ‘list3’).
Same is happening in second and third call of ‘extend’ function in these generator object contains
values only in ‘list2’ and ‘list3’ respectively.
So, ‘result’ variable will contain elements which are only in one list (not more than 1 list).
View Answer
Answer: c
We use the function append to add an element to the list.
Explanation:
a) * abcde *
b) *abcde *
c) * abcde*
d) * abcde *
View Answer
https://www.sanfoundry.com/1000-python-questions-answers/ 15/21
12/30/21, 11:12 AM 1000 Python MCQ (Multiple Choice Questions) - Sanfoundry
Answer: b
Explanation: Padding is done towards the right-hand-side first when the final string is of even length.
1. >>>list1 = [1, 3]
2. >>>list2 = list1
3. >>>list1[0] = 4
4. >>>print(list2)
a) [1, 4]
b) [1, 3,
4]
c) [4, 3]
d) [1, 3]
View Answer
Answer: c
Explanation: Lists should be copied by executing [:] operation.
View Answer
Answer: c
Functions are reusable pieces of programs. They allow you to give a name to a block of
Explanation:
statements, allowing you to run that block using the specified name anywhere in your program and
any number of times.
47. Which of the following Python statements will result in the output: 6?
A = [[1, 2, 3],
[4, 5, 6],
[7, 8, 9]]
a) A[2][1]
b) A[1][2]
c) A[3][2]
d) A[2][3]
View Answer
https://www.sanfoundry.com/1000-python-questions-answers/ 16/21
12/30/21, 11:12 AM 1000 Python MCQ (Multiple Choice Questions) - Sanfoundry
Answer: b
Explanation: The output that is required is 6, that is, row 2, item 3. This position is represented by the
statement: A[1][2].
View Answer
Answer: d
Identifiers can be of any length.
Explanation:
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
View Answer
Answer: c
The else part is not executed if control breaks out of the loop.
Explanation:
x = 'abcd'
for i in range(len(x)):
print(i)
a) error
b) 1 2 3 4
c) a b c d
https://www.sanfoundry.com/1000-python-questions-answers/ 17/21
12/30/21, 11:12 AM 1000 Python MCQ (Multiple Choice Questions) - Sanfoundry
d) 0 1 2 3
View Answer
Answer: d
i takes values 0, 1, 2 and 3.
Explanation:
b) Custom function
User defined function
c) Built-in function &
d) User function
View Answer
Answer: c
Explanation: Built-in functions and user defined ones. The built-in functions are part of the Python
language. Examples are: dir(), len() or abs(). The user defined functions are functions created with the
def keyword.
1. def addItem(listParam):
2. listParam += [1]
3.
4. mylist = [1, 2, 3, 4]
5. addItem(mylist)
6. print(len(mylist))
a) 5
b) 8
c) 2
d) 1
View Answer
Answer: a
Explanation: + will append the element to the list.
View Answer
https://www.sanfoundry.com/1000-python-questions-answers/ 18/21
12/30/21, 11:12 AM 1000 Python MCQ (Multiple Choice Questions) - Sanfoundry
Answer: d
Explanation: Tuples are represented with round brackets.
54. What will be the output of the following Python code snippet?
z=set('abc$de')
'a' in z
a) Error
b) True
c) False
d) No output
View Answer
Answer: b
Explanation: The code shown above is used to check whether a particular item is a part of a given set
or not. Since ‘a’ is a part of the set z, the output is true. Note that this code would result in an error in
the absence of the quotes.
round(4.576)
a) 4
b) 4.6
c) 5
d) 4.5
View Answer
Answer: c
Explanation: This is a built-in function which rounds a number to give precision in decimal digits. In
the above case, since the number of decimal places has not been specified, the decimal number is
rounded off to a whole number. Hence the output will be 5.
View Answer
Answer: d
Python has a nifty feature called documentation strings, usually referred to by its
Explanation:
https://www.sanfoundry.com/1000-python-questions-answers/ 19/21
12/30/21, 11:12 AM 1000 Python MCQ (Multiple Choice Questions) - Sanfoundry
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.
c) Hello
foo and bin
d) None of the mentioned
View Answer
Answer: c
Explanation: The elements of the tuple are accessed by their indices.
View Answer
Answer: a
math.pow() returns a floating point number.
Explanation:
View Answer
Answer: b
Explanation: Each object in Python has a unique id. The id() function returns the object’s id.
x = [[0], [1]]
a) 01
b) [0]
[1]
https://www.sanfoundry.com/1000-python-questions-answers/ 20/21
12/30/21, 11:12 AM 1000 Python MCQ (Multiple Choice Questions) - Sanfoundry
c) (’01’)
d) (‘[0] [1]’,)
View Answer
Answer: d
(element,) is not the same as element. It is a tuple with one item.
Explanation:
View Answer
Answer: a
Explanation: Pickling is the process of sterilizing a Python object, that is, conversion of a byte stream
into Python object hierarchy. The reverse of this process is known as unpickling.
def foo():
try:
return 1
finally:
return 2
k = foo()
print(k)
a) error, there is more than one return statement in a single try-finally block
b) 3
c) 2
d) 1
View Answer
Answer: c
Explanation: The finally block is executed even there is a return statement in the try block.
https://www.sanfoundry.com/1000-python-questions-answers/ 21/21