Python Programming MCQs
Python Programming MCQs
Answer : b
Answer: a
Answer: a
Answer: d
5. Given a function that does not return any value, What value is thrown by default
when executed in shell.
a) int
b) bool
c) void
d) None
Answer: d
Answer: a
Answer: a
Answer: a
9. What error occurs when you execute the following Python code snippet?
apple = mango
a) SyntaxError
b) NameError
c) ValueError
d) TypeError
Answer: b
Answer: a
11. In order to store values in terms of key and value we use what core data type.
a) list
b) tuple
c) class
d) dictionary
Answer: d
Answer: c
Answer: a
Answer: b
a)a
b)ab
c)cd
d) dc
Answer: c
16. What will be the output of the following Python code?
1. >>>str1="helloworld"
2. >>>str1[::-1]
a) dlrowolleh
b) hello
c) world
d) helloworld
Answer: a
Answer: a
Answer: c
19. Suppose list1 is [2, 33, 222, 14, 25], What is list1[-1]?
a) Error
b) None
c) 25
d) 2
Answer: c
20. What will be the output of the following Python code?
1. >>>names = ['Amir', 'Bear', 'Charlton', 'Daman']
2. >>>print(names[-1][-1])
a) A
b) Daman
c) Error
d) n
Answer: d
Answer: a
Answer: b
Answer: b
a) (1, 2)
b) (1, 2, 4)
c) (2, 4)
d) (2, 4, 3)
Answer: c
Answer: d
26. Which of the following is not the correct syntax for creating a set?
a) set([[1,2],[3,4]])
b) set([1,2,2,3,4])
c) set((1,2,3,4))
d) {1,2,3,4}
Answer: a
27. Which of the following functions is a built-in function in python?
a) seed()
b) sqrt()
c) factorial()
d) print()
Answer: d
round(4.576)
a) 4.5
b) 5
c) 4
d) 4.6
Answer: b
Answer: c
Answer: a
31. Which of the following functions does not necessarily accept only iterables as
arguments?
a) enumerate()
b) all()
c) chr()
d) max()
Answer: c
Answer: a
Answer: c
Answer: a
Answer: a
Answer: a
a) error
b) 24000
c) 430
d) 432
Answer : d
Answer: c
39. If a function doesn’t have a return statement, which of the following does the
function return?
a) int
b) null
c) None
d) An exception is thrown without the return statement
Answer: c
40. What is the type of each element in sys.argv?
a) set
b) list
c) tuple
d) string
Answer: d
41. How are keyword arguments specified in the function heading?
a) one-star followed by a valid identifier
b) one underscore followed by a valid identifier
c) two stars followed by a valid identifier
d) two underscores followed by a valid identifier
Answer: c
42. How many keyword arguments can be passed to a function in a single function
call?
a) zero
Answer: c
43. How are variable length arguments specified in the function heading?
a) one star followed by a valid identifier
b) one underscore followed by a valid identifier
c) two stars followed by a valid identifier
d) two underscores followed by a valid identifier
Answer: a
44. Which module in the python standard library parses options received from the
command line?
a) getopt
b) os
c) getarg
d) main
Answer: a
45. How are default arguments specified in the function heading?
a) identifier followed by an equal to sign and the default value
b) identifier followed by the default value within backticks (“)
c) identifier followed by the default value within square brackets ([])
d) identifier
Answer: a
46. Which of the following data structures is returned by the functions globals()
and locals()?
a) list
b) set
c) dictionary
d) tuple
Answer: c
47.What happens if a local variable exists with the same name as the global
variable you want to access?
a) Error
b) The local variable is shadowed
c) Undefined behavior
d) The global variable is shadowed
Answer: d
48.Which is the most appropriate definition for recursion?
a) A function that calls itself
b) A function execution instance that calls another execution instance of the same
function
c) A class method that calls another class method
Answer: b
49.Which of these is false about recursion?
a) Recursive function can be replaced by a non-recursive function
b) Recursive functions usually take more memory space than non-recursive function
c) Recursive functions run faster than non-recursive function
d) Recursion makes programs easier to understand
Answer: c
50.What is tail recursion?
a) A recursive function that has two base cases
b) A function where the recursive functions leads to an infinite loop
c) A recursive function where the function doesn’t return anything and just prints the
values
d) A function where the recursive call is the last thing executed by the function
Answer: d
51.What happens if the base condition isn’t defined in recursive programs?
a) Program gets into an infinite loop
b) Program runs once
c) Program runs n number of times where n is the argument given to the function
d) An exception is thrown
Answer: a
52.Which of the following functions can help us to find the version of python that
we are currently working on?
a) sys.version
b) sys.version()
c) sys.version(0)
d) sys.version(1)
Answer: a
53.Program code making use of a given module is called a ______ of the module.
a) Client
b) Docstring
c) Interface
d) Modularity
Answer: a
54.______ is a string literal denoted by triple quotes for providing the
specifications of certain program elements.
a) Interface
b) Modularity
c) Client
d) Docstring
Answer: d
Answer: b
56.What will be the output of the following Python code?
a) 120
b) Nothing is printed
c) Error, method factorial doesn’t exist in math module
d) Error, the statement should be: print(factorial(5))
Answer: d
57.What will be the output of the following Python code if the system date is 18th
August, 2016?
tday=datetime.date.today()
print(tday.month())
a) August
b) Aug
c) 08
d) 8
Answer: d
58.Which of the following functions can be used to find the coordinated universal
time, assuming that the datetime module has already been imported?
a) datetime.utc()
b) datetime.datetime.utc()
c) datetime.utcnow()
d) datetime.datetime.utcnow()
Answer: d
59.The sleep function (under the time module) is used to ___________
a) Pause the code for the specified number of seconds
b) Return the specified number of seconds, in terms of milliseconds
c) Stop the execution of the code
d) Return the output of the code had it been executed earlier by the specified number of
seconds
Answer: a
Answer: a
61. What will be the output of the following Python code?
import random
random.choice(2,3,4)
Answer: c
62. Which of the following returns a string that represents the present working
directory?
a) os.getcwd()
b) os.cwd()
c) os.getpwd()
d) os.pwd()
Answer: a
63. What does os.link() do?
a) create a symbolic link
b) create a hard link
c) create a soft link
d) none of the mentioned
Answer: b
64. The command which helps us to reset the pen (turtle):
a) turtle.reset
b) turtle.penreset
c) turtle.penreset()
d) turtle.reset()
Answer: d
65. Which of the following functions does not accept any arguments?
a) position
b) fillcolor
c) goto
d) setheading()
Answer: a
import turtle
t=turtle.Pen()
t.goto(300,9)
t.position()
a) 300.00, 9.00
b) 9, 300
c) 300, 9
d) 9.00, 300.00
Answer: a
67. What will be the output of the following Python code?
import turtle
t=turtle.Pen()
t.color(0,0,1)
t.begin_fill()
t.circle(15)
t.end_fill()
a) Error
b) A circle filled in with the colour red
c) A circle filled in with the colour blue
d) A circle filled in with the colour green
Answer: c
68. Which of the following functions can be used to make the arrow black?
a) turtle.color(0,1,0)
b) turtle.color(1,0,0)
c) turtle.color(0,0,1)
d) turtle.color(0,0,0)
Answer: d
69. In which direction is the turtle pointed by default?
a) North
b) South
c) East
d) West
Answer: c
70. The command used to set only the x coordinate of the turtle at 45 units is:
a) reset(45)
b) setx(45)
c) xset(45)
d) xreset(45)
Answer: b
Answer: a
72. Which of the following functions results in an error?
a) turtle.shape(“turtle”)
b) turtle.shape(“square”)
c) turtle.shape(“triangle”)
d) turtle.shape(“rectangle”)
Answer: d
73. What will be the output of the following Python code?
import turtle
t=turtle.Pen()
t.backward(100)
t.penup()
t.right(45)
t.isdown()
a) True
b) False
c) Yes
d) No
Answer: b
74. The function used to alter the thickness of the pen to ‘x’ units:
a) turtle.width(x)
b) turtle.span(x)
c) turtle.girth(x)
d) turtle.thickness(x)
Answer: a
75.Which module in Python supports regular expressions?
a) re
b) regex
c) pyregex
d) none of the mentioned
Answer: a
Answer: c
77. What does the function re.match do?
a) matches a pattern at the start of the string
b) matches a pattern at any position in the string
c) such a function does not exist
d) none of the mentioned
Answer: a
78. What does the function re.search do?
a) matches a pattern at the start of the string
b) matches a pattern at any position in the string
c) such a function does not exist
d) none of the mentioned
Answer: b
79. Which function overloads the + operator?
a) __add__()
b) __plus__()
c) __sum__()
d) none of the mentioned
Answer: a
80. Which operator is overloaded by __invert__()?
a) !
b) ~
c) ^
d) –
Answer: b
81. _____ represents an entity in the real world with its identity and behaviour.
a) A method
b) An object
c) A class
d) An operator
Answer: b
82. . _____ is used to create an object.
a) class
b) constructor
c) User-defined functions
d) In-built functions
Answer: b
83. The assignment of more than one function to a particular operator is _______
a) Operator over-assignment
b) Operator overriding
Answer: c
84. Which of the following is not a class method?
a) Non-static
b) Static
c) Bounded
d) Unbounded
Answer: a
85. What are the methods which begin and end with two underscore characters
called?
a) Special methods
b) In-built methods
c) User-defined methods
d) Additional methods
Answer: a
86.What does print(Test.__name__) display (assuming Test is the name of the
class)?
a) ()
b) Exception is thrown
c) Test
d) __main__
Answer: c
87. Which of the following statements is wrong about inheritance?
a) Protected members of a class can be inherited
b) The inheriting class is called a subclass
c) Private members of a class can be inherited and accessed
d) Inheritance is one of the features of OOP
Answer: c
Answer: a
89. What type of inheritance is illustrated in the following Python code?
class A():
pass
class B():
pass
class C(A,B):
a) Multi-level inheritance
b) Multiple inheritance
c) Hierarchical inheritance
d) Single-level inheritance
Answer: b
90. What is the biggest reason for the use of polymorphism?
a) It allows the programmer to think at a more abstract level
b) There is less program code to write
c) The program will have a more elegant design and will be easier to maintain and
update
d) Program code takes up less space
Answer: c
91. Which of the following statements is true?
a) A non-private method in a superclass can be overridden
b) A subclass method can be overridden by the superclass
c) A private method in a superclass can be overridden
d) Overriding isn’t possible in Python
Answer: a
92. How many except statements can a try-except block have?
a) zero
b) one
c) more than one
d) more than zero
Answer: d
93. When will the else part of try-except-else be executed?
a) always
b) when an exception occurs
c) when no exception occurs
d) when an exception occurs in to except block
Answer: c
94. Is the following Python code valid?
try:
# Do something
except:
# Do something
finally:
# Do something
Answer: a
96. When is the finally block executed?
a) when there is no exception
b) when there is an exception
c) only if some condition that has been specified is satisfied
d) always
Answer: d
97. What happens when ‘1’ == 1 is executed?
a) we get a True
b) we get a False
c) an TypeError occurs
d) a ValueError occurs
Answer: b
98. To open a file c:\scores.txt for reading, we use _____________
a) infile = open(“c:\scores.txt”, “r”)
b) infile = open(“c:\\scores.txt”, “r”)
c) infile = open(file = “c:\scores.txt”, “r”)
d) infile = open(file = “c:\\scores.txt”, “r”)
Answer: b
99. To read two characters from a file object infile, we use ____________
a) infile.read(2)
b) infile.read()
c) infile.readline()
d) infile.readlines()
Answer: a
100. What will be the output of the following Python code snippet?
print('Ab!2'.swapcase())
a) AB!@
b) ab12
c) aB!2
d) aB1@
Answer: c