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. The output of the snippet of code shown below is:

import datetime
d=datetime.date(2016,7,24)
print(d)

a) Error
b) 2017-07-24
c) 2017-7-24
d) 24-7-2017

View Answer

Answer: b [Reason:] In the snippet of code shown above, we are simply printing the date entered by us. We enter the date in the format: yyyy,m,dd. The date is then printed in the format: yyyy-mm-dd. Hence the output is: 2017-07-24.

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

import datetime
d=datetime.date(2017,06,18)
print(d)

a) Error
b) 2017-06-18
c) 18-06-2017
d) 06-18-2017

View Answer

Answer: a [Reason:] The code shown above will result in an error because of the format of the date entered. Had the date been entered as: d=datetime.date(2017,6,18), no error would have been thrown.

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

tday=datetime.date.today()
print(tday.month())

a) August
b) Aug
c) 08
d) 8

View Answer

Answer: d [Reason:] The code shown above prints the month number from the system date. Therefor the output will be 8 if the system date is 18th August, 2016.

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

import datetime
tday=datetime.date.today()
print(tday)

a) 18-06-2017
b) 06-18-2017
c) 2017-06-18
d) Error

View Answer

Answer: c [Reason:] The code shown above prints the system date in the format yyyy-mm-dd. Hence the output of this code is: 2017-06-18.

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

tday=datetime.date.today()
print(tday.weekday())

a) 6
b) 1
c) 0
d) 7

View Answer

Answer: a [Reason:] The code shown above prints an integer depending on which day of the week it is. Monday-0, Tuesday-1, Wednesday-2, Thursday-3, Friday-4, Saturday-5, Sunday-6. Hence the output is 6 in the case shown above.

6. What is the output of the following code if the system date is 21st June, 2017 (Wednesday)?

tday=datetime.date.today()
print(tday.isoweekday())

a) Wed
b) Wednesday
c) 2
d) 3

View Answer

Answer: d [Reason:] This code prints an integer depending on which day of the week it is. Monday-1, Tuesday-2, Wednesday-3, Thursday-4, Friday-5, Saturday-6, Sunday-7. Hence the output of the code shown above is 3.

7. Point out the error (if any) in the code shown below if the system date is 18th June, 2017?

tday=datetime.date.today()
bday=datetime.date(2017,9,18)
till_bday=bday-tday
print(till_bday)

a) 3 months, 0:00:00
b) 90 days, 0:00:00
c) 3 months 2 days, 0:00:00
d) 92 days, 0:00:00

View Answer

Answer: d [Reason:] The code shown above can be used to find the number of days between two given dates. The output of the code shown above will thus be 92.

8. The value returned when we use the function isoweekday() is ______ and that for the function weekday() is ________ if the system date is 19th June, 2017 (Monday).
a) 0,0
b) 0,1
c) 1,0
d) 1,1

View Answer

Answer: c [Reason:] The value returned when we use the function isoweekday() is 1 and that for the function weekday() is 0 if the system date is 19th June, 2017 (Monday).

9. Which of the following will throw an error if used after the code shown below?

tday=datetime.date.today()
bday=datetime.date(2017,9,18)
t_day=bday-tday

a) print(t_day.seconds)
b) print(t_day.months)
c) print(t_day.max)
d) print(t_day.resolution)

View Answer

Answer: b [Reason:] The statement: print(t_day.months) will throw an error because there is no function such as t_day.months, whereas t_day.seconds, t_day.max and t_day.resolution are valid, provided that t_day is defined.

10. What is the output of the code shown below if the system date is: 6/19/2017

tday=datetime.date.today()
tdelta=datetime.timedelta(days=10)
print(tday+tdelta)

a) 2017-16-19
b) 2017-06-9
c) 2017-06-29
d) Error

View Answer

Answer: c [Reason:] The code shown above will add the specified number of days to the current date and print the new date. On adding ten days to 6/19/2017, we get 6/29/2017. Hence the output is: 2017-06-29.

Python MCQ Set 2

1. The output of both of the print statements is the same. State whether true or false.

import datetime
dt_1 = datetime.datetime.today()
dt_2 = datetime.datetime.now()
print(dt_1)
print(dt_2)

a) True
b) False

View Answer

Answer: b [Reason:] The output of the two print statements is not the same because of the difference in time between the execution of the two print statements. There is a difference in the order of milli-seconds between the two statements and this is reflected in the output.

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

View Answer

Answer: d [Reason:] The function datetime.datetime.utcnow() can be used to find the UTC (Coordinated Universal Time), assuming that the datetime module has already been imported. The other function s shown above are invalid.

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

import time
time.time()

a) The number of hours passed since 1st January, 1970
b) The number of days passed since 1st January, 1970
c) The number of seconds passed since 1st January, 1970
d) The number of minutes passed since 1st January, 1970

View Answer

Answer: c [Reason:] The code shown above will return the number of seconds passed since 1st January, 1970.

4. What is the output of the following code, if the time module has already been imported?

def num(m):
	t1 = time.time()
	for i in range(0,m):
		print(i)
	t2 = time.time()
	print(str(t2-t1))
 
    num(3)

a) 1
2
3
The time taken for the execution of the code
b) 3
The time taken for the execution of the code
c) 1
2
3
UTC time
d) 3
UTC time

View Answer

Answer: a [Reason:] The code shown above will return the numbers 1, 2, 3, followed by the time taken in the execution of the code. Hence option (a) shows the output correctly.

5. The output of the code shown below:

import time
time.asctime()

a) Current date only
b) UTC time
c) Current date and time
d) Current time only

View Answer

Answer: c [Reason:] The function time.asctime(), present if the time module can be used to return the current date and time. It can also accept a parameter and return the date and time in a particular format. However in the above code, since we have not passed any parameters in the above code, the current date and time is returned.

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

import time
t=(2010, 9, 20, 8, 15, 12, 6)
time.asctime(t)

a) ‘20 Sep 2010 8:15:12 Sun’
b) ‘2010 20 Sept 08:15:12 Sun’
c) ‘Sun Sept 20 8:15:12 2010’
d) Error

View Answer

Answer: d [Reason:] The code shown above results in an error because this function accepts exactly 9 arguments (including day of the year and DST), but only 7 are given. Hence an error is thrown.

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

import time
t=(2010, 9, 20, 8, 45, 12, 6, 0, 0)
time.asctime(t)

a) ‘Sep 20 2010 08:45:12 Sun’
b) ‘Sun Sep 20 08:45:12 2010’
c) ’20 Sep 08:45:12 Sun 2010’
d) ‘2010 20 Sep 08:45:12 Sun’

View Answer

Answer: b [Reason:] The code shown above returns the given date and time in a particular format. Hence the output of the code shown above will be: ‘Sun Sep 20 08:45:12 2010’.

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

View Answer

Answer: a [Reason:] The sleep function (under the time module) is used to pause the code for the specified number of seconds. The number of seconds is taken as an argument by this function.

9. The output of the code shown will be:

import time
for i in range(0,5):
	print(i)
	time.sleep(2)

a) After an interval of 2 seconds, the numbers 1, 2, 3, 4, 5 are printed all together
b) After an interval of 2 seconds, the numbers 0, 1, 2, 3, 4 are printed all together
c) Prints the numbers 1, 2, 3, 4, 5 at an interval of 2 seconds between each number
d) Prints the numbers 0, 1, 2, 3, 4 at an interval of 2 seconds between each number

View Answer

Answer: d [Reason:] The output of the code shown above will be the numbers 0, 1, 2, 3, 4 at an interval of 2 seconds each.

10. The output of the code shown below is
time.struct_time(tm_year=2017, tm_mon=6, tm_mday=25, tm_hour=18, tm_min=26, tm_sec=6, tm_wday=6, tm_yday=176, tm_isdst=0)
Code:

import time
t=time.localtime()
print(t)

To extract only the year from this, we can use the function:
a) t[1]
b) tm_year
c) t[0]
d) t_year

View Answer

Answer: c [Reason:] To extract the year from the code shown above, we use the command t[0]. The command t[1] will return the month number (6 in the above case). The commands tm_year and t_year will result in errors.

11. State whether true or false.

s = time.time()
t= time.time()
s == t

a) True
b) False

View Answer

Answer: b [Reason:] The variables ‘s’ and ‘t’ will not be equal due to the slight difference in the time of their execution. Hence the output of this code will be: False.

Python MCQ Set 3

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

def mk(x):
    def mk1():
        print("Decorated")
        x()
    return mk1
def mk2():
    print("Ordinary")
p = mk(mk2)
p()

a) Decorated
Decorated
b) Ordinary
Ordinary
c) Ordinary
Decorated
d) Decorated
Ordinary

View Answer

Answer: d [Reason:] The code shown above first prints the word “Decorated” and then “ordinary”. Hence the output of this code is: Decorated Ordinary.

2. In the code shown below, which function is the decorator?

def mk(x):
    def mk1():
        print("Decorated")
        x()
    return mk1
def mk2():
    print("Ordinary")
p = mk(mk2)
p()

a) p()
b) mk()
c) mk1()
d) mk2()

View Answer

Answer: b [Reason:] In the code shown above, the function mk() is the decorator. The function which is getting decorated is mk2(). The return function is given the name p().

3. The ______ symbol along with the name of the decorator function can be placed above the definition of the function to be decorated works as an alternate way for decorating a function.
a) #
b) $
c) @
d) &

View Answer

Answer: c [Reason:] The @ symbol along with the name of the decorator function can be placed above the definition of the function to be decorated works as an alternate way for decorating a function.

4. What is the output of the code shown?

def ordi():
	print("Ordinary")
ordi
ordi()

a) Address
Ordinary
b) Error
Address
c) Ordinary
Ordinary
d) Ordinary
Address

View Answer

Answer: a [Reason:] The code shown above returns the address on the function ordi first, after which the word “Ordinary” is printed. Hence the output of this code is: Address Ordinary.

5. The two snippets of codes shown below are equivalent. State whether true or false.

CODE 1
  @f
def f1():
        print(“Hello”)
CODE 2
  def f1():
         print(“Hello”)
f1 = f(f1)

a) True
b) False

View Answer

Answer: a [Reason:] The @ symbol can be used as an alternate way to specify a function that needs to be decorated. The output of the codes shown above is the same. Hence they are equivalent. Therefore this statement is true.

6. What is the output of the following function?

def f(p, q):
	return p%q
f(0, 2)
f(2, 0)

a) 0
0
b) Zero Division Error
Zero Division Error
c) 0
Zero Division Error
d) Zero Division Error
0

View Answer

Answer: c [Reason:] The output of f(0, 2) is 0, since o%2 is equal to 0. The output of the f(2, 0) is a Zero Division Error. We can make use of decorators in order to avoid this error.

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

def f(x):
    def f1(a, b):
        print("hello")
        if b==0:
            print("NO")
            return
        return f(a, b)
    return f1
@f
def f(a, b):
    return a%b
f(4,0)

a) hello
NO
b) hello
Zero Division Error
c) NO
d) hello

View Answer

Answer: a [Reason:] In the code shown above, we have used a decorator in order to avoid the Zero Division Error. Hence the output of this code is: hello NO.

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

def f(x):
    def f1(*args, **kwargs):
        print("*"* 5)
        x(*args, **kwargs)
        print("*"* 5)
    return f1
def a(x):
    def f1(*args, **kwargs):
        print("%"* 5)
        x(*args, **kwargs)
        print("%"* 5)
    return f1
@f
@a
def p(m):
    print(m)
p("hello")

a) *****
%%%%%
hello
%%%%%
*****
b) Error
c) *****%%%%%hello%%%%%*****
d) hello

View Answer

Answer: a [Reason:] The code shown above uses multiple decorators. The output of this code is: ***** %%%%% hello %%%%% *****

9. The code shown above can work with ____ parameters.

def f(x):
    def f1(*args, **kwargs):
           print("aidlo")
           return x(*args, **kwargs)
    return f1

a) 2
b) 1
c) any number of
d) 0

View Answer

Answer: c [Reason:] The code shown above shows a general decorator which can work with any number o arguments.

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

def f(x):
    def f1(*args, **kwargs):
        print("*", 5)
        x(*args, **kwargs)
        print("*", 5)
    return f1
@f
def p(m):
    p(m)
print("hello")

a) *****
hello
b) *****
*****
hello
c) *****
d) hello

View Answer

Answer: d [Reason:] In the code shown above, we have not passed any parameter to the function p. Hence the output of this code is: hello.

11. A function with parameters cannot be decorated. State whether true or false.
a) True
b) False

View Answer

Answer: a [Reason:] Any function, irrespective of whether or not it has parameters can be decorated. Hence the statement is false.

12. Identify the decorator in the snippet of code shown below.

def sf():
     pass
sf = mk(sf)
@f
def sf():
     return

a) @f
b) f
c) sf()
d) mk

View Answer

Answer: d [Reason:] In the code shown above, @sf is not a decorator but only a decorator line. The ‘@’ symbol represents the application of a decorator. The decorator here is the function mk.

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

class A:
    @staticmethod
    def a(x):
        print(x)
A.a(100)

a) Error
b) Warning
c) 100
d) No output

View Answer

Answer: c [Reason:] The code shown above demonstrates rebinding using a static method. This can be done with or without a decorator. The output of this code will be 100.

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

def d(f):
    def n(*args):
        return '$' + str(f(*args))
    return n
@d
def p(a, t):
    return a + a*t 
print(p(100,0))

a) 100
b) $100
c) $0
d) 0

View Answer

Answer: b [Reason:] In the code shown above, the decorator helps us to prefix the dollar sign along with the value. Since the second argument is zero, the output of the code is: $100.

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

def c(f):
    def inner(*args, **kargs):
        inner.co += 1
        return f(*args, **kargs)
    inner.co = 0
    return inner
@c
def fnc():
    pass
if __name__ == '__main__':
    fnc()
    fnc()
    fnc()
    print(fnc.co)

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

View Answer

Answer: b [Reason:] The code shown above returns the number of times a given function has been called. Hence the output of this code is: 3

Python MCQ Set 4

1. Which of these about a dictionary is false?
a) The values of a dictionary can be accessed using keys
b) The keys of a dictionary can be accessed using values
c) Dictionaries aren’t ordered
d) Dictionaries are mutable

View Answer

Answer: b [Reason:] The values of a dictionary can be accessed using keys but the keys of a dictionary can’t be accessed using values.

2. Which of the following is not a declaration of the dictionary?
a) {1: ‘A’, 2: ‘B’}
b) dict([[1,”A”],[2,”B”]])
c) {1,”A”,2”B”}
d) { }

View Answer

Answer: c [Reason:] Option c is a set, not a dictionary.

3. What is the output of the following code?

a={1:"A",2:"B",3:"C"}
for i,j in a.items():
    print(i,j,end=" ")

a) 1 A 2 B 3 C
b) 1 2 3
c) A B C
d) 1:”A” 2:”B” 3:”C”

View Answer

Answer: a [Reason:] In the above code, variables i and j iterate over the keys and values of the dictionary respectively.

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

a={1:"A",2:"B",3:"C"}
print(a.get(1,4))

a) 1
b) A
c) 4
d) Invalid syntax for get method

View Answer

Answer: b [Reason:] The get() method returns the value of the key if the key is present in the dictionary and the default value(second parameter) if the key isn’t present in the dictionary.

5. What is the output of the following code?

a={1:"A",2:"B",3:"C"}
print(a.get(5,4))

a) Error, invalid syntax
b) A
c) 5
d) 4

View Answer

Answer: d [Reason:] The get() method returns the default value(second parameter) if the key isn’t present in the dictionary.

6. What is the output of the following code?

a={1:"A",2:"B",3:"C"}
print(a.setdefault(3))

a) {1: ‘A’, 2: ‘B’, 3: ‘C’}
b) C
c) {1: 3, 2: 3, 3: 3}
d) No method called setdefault() exists for dictionary

View Answer

Answer: b [Reason:] setdefault() is similar to get() but will set dict[key]=default if key is not already in the dictionary.

7. What is the output of the following code?

a={1:"A",2:"B",3:"C"}
a.setdefault(4,"D")
print(a)

a) {1: ‘A’, 2: ‘B’, 3: ‘C’, 4: ‘D’}.
b) None.
c) Error.
d) [1,3,6,10].

View Answer

Answer: a [Reason:] setdefault() will set dict[key]=default if key is not already in the dictionary.

8. What is the output of the following code?

a={1:"A",2:"B",3:"C"}
b={4:"D",5:"E"}
a.update(b)
print(a)

a) {1: ‘A’, 2: ‘B’, 3: ‘C’}
b) Method update() doesn’t exist for dictionaries
c) {1: ‘A’, 2: ‘B’, 3: ‘C’, 4: ‘D’, 5: ‘E’}
d) {4: ‘D’, 5: ‘E’}

View Answer

Answer: c [Reason:] update() method adds dictionary b’s key-value pairs to dictionary a. Execute in python shell to verify.

9. What is the output of the following code?

a={1:"A",2:"B",3:"C"}
b=a.copy()
b[2]="D"
print(a)

a) Error, copy() method doesn’t exist for dictionaries
b) {1: ‘A’, 2: ‘B’, 3: ‘C’}
c) {1: ‘A’, 2: ‘D’, 3: ‘C’}
d) “None” is printed

View Answer

Answer: b [Reason:] Changes made in the copy of the dictionary isn’t reflected in the original one.

10. What is the output of the following code?

a={1:"A",2:"B",3:"C"}
a.clear()
print(a)

a) None
b) { None:None, None:None, None:None}
c) {1:None, 2:None, 3:None}
d) { }

View Answer

Answer: d [Reason:] The clear() method clears all the key-value pairs in the dictionary.

11. Which of the following isn’t true about dictionary keys?
a) More than one key isn’t allowed
b) Keys must be immutable
c) Keys must be integers
d) When duplicate keys encountered, the last assignment wins

View Answer

Answer: c [Reason:] Keys of a dictionary may be any data type that is immutable.

12. What is the output of the following code?

a={1:5,2:3,3:4}
a.pop(3)
print(a)

a) {1: 5}
b) {1: 5, 2: 3}
c) Error, syntax error for pop() method
d) {1: 5, 3: 4}

View Answer

Answer: b [Reason:] pop() method removes the key-value pair for the key mentioned in the pop() method.

13. What is the output of the following code?

a={1:5,2:3,3:4}
print(a.pop(4,9))

a) 9
b) 3
c) Too many arguments for pop() method
d) 4

View Answer

Answer: a [Reason:] pop() method returns the value when the key is passed as an argument and otherwise returns the default value(second argument) if the key isn’t present in the dictionary.

14. What is the output of the following code?

a={1:"A",2:"B",3:"C"}
for i in a:
    print(i,end=" ")

a) 1 2 3
b) ‘A’ ‘B’ ‘C’
c) 1 ‘A’ 2 ‘B’ 3 ‘C’
d) Error, it should be: for i in a.items():

View Answer

Answer: a [Reason:] The variable i iterates over the keys of the dictionary and hence the keys are printed.

15. Execute the following in Python shell?

>>> a={1:"A",2:"B",3:"C"}
>>> a.items()

a) Syntax error
b) dict_items([(‘A’), (‘B’), (‘C’)])
c) dict_items([(1,2,3)])
d) dict_items([(1, ‘A’), (2, ‘B’), (3, ‘C’)])

View Answer

Answer: d [Reason:] The method items() returns list of tuples with each tuple having a key-value pair.

Python MCQ Set 5

1. Which of the statements about dictionary values if false?
a) More than one key can have the same value
b) The values of the dictionary can be accessed as dict[key].
c) Values of a dictionary must be unique
d) Values of a dictionary can be a mixture of letters and numbers

View Answer

Answer: c [Reason:] More than one key can have the same value.

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

>>> a={1:"A",2:"B",3:"C"}
>>> del a

a) method del doesn’t exist for the dictionary
b) del deletes the values in the dictionary
c) del deletes the entire dictionary
d) del deletes the keys in the dictionary

View Answer

Answer: c [Reason:] del deletes the entire dictionary and any further attempt to access it will throw an error.

3. If a is a dictionary with some key-value pairs, what does a.popitem() do?
a) Removes an arbitrary element
b) Removes all the key-value pairs
c) Removes the key-value pair for the key given as an argument
d) Invalid method for dictionary

View Answer

Answer: a [Reason:] The method popitem() removes a random key-value pair.

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

total={}
def insert(items):
    if items in total:
        total[items] += 1
    else:
        total[items] = 1
insert('Apple')
insert('Ball')
insert('Apple')
print (len(total))

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

View Answer

Answer: c [Reason:] The insert() function counts the number of occurrences of the item being inserted into the dictionary. There are only 2 keys present since the key ‘Apple’ is repeated. Thus, the length of the dictionary is 2.

5. What is the output of the following snippet of code?

a = {}
a[1] = 1
a['1'] = 2
a[1]=a[1]+1
count = 0
for i in a:
    count += a[i]
print(count)

a) 1
b) 2
c) 4
d) Error, the keys can’t be a mixture of letters and numbers

View Answer

Answer: c [Reason:] The above piece of code basically finds the sum of the values of keys.

6. What is the output of the following snippet of code?

numbers = {}
letters = {}
comb = {}
numbers[1] = 56
numbers[3] = 7
letters[4] = 'B'
comb['Numbers'] = numbers
comb['Letters'] = letters
print(comb)

a) Error, dictionary in a dictionary can’t exist
b) ‘Numbers’: {1: 56, 3: 7}
c) {‘Numbers’: {1: 56}, ‘Letters’: {4: ‘B’}}
d) {‘Numbers’: {1: 56, 3: 7}, ‘Letters’: {4: ‘B’}}

View Answer

Answer: d [Reason:] Dictionary in a dictionary can exist.

7. What is the output of the following snippet of code?

test = {1:'A', 2:'B', 3:'C'}
test = {}
print(len(test))

a) 0
b) None
c) 3
d) An exception is thrown

View Answer

Answer: a [Reason:] In the second line of code, the dictionary becomes an empty dictionary. Thus, length=0.

8. What is the output of the following snippet of code?

test = {1:'A', 2:'B', 3:'C'}
del test[1]
test[1] = 'D'
del test[2]
print(len(test))

a) 0
b) 2
c) Error as the key-value pair of 1:’A’ is already deleted
d) 1

View Answer

Answer: b [Reason:] After the key-value pair of 1:’A’ is deleted, the key-value pair of 1:’D’ is added.

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

a = {}
a[1] = 1
a['1'] = 2
a[1.0]=4
count = 0
for i in a:
    count += a[i]
print(count)

a) An exception is thrown
b) 3
c) 6
d) 2

View Answer

Answer: c [Reason:] The value of key 1 is 4 since 1 and 1.0 are the same. Then, the function count() gives the sum of all the values of the keys (2+4).

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

a={}
a['a']=1
a['b']=[2,3,4]
print(a)

a) Exception is thrown
b) {‘b’: [2], ‘a’: 1}
c) {‘b’: [2], ‘a’: [3]}
d) {‘b’: [2, 3, 4], ‘a’: 1}

View Answer

Answer: d [Reason:] Mutable members can be used as the values of the dictionary but they cannot be used as the keys of the dictionary.

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

>>>import collections
>>> a=collections.Counter([1,1,2,3,3,4,4,4])
>>> a

a) {1,2,3,4}
b) Counter({4, 1, 3, 2})
c) Counter({4: 3, 1: 2, 3: 2, 2: 1})
d) {4: 3, 1: 2, 3: 2, 2: 1}

View Answer

Answer: c [Reason:] The statement a=collections.OrderedDict() generates a dictionary with the number as the key and the count of times the number appears as the value.

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

>>>import collections
>>> b=collections.Counter([2,2,3,4,4,4])
>>> b.most_common(1)

a) Counter({4: 3, 2: 2, 3: 1})
b) {3:1}
c) {4:3}
d) [(4, 3)].

View Answer

Answer: d [Reason:] The most_common() method returns the n number key-value pairs where the value is the most recurring.

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

>>>import collections
>>> b=collections.Counter([2,2,3,4,4,4])
>>> b.most_common(1)

a) Counter({4: 3, 2: 2, 3: 1})
b) {3:1}
c) {4:3}
d) [(4, 3)].

View Answer

Answer: d [Reason:] The most_common() method returns the n number key-value pairs where the value is the most recurring.

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

>>> import collections
>>> a=collections.Counter([2,2,3,3,3,4])
>>> b=collections.Counter([2,2,3,4,4])
>>> a|b

a) Counter({3: 3, 2: 2, 4: 2})
b) Counter({2: 2, 3: 1, 4: 1})
c) Counter({3: 2})
d) Counter({4: 1})

View Answer

Answer: a [Reason:] a|b returns the pair of keys and the highest recurring value.

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

>>> import collections
>>> a=collections.Counter([3,3,4,5])
>>> b=collections.Counter([3,4,4,5,5,5])
>>> a&b

a) Counter({3: 12, 4: 1, 5: 1})
b) Counter({3: 1, 4: 1, 5: 1})
c) Counter({4: 2})
d) Counter({5: 1})

View Answer

Answer: b [Reason:] a&b returns the pair of keys and the lowest recurring value.

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