Python MCQ
Python MCQ
Answer: d
1. Who developed Python Programming Language?
Explanation: Most keywords are in lowercase, but
a) Wick van Rossum
some like True, False, and None are capitalized.
b) Rasmus Lerdorf
c) Guido van Rossum 7. What will be the value of the following Python
d) Niene Stom expression?
Answer: c
print(4 + 3 % 5)
Explanation: Python language is designed by a
Dutch programmer Guido van Rossum in the a) 7
Netherlands. b) 2
c) 4
2. Which type of Programming does Python
d) 1
support?
Answer: a
a) object-oriented programming
Explanation: In Python, the modulus operator %
b) structured programming
has higher precedence than addition +. So, the
c) functional programming
expression is evaluated as 4 + (3 % 5), which is 4 +
d) all of the mentioned
3 = 7.
Answer: d
Explanation: Python is an interpreted programming 8. Which of the following is used to define a block
language, which supports object-oriented, of code in Python language?
structured, and functional programming. a) Indentation
b) Key
3. Is Python case sensitive when dealing with
c) Brackets
identifiers?
d) All of the mentioned
a) no
Answer: a
b) yes
Explanation: In Python, to define a block of code
c) machine dependent
we use indentation. Indentation refers to
d) none of the mentioned
whitespaces at the beginning of the line.
Answer: b
Explanation: Case is always significant while 9. Which keyword is used for function in Python
dealing with identifiers in python. language?
a) Function
4. Which of the following is the correct extension
b) def
of the Python file?
c) Fun
a) .python
d) Define
b) .pl
c) .py
d) .p Answer: b
Answer: c Explanation: The def keyword is used to create, (or
Explanation: ‘.py’ is the correct extension of the define) a function in python.
Python file. Python programs can be written in any
text editor. To save these programs we need to 10. Which of the following character is used to give
save in files with file extension ‘.py’. single-line comments in Python?
a) //
5. Is Python code compiled or interpreted? b) #
a) Python code is both compiled and interpreted c) !
b) Python code is neither compiled nor interpreted d) /*
c) Python code is only compiled Answer: b
d) Python code is only interpreted Explanation: To write single-line comments in
Answer: a Python use the Hash character (#) at the beginning
Explanation: Many languages have been of the line. It is also called number sign or pound
implemented using both compilers and sign. To write multi-line comments, close the text
interpreters, including C, Pascal, and Python. between triple quotes.
advertisement Example: “”” comment
text “””
6. All keywords in Python are in _________
a) Capitalized 11. What will be the output of the following
b) lower case Python code?
c) UPPER CASE i=1
while True: Division, Addition, Subtraction
if i%3 == 0:
Answer: d
break
Explanation: Python follows the PEMDAS rule
print(i) (similar to BODMAS): Parentheses, Exponentiation,
Multiplication/Division, then Addition/Subtraction.
i+=1 Operators at the same level are evaluated left to
a) 1 2 3 right.
b) SyntaxError 15. What will be the output of the following
c) 1 2 Python code snippet if x=1?
d) none of the mentioned
Answer: b x<<2
Explanation: The output will be a SyntaxError
a) 4
because i + = 1 is invalid syntax in Python. There
b) 2
should be no space between + and =. The correct
c) 1
syntax is i += 1.
d) 8
12. Which of the following functions can help us to
find the version of python that we are currently
Answer: a
working on?
Explanation: The binary form of 1 is 0001. The
a) sys.version(1)
expression x<<2 implies we are performing bitwise
b) sys.version(0)
left shift on x. This shift yields the value: 0100,
c) sys.version()
which is the binary form of the number 4.
d) sys.version
Answer: d 16. What does pip stand for python?
Explanation: The function sys.version can help us a) Pip Installs Python
to find the version of python that we are currently b) Pip Installs Packages
working on. It also contains information on the c) Preferred Installer Program
build number and compiler used. For example, d) All of the mentioned
3.5.2, 2.7.3 etc. this function also returns the Answer: c
current date, time, bits etc along with the version. Explanation: pip is a package manager for python.
Which is also called Preferred Installer Program.
13. Python supports the creation of anonymous
functions at runtime, using a construct called 17. Which of the following is true for variable
__________ names in Python?
a) pi a) underscore and ampersand are the only two
b) anonymous special characters allowed
c) lambda b) unlimited length
d) none of the mentioned c) all private members must have leading and
trailing underscores
d) none of the mentioned
Answer: c
18. What are the values of the following Python
Explanation: In Python, lambda functions are
expressions?
anonymous, meaning they don’t have a name.
They are defined using the lambda keyword and print(2**(3**2))
can take any number of arguments but only have
one expression. Lambdas are useful for creating print((2**3)**2)
small, throwaway functions quickly without print(2**3**2)
formally defining them using def.
a) 512, 64, 512
14. What is the order of precedence in python? b) 512, 512, 512
a) Exponential, Parentheses, Multiplication, c) 64, 512, 64
Division, Addition, Subtraction d) 64, 64, 64
b) Exponential, Parentheses, Division, Answer: a
Multiplication, Addition, Subtraction Explanation: Expression 1 is evaluated as: 2**9,
c) Parentheses, Exponential, Multiplication, which is equal to 512. Expression 2 is evaluated as
Addition, Division, Subtraction 8**2, which is equal to 64. The last expression is
d) Parentheses, Exponential, Multiplication, evaluated as 2**(3**2). This is because the
associativity of ** operator is from right to left. 23. The following python program can work with
Hence the result of the third expression is 512. ____ parameters.
D Answer: a
Explanation: The expression [1, 2, 3, 4][::-1] uses
slicing with a step of -1 to reverse the list. So the temp = tester(12)
list becomes [4, 3, 2, 1]. The for loop iterates over
print(temp.id)
this reversed list and prints each element, with
end=’ ‘ ensuring the output is on one line with a) 12
spaces in between. b) 224
c) None
32. What will be the output of the following
d) Error
Python statement?
Answer: a
print("a"+"bc") Explanation: When the tester class is instantiated
with temp = tester(12), the __init__ method is
a) bc
called. The id argument is passed as 12, and inside
b) abc
the __init__ method, self.id is assigned the string
c) a
value of id, which is “12”. However, the local
d) bca
variable id is reassigned to “224”, but this change
does not affect self.id, which retains the value “12”.
Answer: b 36. What will be the output of the following
Explanation: In Python, the + operator is used for Python program?
string concatenation. “a” + “bc” joins the two
strings together into a single string “abc”. def foo(x):
a) {‘a’, ‘c’, ‘c’, ‘p’, ‘q’, ‘s’, ‘a’, ‘n’} result.extend(i for i in list2 if i not in (list1+list3)
b) {‘abc’, ‘p’, ‘q’, ‘san’} and i not in result)
c) {‘a’, ‘b’, ‘c’, ‘p’, ‘q’, ‘san’}
result.extend(i for i in list3 if i not in (list1+list2)
d) {‘a’, ‘b’, ‘c’, [‘p’, ‘q’], ‘san}
and i not in result)
print(result)
Answer: c
Explanation: The code shown first adds the a) [1, 3, 5, 7, 8]
element ‘san’ to the set z. The set z is then b) [1, 7, 8]
updated and two more elements, namely, ‘p’ and c) [1, 2, 4, 7, 8]
‘q’ are added to it. Hence the output is: {‘a’, ‘b’, ‘c’, d) error
‘p’, ‘q’, ‘san’}
list1[0] = 4 i=0
a) [1, 4] print(i)
b) [1, 3, 4]
c) [4, 3] i += 1
d) [1, 3] if i == 3:
break
Answer: c
Explanation: In the code, list2 = list1 creates a else:
reference to the same list in memory. So when
print(0)
list1[0] is changed to 4, list2 also reflects that
change. The output is [4, 3]. a) error
b)
46. Which one of the following is the use of
function in python? 0
a) Functions don’t provide better modularity for
1
your application
b) you can’t also create your own functions 2
c) Functions are reusable pieces of programs
d) All of the mentioned 3
0
Answer: c c)
Explanation: Functions are reusable pieces of
programs. They allow you to give a name to a block 0
of statements, allowing you to run that block using
1
the specified name anywhere in your program and
any number of times. 2
47. Which of the following Python statements will d) none of the mentioned
result in the output: 6?
print(i) print('a' in z)
a) error a) Error
b) 1 2 3 4 b) True
c) a b c d c) False
d) 0 1 2 3 d) No output
Answer: d Answer: b
Explanation: i takes values 0, 1, 2 and 3. Explanation: The code shown above is used to
check whether a particular item is a part of a given
51. What are the two main types of functions in
set or not. Since ‘a’ is a part of the set z, the output
Python?
is true. Note that this code would result in an error
a) System function
in the absence of the quotes.
b) Custom function
c) Built-in function & User defined function 55. What will be the output of the following
d) User function Python expression?
Answer: c
print(round(4.576))
Explanation: Built-in functions and user defined
ones. The built-in functions are part of the Python a) 4
language. Examples are: dir(), len() or abs(). The b) 4.6
user defined functions are functions created with c) 5
the def keyword. d) 4.5
52. What will be the output of the following
Python program? Answer: c
def addItem(listParam): Explanation: The round() function rounds the
number to the nearest integer by default. Since
listParam += [1] 4.576 is closer to 5 than 4, round(4.576) returns 5.
Therefore, the output is 5.
x = [[0], [1]]
a) 01
b) [0] [1]
c) (’01’)
d) (‘[0] [1]’,)
Answer: d
Explanation: In this code, map(str, x) converts each
inner list [0] and [1] into strings: “[0]” and “[1]”.
Then ‘ ‘.join(…) creates the string “[0] [1]”. Finally, it
is wrapped in a tuple using the comma syntax,
resulting in (‘[0] [1]’,).
def foo():