Python MCQ Number 01387

Python MCQ Set 1

1. 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

Answer: a [Reason:] 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.

2. Which of the following functions is not defined under the sys module?
a) sys.platform
b) sys.path
c) sys.readline
d) sys.argv

Answer

Answer: c [Reason:] The functions sys.platform, sys.path and sys.argv are defined under the sys module. The function sys.readline is not defines. However, sys.stdin.readline is defined.

3. The output of the functions len(“abc”) and sys.getsizeof(“abc”) will be the same. State whether true or false.
a) True
b) False

Answer

Answer: b [Reason:] The function len returns the length of the string passed, and hence it’s output will be 3. The function getsizeof, present under the sys module returns the size of the object passed. It’s output will be a value much larger than 3. Hence the above statement is false.

4. What is the output of the code shown below, if the code is run on Windows operating system?

import sys
if sys.platform[:2]== 'wi':
print("Hello")

a) Error
b) Hello
c) No output
d) Junk value

Answer

Answer: b [Reason:] The output of the function sys.platform[:2] is equal to ‘wi’, when this code is run on windows operating system. Hence the output printed is ‘hello’.

5. What is the output of the following line of code, if the sys module has already been imported?

sys.stdout.write("hello world")

a) helloworld
b) hello world10
c) hello world11
d) error

Answer

Answer: c [Reason:] The function shown above prints the given string along with the length of the string. Hence the output of the function shown above will be hello world11.

6. What is the output of this code?

import sys
eval(sys.stdin.readline())
"India"

a) India5
b) India
c) ‘Indian’
d) ‘India’

Answer

Answer: d [Reason:] The function shown above evaluates the input into a string. Hence if the input entered is enclosed in double quotes, the output will be enclosed in single quotes. Therefore, the output of this code is ‘India’.

7. What is the output of the code shown below?

import sys
eval(sys.stdin.readline())
Computer

a) Error
b) ‘Computern’
c) Computer8
d) Computer

Answer

Answer: a [Reason:] The code shown above will result in an error. This is because this particular function accepts only strings enclosed in single or double inverted quotes, or numbers. Since the string entered above is not enclosed in single or double inverted quotes, an error will be thrown.

8. What is the output of the code shown below?

import sys
sys.argv[0]

a) Junk value
b) ‘ ‘
c) No output
d) Error

Answer

Answer: b [Reason:] The output of the function shown above will be a blank space enclosed in single quotes. Hence the output of the code shown above is ‘ ‘.

9. What is the output of the code shown below is:

import sys
sys.stderr.write(“hello”)

a) ‘hello’
b) ‘hellon’
c) hello
d) hello5

Answer

Answer: d [Reason:] The code shown above returns the string, followed by the length of the string. Hence the output of the code shown above is hello5.

10. What is the output of the code shown below?

import sys
sys.argv

a) ‘ ‘
b) [ ]
c) [‘ ‘]
d) Error

Answer

Answer: c [Reason:] The output of the code shown above is a blank space inserted in single quotes, which is enclosed by square brackets. Hence the output will be [‘ ‘].

11. To obtain a list of all the functions defined under sys module, which of the following functions can be used?
a) print(sys)
b) print(dir.sys)
c) print(dir[sys])
d) print(dir(sys))

Answer

Answer: d [Reason:] The function print(dir(sys)) helps us to obtain a list of all the functions defined under the sys module. The function can be used to obtain the list of functions under any given module in Python.

12. The output of the function len(sys.argv) is ____________
a) Error
b) 1
c) 0
d) Junk value

Answer

Answer: b [Reason:] The output of the function sys.argv is [‘ ‘]. When we execute the function len([‘ ‘]), the output is 1. Hence the output of the function len(sys.argv) is also 1.

Python MCQ Set 2

1. What is the data type of (1)?
a) Tuple
b) Integer
c) List
d) Both tuple and integer

Answer

Answer: b [Reason:] A tuple of one element must be created as (1,).

2. If a=(1,2,3,4), a[1:-1] is
a) Error, tuple slicing doesn’t exist
b) [2,3].
c) (2,3,4)
d) (2,3)

Answer

Answer: d [Reason:] Tuple slicing exists and a[1:-1] returns (2,3).

3. What is the output of the following code?

>>> a=(1,2,(4,5))
>>> b=(1,2,(3,4))
>>> a<b

a) False
b) True
c) Error, < operator is not valid for tuples
d) Error, < operator is valid for tuples but not if there are sub-tuples

Answer

Answer: a [Reason:] Since the first element in the sub-tuple of a is larger that the first element in the subtuple of b, False is printed.

4. What is the output of the following piece of code when executed in Python shell?

>>> a=("Check")*3
>>> a

a) (‘Check’,’Check’,’Check’)
b) * Operator not valid for tuples
c) (‘CheckCheckCheck’)
d) Syntax error

Answer

Answer: c [Reason:] Here (“Check”) is a string not a tuple because there is no comma after the element.

5. What is the output of the following code?

>>> a=(1,2,3,4)
>>> del(a[2])

a) Now, a=(1,2,4)
b) Now, a=(1,3,4)
c) Now a=(3,4)
d) Error as tuple is immutable

Answer

Answer: d [Reason:] ‘tuple’ object doesn’t support item deletion.

6. What is the output of the following code?

>>> a=(2,3,4)
>>> sum(a,3)

a) Too many arguments for sum() method
b) The method sum() doesn’t exist for tuples
c) 12
d) 9

Answer

Answer: c [Reason:] In the above case, 3 is the starting value to which the sum of the tuple is added to.

7. Is the following piece of code valid?

>>> a=(1,2,3,4)
>>> del a

a) No because tuple is immutable
b) Yes, first element in the tuple is deleted
c) Yes, the entire tuple is deleted
d) No, invalid syntax for del method

Answer

Answer: c [Reason:] The command del a deletes the entire tuple.

8. What type of data is: a=[(1,1),(2,4),(3,9)]?
a) Array of tuples
b) List of tuples
c) Tuples of lists
d) Invalid type

Answer

Answer: b [Reason:] The variable a has tuples enclosed in a list making it a list of tuples.

9. What is the output of the following piece of code?

>>> a=(0,1,2,3,4)
>>> b=slice(0,2)
>>> a[b]

a) Invalid syntax for slicing
b) [0,2].
c) (0,1)
d) (0,2)

Answer

Answer: c [Reason:] The method illustrated in the above piece of code is that of naming of slices.

10. Is the following piece of code valid?

>>> a=(1,2,3)
>>> b=('A','B','C')
>>> c=zip(a,b)

a) Yes, c will be ((1,2,3),(‘A’,’B’,’C’))
b) Yes, c will be ((1,2,3),(‘A’,’B’,’C’))
c) No because tuples are immutable
d) No because the syntax for zip function isn’t valid

Answer

Answer: a [Reason:] Zip function combines individual elements of two iterrables into tuples. Execute in Python shell to verify.

Python MCQ Set 3

1. Is the following piece of code valid?

>>> a,b,c=1,2,3
>>> a,b,c

a) Yes, [1,2,3] is printed
b) No, invalid syntax
c) Yes, (1,2,3) is printed
d) 1 is printed

Answer

Answer: c [Reason:] A tuple needn’t be enclosed in parenthesis.

2. What is the output of the following piece of code?

a = ('check',)
n = 2
for i in range(int(n)):
    a = (a,)
    print(a)

a) Error, tuples are immutable
b) ((‘check’,),)
(((‘check’,),),).
c) ((‘check’,)’check’,)
d) ((‘check’,)’check’,)
(((‘check’,)’check’,)’check’,)

Answer

Answer: b [Reason:] The loop runs two times and each time the loop runs an extra parenthesis along with a comma is added to the tuple (as a=(a’)).

3. Is the following line of code valid?

>>> a,b=1,2,3

a) Yes, this is an example of tuple unpacking. a=1 and b=2
b) Yes, this is an example of tuple unpacking. a=(1,2) and b=3
c) No, too many values to unpack
d) Yes, this is an example of tuple unpacking. a=1 and b=(2,3)

Answer

Answer: c [Reason:] For unpacking to happen, the number of values of the right hand side must be equal to the number of variables on the left hand side.

4. What is the output of the following piece of code when executed in Python shell?

>>> a=(1,2)
>>> b=(3,4)
>>> c=a+b
>>> c

a) (4,6)
b) (1,2,3,4)
c) Error as tuples are immutable
d) None

Answer

Answer: b [Reason:] In the above piece of code, the values of the tuples aren’t being changed. Both the tuples are simply concatenated.

5. What is the output of the following code?

>>> a,b=6,7
>>> a,b=b,a
>>> a,b

a) (6,7)
b) Invalid syntax
c) (7,6)
d) Nothing is printed

Answer

Answer: c [Reason:] The above piece of code illustrates the unpacking of variables.

6. What is the output of the following code?

>>> import collections
>>> a=collections.namedtuple('a',['i','j'])
>>> obj=a(i=4,j=7)
>>> obj

a) a(i=4, j=7)
b) obj(i=4, j=7)
c) (4,7)
d) An exception is thrown

Answer

Answer: a [Reason:] The above piece of code illustrates the concept of named tuples.

7. Tuples can’t be made keys of a dictionary. True or False?
a) True
b) False

Answer

Answer: b [Reason:] Tuples can be made keys of a dictionary because they are hashable.

8. Is the following piece of code valid?

>>> a=2,3,4,5
>>> a

a) Yes, 2 is printed
b) Yes, [2,3,4,5] is printed
c) No, too many values to unpack
d) Yes, (2,3,4,5) is printed

Answer

Answer: d [Reason:] A tuple needn’t be enclosed in parenthesis.

9. What is the output of the following piece of code?

>>> a=(2,3,1,5)
>>> a.sort()
>>> a

a) (1,2,3,5)
b) (2,3,1,5)
c) None
d) Error, tuple has no attribute sort

Answer

Answer: d [Reason:] A tuple is immutable thus it doesn’t have a sort attribute.

10. Is the following piece of code valid?

>>> a=(1,2,3)
>>> b=a.update(4,)

a) Yes, a=(1,2,3,4) and b=(1,2,3,4)
b) Yes, a=(1,2,3) and b=(1,2,3,4)
c) No because tuples are immutable
d) No because wrong syntax for update() method

Answer

Answer: c [Reason:] Tuple doesn’t have any update() attribute because it is immutable.

11. What is the output of the following piece of code?

>>> a=[(2,4),(1,2),(3,9)]
>>> a.sort()
>>> a

a) [(1, 2), (2, 4), (3, 9)].
b) [(2,4),(1,2),(3,9)].
c) Error because tuples are immutable
d) Error, tuple has no sort attribute

Answer

Answer: d [Reason:] A list of tuples is a list itself. Hence items of a list can be sorted.

Python MCQ Set 4

1. What is the output shape of the code shown?

import turtle
t=turtle.Pen()
for i in range(0,4):
	t.forward(100)
	t.left(120)

a) square
b) rectangle
c) triangle
d) kite

Answer

Answer: c [Reason:] According to the code shown above, 4 lines will be drawn. Three lines will be in the shape of a triangle. The fourth line will trace the base, which is already drawn. Hence the base will be slightly thicker than the rest of the lines. However there will be no change in the shape due to this extra line. Hence the output shape will be a triangle.

2. The number of lines drawn in each case, assuming that the turtle module has been imported:

Case 1:
for i in range(0,10):
turtle.forward(100)
turtle.left(90)
Case 2:
for i in range(1,10):
turtle.forward(100)
turtle.left(90)

a) 10, 9
b) 9, 10
c) 9, 9
d) 10, 10

Answer

Answer: a [Reason:] The number of lines drawn in the first case is 10, while that in the second case is 9.

3. The command which helps us to reset the pen (turtle):
a) turtle.reset
b) turtle.penreset
c) turtle.penreset()
d) turtle.reset()

Answer

Answer: d [Reason:] The command turtle.reset() helps us to reset the pen. After the execution of this command, we get a blank page with an arrow on it. We can then perform any desired operation on this page.

4. Fill in the blank such that the code shown below results in the formation of an inverted, equilateral triangle.

import turtle
t=turtle.Pen()
for i in range(0,3):
	t.forward(150)
	t.right(_____)

a) -60
b) 120
c) -120
c) 60

Answer

Answer: b [Reason:] An angle of -120 will result in the formation of an upright, equilateral triangle. An angle of 120 will result in the formation of an inverted triangle. The angles of 60 and -60 do not result in the formation of a triangle.

5. What will be the output shape of the code shown below?

import turtle
t=turtle.Pen()
for i in range(1,4):
	t.forward(60)
	t.left(90)

a) Rectangle
b) Trapezium
c) Triangle
d) Square

Answer

Answer: d [Reason:] The code shown above will result in the formation of a square, with each of side 60.

6. What is the output of the following code?

import turtle
t=turtle.Pen()
for i in range(0,4):
	t.forward(100)
	t.left(90)
 
t.penup()
t.left(90)
t.forward(200)
for i in range(0,4):
	t.forward(100)
	t.left(90)

a) Error
b) 1 square
c) 2 squares, at a separation of100 units, joined by a straight line
d) 2 squares, at a separation of 100 units, without a line joining them

Answer

Answer: b [Reason:] The output of the code shown above will be a single square. This is because the function t.penup() is used to lift the pen after the construction of the first square. However, the function t.pendown() has not been used to put the pen back down. Hence, the output shape of this code is one square, of side 100 units.

7. Which of the following functions does not accept any arguments?
a) position
b) fillcolor
c) goto
d) setheading()

Answer

Answer: a [Reason:] The functions fillcolor(), goto() and setheading() accept arguments, whereas the function position() does not accept any arguments. The function position() returns the current position of the turtle.

8. What are the outcomes of the code shown below?

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

Answer: a [Reason:] The goto functions takes the arrow to the position specified by the user as arguments. The position function returns the current position of the arrow. Hence the output of the code shown above will be: 300.00, 9.00.

9. What is the output of the code shown below?

import turtle
t=turtle.Pen()
for i in range(0,5):
        t.left(144)
        t.forward(100)

a) Trapezium
b) Parallelepiped
c) Tetrahedron
d) Star

Answer

Answer: d [Reason:] It is clear from the above code that 5 lines will be drawn on the canvas, at an angle of 144 degrees. The only shape which fits this description is star. Hence the output of the code shown above is star.

10. What is the output of the functions shown below?

import turtle
t=turtle.Pen()
for i in range(0,3):
	t.forward(100)
	t.left(120)
 
t.back(100)
for i in range(0,3):
	t.forward(100)
	t.left(120)

a) Error
b) Two triangles, joined by a straight line
c) Two triangles, joined at one vertex
d) Two separate triangles, not connected by a line

Answer

Answer: c [Reason:] The output of the code shown above is two equilateral triangles (of side 100 units), joined at the vertex.

Python MCQ Set 5

1. The output of the code shown below:

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

Answer: c [Reason:] The function t.colour(0, 0, 1) is used to fill in the colour blue into any given shape. Hence the output of the code shown above will be a circle filled in with the colour blue.

2. 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

Answer: d [Reason:] The function turtle.color(0,0,0) can change the colour of the arrow. The function turtle.color(0,1,0) will make the arrow green. The function turtle.color(1,0,0) will make the arrow red. The function turtle.color(0,0,1) will make the arrow blue. The function turtle.color(0,0,0) will make the arrow black.

3. What is the output of the code shown?

import turtle
t=turtle.Pen()
t.color(1,1,1)
t.begin_fill()
for i in range(0,3):
	t.forward(100)
	t.right(120)
              t.end_fill()

a) Blank page
b) A triangle filled in with the colour yellow
c) A triangle which is not filled in with any colour
d) Error

Answer

Answer: a [Reason:] The code shown above will result in a blank page. This is because the command turtle.color(1,1,1) eliminates the arrow from the page. Hence all the commands after this command are ineffective.

4. What is the output of the code shown below?

import turtle
t=turtle.Pen()
t.color(0,1,0)
t.begin_fill()
for i in range(0,4):
	t.forward(100)
	t.right(90)

a) A square filled in with the colour green
b) A square outlined with the colour green
c) Blank canvas
d) Error

Answer

Answer: c [Reason:] The output shape of the code shown above is a square, outlined with the colour green, but not filled in with any colour. This is because we have not used the command t.end_fill() at the end.

5. In which direction is the turtle pointed by default?
a) North
b) South
c) East
d) West

Answer

Answer: c [Reason:] By default, the turtle is pointed towards the east direction. We can change the direction of the turtle by using certain commands. However, whenever the turtle is reset, it points towards east.

6. 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

Answer: b [Reason:] The command setx(45) is used to set the x coordinate of the turtle. Similarly, the command sety() is used to set the y coordinate of the turtle. The function reset() takes two values as arguments, one for the x-coordinate and the other for the y-coordinate.

7. Which of the following functions returns a value in degrees, counter-clockwise from the horizontal right?
a) heading()
b) degrees()
c) position()
d) window_height()

Answer

Answer: a [Reason:] The function heading() returns the heading of the turtle, which is a value in degrees counter-clockwise from the horizontal right. This measure will be in radians if radians() has been called.

8. What is the output of the following code?

import turtle
t=turtle.Pen()
t.right(90)
t.forward(100)
t.heading()

a) 0.0
b) 90.0
c) 270.0
d) 360.0

Answer

Answer: c [Reason:] The output of the code shown above will be 270.0. The function heading() returns the heading of the turtle, a value in degrees, counter-clockwise from the horizontal right. The output shape of this code is a straight line pointing downwards.

9. What is the output of the code shown below?

import turtle
t=turtle.Pen()
t.clear()
t.isvisible()

a) Yes
b) True
c) No
d) False

Answer

Answer: b [Reason:] The function t.clear() returns a blank canvas, without changing the position of the turtle. Since the turtle is visible on the blank canvas, the output of this code is: Yes.

10. What is the output of the code shown?

import turtle
t=turtle.Pen()
t.forward(100)
t.left(90)
t.clear()
t.position()

a) 0.00, 90.00
b) 0.00, 0.00
c) 100.00, 90.00
d) 100.00, 100.00

Answer

Answer: d [Reason:] The output of the code shown above is 100.00, 100.00. The function clear() is used to erase the entire canvas and redraw the turtle. However, the position of the turtle is not changed.

Synopsis and Project Report

You can buy synopsis and project from distpub.com. Just visit https://distpub.com/product-category/projects/ and buy your university/institute project from distpub.com

ed010d383e1f191bdb025d5985cc03fc?s=120&d=mm&r=g

DistPub Team

Distance Publisher (DistPub.com) provide project writing help from year 2007 and provide writing and editing help to hundreds student every year.