Select Page
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages
Filter by Categories
nmims post
Objective Type Set
Online MCQ Assignment
Question Solution
Solved Question
Uncategorized

Python MCQ Set 1

1. 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 [Reason:] The functions shown above will change the arrow to the shape mentioned. The functions turtle.shape(“turtle”), turtle.shape(“square”) and turtle.shape(“triangle”) are valid whereas the function turtle.shape(“rectangle”) is invalid.

2. What is the output of the snippet of code shown below?

```import turtle
t=turtle.Pen
t.tilt(75)
t.forward(100)```

a) A straight line of 100 units tiled at 75 degrees from the horizontal
b) A straight line of 100 units tilted at 15 degrees from the horizontal
c) A straight line of 100 units lying along the horizontal
d) Error

Answer: c [Reason:] The function turtle.tilt(75) will tilt the turtle. But the straight line (of 100 units) is drawn along the horizontal. Hence the output of the code shown above is a straight line of 100 units lying along the horizontal.

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

```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 [Reason:] In the code shown above, we have used the function t.penup() to life the pen from the canvas. However, we have not used the function t.pendown() to keep the pen back down. The function turtle.isdown() returns True if the pen is down and False if the pen is not down. Hence the output is False.

4. The function used to alter the thickness of the pen to ‘x’ units:
a) turtle.width(x)
b) tutle.span(x)
c) turtle.girth(x)
d) turtle.thickness(x)

Answer: a [Reason:] The function turtle.width(x) is used to alter the thickness of the pen to ‘x’ units. The function turtle.span(x), turtle.girth(x) and turtle.thickness(x) are invalid.

5. What is the output of the code shown below if the system date is 18th June, 2017 (Sunday)?

```import turtle
t=turtle.Pen()
t.goto(100,0)
t.towards(0,0)```

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

Answer: b [Reason:] The function t.towards(x,y) returns the angle between the line to the line specified by (x,y). Hence the output will be 180.0.

6. What is the output of the following code?

```import turtle
t=turtle.Pen()
t.position()
(100.00,0.00)
t.goto(100,100)
t.distance(100,0)```

a) 0.0
b) Error
c) 100.0, 100.0
d) 100.0

Answer: d [Reason:] The distance() function returns the distance between the turtle to the given vector. Hence the output of the code shown above is 100.0.

7. The output of the following code will result in a shape similar to the alphabet ___________

```import turtle
t=turtle.Turtle()
t1=turtle.Turtle()
t.left(45)
t1.left(135)
t.forward(100)
t1.forward(100)```

a) V
b) Inverted V
c) X
d) T

Answer: a [Reason:] In the code shown above, two pens have been used to create a shape similar to the alphabet ‘V’. The angle between the two straight lines is 90 degrees.

8. The output of the code shown is similar to the alphabet _______________

```import turtle
t=turtle.Pen()
t1=turtle.Pen()
t2=turtle.Pen()
t.forward(100)
t1.forward(100)
t2.forward(100)
t1.left(90)
t1.forward(75)
t2.right(90)
t2.forward(75)```

a) X
b) N
c) T
d) M

Answer: c [Reason:] In the above code, three pens have been used to create a shape similar to the letter ‘T’. All the three straight lines are mutually perpendicular.

9. The code shown below will result in an error. State whether true or false.

```import turtle
t=turtle.Pen()
t.speed(-45)
t.circle(30)```

a) True
b) False

Answer: b [Reason:] Although a negative speed is not possible, the code shown above does not result in an error. Hence, the answer is False.

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

```import turtle()
t=turtle.Pen()
t.goto(50,60)
t1=t.clone()
t1.ycor()```

a) 0.0
b) 50.0
c) 60.0
d) Error

Answer: c [Reason:] The function clone() is used to create a clone of the turtle, having the same properties such as position, coordinates etc. Hence, the properties of the t and t1 are the same in the code shown above. The function ycor() returns the y-coordinate of the turtle. Hence the output of the code is 60.0.

11. The output shape of the code shown above:

```import turtle
t=turtle.Pen()
for i in range(0,6):
t.forward(100)
t.left(60)```

a) Hexagon
b) Octagon
c) Pentagon
d) Heptagon

Answer: a [Reason:] The code shown above creates a six-sided polygon. The output shape of the code shown above is will be a hexagon.

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

```import turtle
t=turtle.Pen()
t.resizemode(“aidlo”)
t.resizemode()```

a) user
b) auto
c) nonresize
d) error

Answer: c [Reason:] When not explicitly specified as auto or user, no adaption of the turtle’s appearance takes place and the mode is ‘noresize’. Hence the output of the code is: noresize.

Python MCQ Set 2

1. Is Python case sensitive when dealing with identifiers?
a) yes
b) no
c) machine dependent
d) none of the mentioned

Answer: a [Reason:] Case is always significant.

2. What is the maximum possible length of an identifier?
a) 31 characters
b) 63 characters
c) 79 characters
d) none of the mentioned

Answer: d [Reason:] Identifiers can be of any length.

3. Which of the following is invalid?
a) _a = 1
b) __a = 1
c) __str__ = 1
d) none of the mentioned

Answer: d [Reason:] All the statements will execute successfully but at the cost of reduced readability.

4. Which of the following is an invalid variable?
a) my_string_1
b) 1st_string
c) foo
d) _

5. Why are local variable names beginning with an underscore discouraged?
a) they are used to indicate a private variables of a class
b) they confuse the interpreter
c) they are used to indicate global variables
d) they slow down execution

Answer: a [Reason:] As Python has no concept of private variables, leading underscores are used to indicate variables that must not be accessed from outside the class.

6. Which of the following is not a keyword?
a) eval
b) assert
c) nonlocal
d) pass

Answer: a [Reason:] eval can be used as a variable.

7. All keywords in Python are in
a) lower case
b) UPPER CASE
c) Capitalized
d) None of the mentioned

Answer: d [Reason:] True, False and None are capitalized while the others are in lower case.

8. Which of the following is true for variable names in Python?
a) unlimited length
b) all private members must have leading and trailing underscores
c) underscore and ampersand are the only two special characters allowed
d) none of the mentioned

Answer: a [Reason:] Variable names can be of any length.

9. Which of the following is an invalid statement?
a) abc = 1,000,000
b) a b c = 1000 2000 3000
c) a,b,c = 1000, 2000, 3000
d) a_b_c = 1,000,000

Answer: b [Reason:] Spaces are not allowed in variable names.

10. Which of the following cannot be a variable?
a) __init__
b) in
c) it
d) on

Answer: b [Reason:] in is a keyword.

Python MCQ Set 3

1. What is the output of the following?

```x = ['ab', 'cd']
for i in x:
i.upper()
print(x)```

a) [‘ab’, ‘cd’].
b) [‘AB’, ‘CD’].
c) [None, None].
d) none of the mentioned

Answer: a [Reason:] The function upper() does not modify a string in place, it returns a new string which isn’t being stored anywhere.

2. What is the output of the following?

```x = ['ab', 'cd']
for i in x:
x.append(i.upper())
print(x)```

a) [‘AB’, ‘CD’].
b) [‘ab’, ‘cd’, ‘AB’, ‘CD’].
c) [‘ab’, ‘cd’].
d) none of the mentioned

Answer: d [Reason:] The loop does not terminate as new elements are being added to the list in each iteration.

3. What is the output of the following?

```i = 1
while True:
if i%3 == 0:
break
print(i)

i + = 1```

a) 1 2
b) 1 2 3
c) error
d) none of the mentioned

Answer: c [Reason:] SyntaxError, there shouldn’t be a space between + and = in +=.

4. What is the output of the following?

```i = 1
while True:
if i%0O7 == 0:
break
print(i)
i += 1```

a) 1 2 3 4 5 6
b) 1 2 3 4 5 6 7
c) error
d) none of the mentioned

Answer: a [Reason:] Control exits the loop when i becomes 7.

5. What is the output of the following?

```i = 5
while True:
if i%0O11 == 0:
break
print(i)
i += 1```

a) 5 6 7 8 9 10
b) 5 6 7 8
c) 5 6
d) error

Answer: b [Reason:] 0O11 is an octal number.

6. What is the output of the following?

```i = 5
while True:
if i%0O9 == 0:
break
print(i)
i += 1```

a) 5 6 7 8
b) 5 6 7 8 9
c) 5 6 7 8 9 10 11 12 13 14 15 ….
d) error

Answer: d [Reason:] 9 isn’t allowed in an octal number.

7. What is the output of the following?

```i = 1
while True:
if i%2 == 0:
break
print(i)
i += 2```

a) 1
b) 1 2
c) 1 2 3 4 5 6 …
d) 1 3 5 7 9 11 …

Answer: d [Reason:] The loop does not terminate since i is never an even number.

8. What is the output of the following?

```i = 2
while True:
if i%3 == 0:
break
print(i)
i += 2```

a) 2 4 6 8 10 …
b) 2 4
c) 2 3
d) error

Answer: b [Reason:] The numbers 2 and 4 are printed. The next value of i is 6 which is divisible by 3 and hence control exits the loop.

9. What is the output of the following?

```i = 1
while False:
if i%2 == 0:
break
print(i)
i += 2```

a) 1
b) 1 3 5 7 …
c) 1 2 3 4 …
d) none of the mentioned

Answer: d [Reason:] Control does not enter the loop because of False.

10. What is the output of the following?

```True = False
while True:
print(True)
break```

a) True
b) False
c) None
d) none of the mentioned

Answer: d [Reason:] SyntaxError, True is a keyword and it’s value cannot be changed.

Python MCQ Set 4

1. Python supports the creation of anonymous functions at runtime, using a construct called __________
a) Lambda
b) pi
c) anonymous
d) None of the mentioned

Answer: a [Reason:] Python supports the creation of anonymous functions (i.e. functions that are not bound 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.

2. What is the output of this program?

1. `y = 6`
2. `z = lambda x: x * y`
3. `print z(8)`

a) 48
b) 14
c) 64
d) None of the mentioned

Answer: a [Reason:] The lambda keyword creates an anonymous function. The x is a parameter, that is passed to the lambda function. The parameter is followed by a colon character. The code next to the colon is the expression that is executed, when the lambda function is called. The lambda function is assigned to the z variable. The lambda function is executed. The number 8 is passed to the anonymous function and it returns 48 as the result. Note that z is not a name for this function. It is only a variable to which the anonymous function was assigned.

3. What is the output of below program?

1. `lamb = lambda x: x ** 3`
2. `print(lamb(5))`

a) 15
b) 555
c) 125
d) None of the mentioned

4. Does Lambda contains return statements?
a) True
b) False

Answer: b [Reason:] lambda definition does not include a return statement. it always contains an expression which is returned. Also note that we can put a lambda definition anywhere a function is expected. We don’t have to assign it to a variable at all.

5. Lambda is a statement.
a) True
b) False

Answer: b [Reason:] lambda is an anonymous function in Python. Hence this statement is false.

6. Lambda contains block of statements
a) True
b) False

7. What is the output of below program?

1. `def f(x, y, z): return x + y + z`
2. `f(2, 30, 400)`

a) 432
b) 24000
c) 430
d) No output

8. What is the output of below program?

1. `def writer():`
2. `	title = 'Sir'`
3. `	name = (lambda x:title + ' ' + x)`
4. `	return name`
5. ` `
6. `who = writer()`
7. `who('Arthur')`

a) Arthur Sir
b) Sir Arthur
c) Arthur
d) None of the mentioned

9. What is the output of this program?

1. `L = [lambda x: x ** 2,`
2. `         lambda x: x ** 3,`
3. `         lambda x: x ** 4]`
4. ` `
5. `for f in L:`
6. `	print(f(3))`

a) 27
81
343
b) 6
9
12
c) 9
27
81
d) None of the mentioned

10. What is the output of this program?

1. `min = (lambda x, y: x if x < y else y)`
2. ` min(101*99, 102*98)`

a) 9997
b) 9999
c) 9996
d) None of the mentioned

Python MCQ Set 5

1. Which are the advantages of functions in python?
a) Reducing duplication of code
b) Decomposing complex problems into simpler pieces
c) Improving clarity of the code
d) All of the mentioned

2. What are the two main types of functions?
a) Custom function
b) Built-in function & User defined function
c) User function
d) System function

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

3. Where is function defined?
a) Module
b) Class
c) Another function
d) All of the mentioned

Answer: d [Reason:] Functions can be defined inside a module, a class or another function.

4. What is called when a function is defined inside a class?
a) Module
b) Class
c) Another function
d) Method

5. Which of the following is the use of id() function in python?
a) Id returns the identity of the object
b) Every object doesn’t have a unique id
c) All of the mentioned
d) None of the mentioned

Answer: a [Reason:] Each object in Python has a unique id. The id() function returns the object’s id.

6. Which of the following refers to mathematical function?
a) sqrt
b) rhombus
d) rhombus

Answer: a [Reason:] Functions that are always available for usage, functions that are contained within external modules, which must be imported and functions defined by a programmer with the def keyword. Eg: math import sqrt The sqrt() function is imported from the math module.

7. What is the output of below program?

1. `def cube(x):`
2. `    return x * x * x      `
3. `x = cube(3)    `
4. `print x`

a) 9
b) 3
c) 27
d) 30

Answer: c [Reason:] A function is created to do a specific task. Often there is a result from such a task. The return keyword is used to return values from a function. A function may or may not return a value. If a function does not have a return keyword, it will send a none value.

8. What is the output of the below program?

1. `def C2F(c):`
2. `    return c * 9/5 + 32`
3. `print C2F(100)`
4. `print C2F(0)`

a) 212
32
b) 314
24
c) 567
98
d) None of the mentioned

Answer: a [Reason:] The code shown above is used to convert a temperature in degree celsius to fahrenheit.

9. What is the output of the below program?

1. `def power(x, y=2):`
2. `    r = 1`
3. `    for i in range(y):`
4. `       r = r * x`
5. `    return r`
6. `print power(3)`
7. `print power(3, 3)`

a) 212
32
b) 9
27
c) 567
98
d) None of the mentioned

Answer: b [Reason:] The arguments in Python functions may have implicit values. An implicit value is used, if no value is provided. Here we created a power function. The function has one argument with an implicit value. We can call the function with one or two arguments.

10. What is the output of the below program?

1. `def sum(*args):`
2. `   '''Function returns the sum `
3. `   of all values'''`
4. `   r = 0`
5. `   for i in args:`
6. `      r += i`
7. `   return r`
8. `print sum.__doc__`
9. `print sum(1, 2, 3)`
10. `print sum(1, 2, 3, 4, 5)`

a) 6
15
b) 6
100
c) 123
12345
d) None of the mentioned