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. What is the output of the following?

for i in range(2.0):
    print(i)

a) 0.0 1.0
b) 0 1
c) error
d) none of the mentioned

View Answer

Answer: c [Reason:] Object of type float cannot be interpreted as an integer.

2. What is the output of the following?

for i in range(int(2.0)):
    print(i)

a) 0.0 1.0
b) 0 1
c) error
d) none of the mentioned

View Answer

Answer: b [Reason:] range(int(2.0)) is the same as range(2).

3. What is the output of the following?

for i in range(float('inf')):
    print (i)

a) 0.0 0.1 0.2 0.3 …
b) 0 1 2 3 …
c) 0.0 1.0 2.0 3.0 …
d) none of the mentioned

View Answer

Answer: d [Reason:] Error, objects of type float cannot be interpreted as an integer.

4. What is the output of the following?

for i in range(int(float('inf'))):
    print (i)

a) 0.0 0.1 0.2 0.3 …
b) 0 1 2 3 …
c) 0.0 1.0 2.0 3.0 …
d) none of the mentioned

View Answer

Answer: d [Reason:] OverflowError, cannot convert float infinity to integer.

5. What is the output of the following?

for i in [1, 2, 3, 4][::-1]:
    print (i)

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

View Answer

Answer: b [Reason:] [::-1] reverses the list.

6. What is the output of the following?

for i in ''.join(reversed(list('abcd'))):
    print (i)

a) a b c d
b) d c b a
c) error
d) none of the mentioned

View Answer

Answer: b [Reason:] ‘ ‘.join(reversed(list(‘abcd’))) reverses a string.

7. What is the output of the following?

for i in 'abcd'[::-1]:
    print (i)

a) a b c d
b) d c b a
c) error
d) none of the mentioned

View Answer

Answer: b [Reason:] [::-1] reverses the string.

8. What is the output of the following?

for i in '':
    print (i)

a) None
b) (nothing is printed)
c) error
d) none of the mentioned

View Answer

Answer: b [Reason:] The string does not have any character to loop over.

9. What is the output of the following?

x = 2
for i in range(x):
    x += 1
    print (x)

a) 0 1 2 3 4 …
b) 0 1
c) 3 4
d) 0 1 2 3

View Answer

Answer: c [Reason:] Variable x is incremented and printed twice.

10. What is the output of the following?

x = 2
for i in range(x):
    x -= 2
    print (x)

a) 0 1 2 3 4 …
b) 0 -2
c) 0
d) error

View Answer

Answer: b [Reason:] The loop is entered twice.

Python MCQ Set 2

1. The output of the snippet of code shown below is:

'%d %s %g you' %(1, 'hello', 4.0)

a) Error
b) 1 hello you 4.0
c) 1 hello 4 you
d) 1 4 hello you

View Answer

Answer: c [Reason:] In the snippet of code shown above, three values are inserted into the target string. When we insert more than one value, we should group the values on the right in a tuple. The % formatting expression operator expects either a single item or a tuple of one or more items on its right side.

2. The output of which of the codes shown below will be: “There are 4 blue birds.”?
a) ‘There are %g %d birds.’ %4 %blue
b) ‘There are %d %s birds.’ %(4, blue)
c) ‘There are %s %d birds.’ %[4, blue]
d) ‘There are %d %s birds.’ 4, blue

View Answer

Answer: b [Reason:] The code ‘There are %d %s birds.’ %(4, blue) results in the output: There are 4 blue birds. When we insert more than one value, we should group the values on the right in a tuple.

3. What is the output of the code shown below if the system date is 18th August, 2016?

x=1234
res='integers:...%d...%-6d...%06d' %(x, x, x)
res

a) ‘integers:…1234…1234 …001234’
b) ‘integers…1234…1234…123400’
c) ‘integers:… 1234…1234…001234’
d) ‘integers:…1234…1234…001234’

View Answer

Answer: a [Reason:] The code shown above prints 1234 for the format specified %d, ‘ 1234’ for the format specifier %-6d, and 001234 for the format specifier %06d. Hence the output of this code is: ) ‘integers:…1234…1234 …001234’

4. What is the output of the code shown?

x=3.3456789
'%f | %e | %g' %(x, x, x)

a) Error
b) ‘3.3456789 | 3.3456789+00 | 3.345678’
c) ‘3.345678 | 3.345678e+0 | 3.345678’
d) ‘3.345679 | 3.345679e+00 | 3.34568’

View Answer

Answer: d [Reason:] The %f %e and %g format specifiers represent floating point numbers in different ways. %e and %E are the same, except that the exponent is in lowercase. %g chooses the format by number content. Hence the output of this code is: ‘3.345679 | 3.345679e+00 | 3.34568’.

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

x=3.3456789
'%-6.2f | %05.2f | %+06.1f' %(x, x, x)

a) ‘3.35 | 03.35 | +003.3’
b) ‘3.3456789 | 03.3456789 | +03.3456789’
c) Error
d) ‘3.34 | 03.34 | 03.34+’

View Answer

Answer: a [Reason:] The code shown above rounds the floating point value to two decimal places. In this code, a variety of addition formatting features such as zero padding, total field width etc. Hence the output of this code is: ‘3.35 | 03.35 | +003.3’.

6. What is the output of the code shown?

x=3.3456789
'%s' %x, str(x)

a) Error
b) (‘3.3456789’, ‘3.3456789’)
c) (3.3456789, 3.3456789)
d) (‘3.3456789’, 3.3456789)

View Answer

Answer: b [Reason:] We can simply convert strings with a %s format expression or the str built-in function. Both of these methods have been shown in this code. Hence the output is: ) (‘3.3456789’, ‘3.3456789’)

7. What is the output of the code shown?

'%(qty)d more %(food)s' %{'qty':1, 'food': 'spam'}

a) Error
b) No output
c) ‘1 more foods’
d) ‘1 more spam’

View Answer

Answer: d [Reason:] String formatting also allows conversion targets on the left to refer to the keys in a dictionary coded on the right and fetch the corresponding values. In the code shown above, (qty) and (food) in the format string on the left refers to keys in the dictionary literal on the right and fetch their assorted values. Hence the output of the code shown above is: 1 more spam.

8. What is the output of the code shown?

a='hello'
q=10
vars()

a) {‘a’ : ‘hello’, ‘q’ : 10, ……..plus built-in names set by Python….}
b) {……Built in names set by Python……}
c) {‘a’ : ‘hello’, ‘q’ : 10}
d) Error

View Answer

Answer: a [Reason:] The built in function vars() returns a dictionary containing all the variables that exist in the place. Hence the output of the code shown above is: {‘a’ : ‘hello’, ‘q’ : 10, ……..plus built-in names set by Python….}

9. The output of the code shown below is:

s='{0}, {1}, and {2}'
s.format('hello', 'good', 'morning')

a) ‘hello good and morning’
b) ‘hello, good, morning’
c) ‘hello, good, and morning’
d) Error

View Answer

Answer: c [Reason:] Within the subject string, curly braces designate substitution targets and arguments to be inserted either by position or keyword. Hence the output of the code shown above:’hello, good,and morning’.

10. What is the output of the code shown?

s='%s, %s & %s'
s%('mumbai', 'kolkata', 'delhi')

a) mumbai kolkata & delhi
b) Error
c) No output
d) ‘mumbai, kolkata & delhi’

View Answer

Answer: d [Reason:] In the code shown above, the format specifier %s is replaced by the designated substitution. Hence the output of the code shown above is: ‘mumbai, kolkata & delhi’.

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

t = '%(a)s, %(b)s, %(c)s'
t % dict(a='hello', b='world', c='universe')

a) ‘hello, world, universe’
b) ‘hellos, worlds, universes’
c) Error
d) hellos, world, universe

View Answer

Answer: a [Reason:] Within the subject string, curly braces represent substitution targets and arguments to be inserted. Hence the output of the code shown above: ‘hello, world, universe’.

12. What is the output of the code shown?

'{a}, {0}, {abc}'.format(10, a=2.5, abc=[1, 2])

a) Error
b) ‘2.5, 10, [1, 2]’
c) 2.5, 10, 1, 2
d) ’10, 2.5, [1, 2]’

View Answer

Answer: b [Reason:] Since we have specified that the order of the output be: {a}, {0}, {abc}, hence the value of associated with {a} is printed first followed by that of {0} and {abc}. Hence the output of the code shown above is: ‘2.5, 10, [1, 2]’.

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

'{0:.2f}'.format(1.234)

a) ‘1’
b) ‘1.234’
c) ‘1.23’
d) ‘1.2’

View Answer

Answer: c [Reason:] The code shown above displays the string method to round off a given decimal number to two decimal places. Hence the output of the code is: ‘1.23’.

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

'%x %d' %(255, 255)

a) ‘ff, 255’
b) ‘255, 255’
c) ‘15f, 15f’
d) Error

View Answer

Answer: a [Reason:] The code shown above converts the given arguments to hexadecimal and decimal values and prints the result. This is done using the format specifiers %x and %d respectively. Hence the output of the code shown above is: ‘ff, 255’.

15. The output of the two codes shown below is the same. State whether true or false.

'{0:.2f}'.format(1/3.0)
'%.2f'%(1/3.0)

a) True
b) False

View Answer

Answer: a [Reason:] The two codes shown above represent the same operation, but in different formats. The output of both of these functions is: ‘0.33’. Hence the statement is true.

Python MCQ Set 3

1. What is the output of the following?

x = 123
for i in x:
    print(i)

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

View Answer

Answer: c [Reason:] Objects of type int are not iterable.

2. What is the output of the following?

d = {0: 'a', 1: 'b', 2: 'c'}
for i in d:
    print(i)

a) 0 1 2
b) a b c
c) 0 a 1 b 2 c
d) none of the mentioned

View Answer

Answer: a [Reason:] Loops over the keys of the dictionary.

3. What is the output of the following?

d = {0: 'a', 1: 'b', 2: 'c'}
for x, y in d:
    print(x, y)

a) 0 1 2
b) a b c
c) 0 a 1 b 2 c
d) none of the mentioned

View Answer

Answer: d [Reason:] Error, objects of type int aren’t iterable.

4. What is the output of the following?

d = {0: 'a', 1: 'b', 2: 'c'}
for x, y in d.items():
    print(x, y)

a) 0 1 2
b) a b c
c) 0 a 1 b 2 c
d) none of the mentioned

View Answer

Answer: c [Reason:] Loops over key, value pairs.

5. What is the output of the following?

d = {0: 'a', 1: 'b', 2: 'c'}
for x in d.keys():
    print(d[x])

a) 0 1 2
b) a b c
c) 0 a 1 b 2 c
d) none of the mentioned

View Answer

Answer: b [Reason:] Loops over the keys and prints the values.

6. What is the output of the following?

d = {0: 'a', 1: 'b', 2: 'c'}
for x in d.values():
    print(x)

a) 0 1 2
b) a b c
c) 0 a 1 b 2 c
d) none of the mentioned

View Answer

Answer: b [Reason:] Loops over the values.

7. What is the output of the following?

d = {0: 'a', 1: 'b', 2: 'c'}
for x in d.values():
    print(d[x])

a) 0 1 2
b) a b c
c) 0 a 1 b 2 c
d) none of the mentioned

View Answer

Answer: d [Reason:] Causes a KeyError.

8. What is the output of the following?

d = {0, 1, 2}
for x in d.values():
    print(x)

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

View Answer

Answer: c [Reason:] Objects of type set have no attribute values.

9. What is the output of the following?

d = {0, 1, 2}
for x in d:
    print(x)

a) 0 1 2
b) {0, 1, 2} {0, 1, 2} {0, 1, 2}
c) error
d) none of the mentioned

View Answer

Answer: a [Reason:] Loops over the elements of the set and prints them.

10. What is the output of the following?

d = {0, 1, 2}
for x in d:
    print(d.add(x))

a) 0 1 2
b) 0 1 2 0 1 2 0 1 2 …
c) None None None
d) None of the mentioned

View Answer

Answer: c [Reason:] Variable x takes the values 0, 1 and 2. set.add() returns None which is printed.

11. What is the output of the following?

for i in range(0):
    print(i)

a) 0
b) no output
c) error
d) none of the mentioned

View Answer

Answer: b [Reason:] range(0) is empty.

Python MCQ Set 4

1. What is a variable defined outside a function referred to as?
a) A static variable
b) A global variable
c) A local variable
d) An automatic variable

View Answer

Answer: b [Reason:] The value of a variable defined outside all function definitions is referred to as a global variable and can be used by multiple functions of the program.

2. What is a variable defined inside a function referred to as?
a) A global variable
b) A volatile variable
c) A local variable
d) An automatic variable

View Answer

Answer: c [Reason:] The variable inside a function is called as local variable and the variable definition is confined only to that function.

3. What is the output of the following code?

i=0
def change(i):
   i=i+1
   return i
change(1)
print(i)

a) 1
b) Nothing is displayed
c) 0
d) An exception is thrown

View Answer

Answer: c [Reason:] Any change made in to an immutable data type in a function isn’t reflected outside the function.

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

def a(b):
    b = b + [5]
 
c = [1, 2, 3, 4]
a(c)
print(len(c))

a) 4
b) 5
c) 1
d) An exception is thrown

View Answer

Answer: b [Reason:] Since a list is mutable, any change made in the list in the function is reflected outside the function.

5. What is the output of the following code?

a=10
b=20
def change():
    global b
    a=45
    b=56
change()
print(a)
print(b)

a)10
56
b)45
56
c)10
20
d)Syntax Error

View Answer

Answer: a [Reason:] The statement “global b” allows the global value of b to be accessed and changed. Whereas the variable a is local and hence the change isn’t reflected outside the function.

6. What is the output of the following code?

def change(i = 1, j = 2):
    i = i + j
    j = j + 1
    print(i, j)
change(j = 1, i = 2)

a) An exception is thrown because of conflicting values
b) 1 2
c) 3 3
d) 3 2

View Answer

Answer: d [Reason:] The values given during function call is taken into consideration, that is, i=2 and j=1.

7. What is the output of the following code?

def change(one, *two):
   print(type(two))
change(1,2,3,4)

a) Integer
b) Tuple
c) Dictionary
d) An exception is thrown

View Answer

Answer: b [Reason:] The parameter two is a variable parameter and consists of (2,3,4). Hence the data type is tuple.

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

View Answer

Answer: c [Reason:] A function can exist without a return statement and returns None if the function doesn’t have a return statement.

9. What is the output of the following code?

def display(b, n):
    while n > 0:
        print(b,end="")
        n=n-1
display('z',3)

a) zzz
b) zz
c) An exception is executed
d) Infinite loop

View Answer

Answer: a [Reason:] The loop runs three times and ‘z’ is printed each time.

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

def find(a, **b):
   print(type(b))
find('letters',A='1',B='2')

a) String
b) Tuple
c) Dictionary
d) An exception is thrown

View Answer

Answer: c [Reason:] b combines the remaining parameters into a dictionary.

Python MCQ Set 5

1. The output of the code shown below is:

odd=lambda x: bool(x%2)
numbers=[n for n in range(10)]
print(numbers)
n=list()
for i in numbers:
    if odd(i):
        continue
    else:
        break

a) [0, 2, 4, 6, 8, 10]
b) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
c) [1, 3, 5, 7, 9]
d) Error

View Answer

Answer: b [Reason:] The code shown above returns a new list containing whole numbers up to 10 (excluding 10). Hence the output of the code is: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9].

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

f=lambda x:bool(x%2)
print(f(20), f(21))

a) False True
b) False False
c) True True
d) True False

View Answer

Answer: a [Reason:] The code shown above will return true if the given argument is an odd number, and false if the given argument is an even number. Since the arguments are 20 and 21 respectively, the output of this code is: False True.

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

import functools
l=[1,2,3,4]
print(functools.reduce(lambda x,y:x*y,l))

a) Error
b) 10
c) 24
d) No output

View Answer

Answer: c [Reason:] The code shown above returns the product of all the elements of the list. Hence the output is 1*2*3*4 = 24.

4. What is the output of the code shown?

l=[1, -2, -3, 4, 5]
def f1(x):
    return x<2
m1=filter(f1, l)
print(list(m1))

a) [1, 4, 5 ]
b) Error
c) [-2, -3]
d) [1, -2, -3]

View Answer

Answer: d [Reason:] The code shown above returns only those elements from the list, which are less than 2. The functional programming tool used to achieve this operation is filter. Hence the output of the code is:[1, -2, -3].

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

l=[-2, 4]
m=map(lambda x:x*2, l)
print(m)

a) [-4, 16]
b) Address of m
c) Error
d) -4
16

View Answer

Answer: b [Reason:] The code shown above returns the address of m. Had we used the statement: print(list(m)), the output would have been: [-4, 16].

6. What is the output of the following code?

l=[1, -2, -3, 4, 5]
def f1(x):
    return x<-1
m1=map(f1, l)
print(list(m1))

a) [False, False, False, False, False]
b) [False, True, True, False, False]
c) [True, False, False, True, True]
d) [True, True, True, True, True]

View Answer

Answer: b [Reason:] This code shown returns a list which contains True if the corresponding element of the list is less than -1, and false if the corresponding element is greater than -1. Hence the output of the code shown above: [False, True, True, False, False].

7. What is the output of the code shown?

l=[1, 2, 3, 4, 5]
m=map(lambda x:2**x, l)
print(list(m))

a) [1, 4, 9, 16, 25 ]
b) [2, 4, 8, 16, 32 ]
c) [1, 0, 1, 0, 1]
d) Error

View Answer

Answer: b [Reason:] The code shown above prints a list containing each element of the list as the power of two. That is, the output is: [2, 4, 8, 16, 32].

8. What is the output of the code shown?

import functools
l=[1, 2, 3, 4, 5]
m=functools.reduce(lambda x, y:x if x>y else y, l)
print(m)

a) Error
b) Address of m
c) 1
d) 5

View Answer

Answer: d [Reason:] The code shown above can be used to find the maximum of the elements from the given list. In the above code, this operation is achieved by using the programming tool reduce. Hence the output of the code shown above is 5.

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

l=[n for n in range(5)]
f=lambda x:bool(x%2)
print(f(3), f(1))
for i in range(len(l)):
    if f(l[i]):
        del l[i]
        print(i)

a) True True
1
2
Error
b) False False
1
2
c) True False
1
2
Error
d) False True
1
2

View Answer

Answer: a [Reason:] The code shown above prints true if the value entered as an argument is odd, else false is printed. Hence the output: True True. The error is due to the list index being out of range.

10. What is the output of the code shown?

m=reduce(lambda x: x-3 in range(4, 10))
print(list(m))

a) [1, 2, 3, 4, 5, 6, 7]
b) No output
c) [1, 2, 3, 4, 5, 6]
d) Error

View Answer

Answer: b [Reason:] The code shown above will result in an error. This is because e have not imported functools. Further, ‘reduce’, as such is not defined. We should use functools.reduce to remove the error.

11. Which of the following numbers will not be a part of the output list of the code shown below?

def sf(a):
    return a%3!=0 and a%5!=0
m=filter(sf, range(1, 31))
print(list(m))

a) 1
b) 29
c) 6
d) 10

View Answer

Answer: d [Reason:] The output list of the code shown above will not contain any element that is divisible by 3 or 5. Hence the number which is not present in the output list is 10. The output list: [1, 2, 4, 7, 8, 11, 13, 14, 16, 17, 19, 22, 23, 26, 28, 29]

12. The single line equivalent of the code shown below is:

l=[1, 2, 3, 4, 5]
def f1(x):
    return x<0
m1=filter(f1, l)
print(list(m1))

a) filter(lambda x:x<0, l) b) filter(lambda x, y: x<0, l) c) filter(reduce x<0, l) d) reduce(x: x<0, l) [learn_more caption="View Answer"]Answer: a [Reason:] The code shown above returns a new list containing only those elements from list l, which are less than 0. Since there are no such elements in the list l, the output of this code is: [].The single line equivalent of this code is filter(lambda x:x<0, l)
.[/learn_more]

13. What is the output of the line of code shown below?

list(map((lambda x:x^2), range(10)))

a) [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
b) Error
c) [2, 3, 0, 1, 6, 7, 4, 5, 10, 11]
d) No output

View Answer

Answer: c [Reason:] The line of code shown above returns a list of each number from 1 to 10, after an XOR operation is performed on each of these numbers with 2. Hence the output of this code is: [2, 3, 0, 1, 6, 7, 4, 5, 10, 11]

14. What is the output of the line of code shown below?

list(map((lambda x:x**2), filter((lambda x:x%2==0), range(10))))

a) [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
b) [0, 4, 16, 36, 64]
c) Error
d) No output

View Answer

Answer: b [Reason:] The output list will contain each number up to 10 raised to 2, except odd numbers, that is, 1, 3, 5, 9. Hence the output of the code is: [0, 4, 16, 36, 64].

15. The output of the two codes shown below is the same. State whether true or false.

[x**2 for x in range(10)]
list(map((lambda x:x**2), range(10)))

a) True
b) False

View Answer

Answer: a [Reason:] Both of the codes shown above print each whole number up to 10, raised to the power 2. Hence the output of both of these codes is: [0, 1, 4, 9, 16, 25, 36, 49, 64, 81].Therefore, the statement is true.

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