QUIZ ON : PROGRAMMING AND PROBLEM SOLVING THROUGH PYTHON - MODULES

MODULES

00:00:00

What will be the output of the following Python function if the random module has already been imported?

random.randint(3.5,7)

Option A. Error
Option B. Any integer between 3.5 and 7, including 7
Option C. Any integer between 3.5 and 7, excluding 7
Option D. The integer closest to the mean of 3.5 and 7
True Answer A

Explanation :
The function random.randint() does not accept a decimal value as a parameter. Hence the function shown above will throw an error.

What will be the output of the following Python function if the random module has already been imported?

random.randint(3.5,7)

Question No# : 01 out of 20



What will be the output of the following Python code?

random.randrange(0,91,5)

Option A. 10
Option B. 18
Option C. 79
Option D. 95
True Answer A

Explanation :
The function shown above will generate an output which is a multiple of 5 and is between 0 and 91. The only option which satisfies these criteria is 10. Hence the only possible output of this function is 10.

What will be the output of the following Python code?

random.randrange(0,91,5)

Question No# : 02 out of 20



Which of the following is equivalent to random.randrange(3)?

Option A. range(3)
Option B. random.choice(range(0, 3))
Option C. random.shuffle(range(3))
Option D. random.select(range(3))
True Answer B

Explanation :
It returns one number from the given range.

Which of the following is equivalent to random.randrange(3)?

Question No# : 03 out of 20



Which of the following will not be returned by random.choice(“1 ,”)?

Option A. 1
Option B. (space)
Option C. ,
Option D. none of the mentioned
True Answer D

Explanation :
Any of the characters present in the string may be returned.

Which of the following will not be returned by random.choice(“1 ,”)?

Question No# : 04 out of 20



What will be the output of the following Python code if the system date is: 6/19/2017

tday=datetime.date.today()

  tdelta=datetime.timedelta(days=10) 

print(tday+tdelta)

Option A. 2017-16-19
Option B. 2017-06-9
Option C. 2017-06-29
Option D. Error
True Answer C

Explanation :
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.

What will be the output of the following Python code if the system date is: 6/19/2017

tday=datetime.date.today()

  tdelta=datetime.timedelta(days=10) 

print(tday+tdelta)

Question No# : 05 out of 20



Which of the following functions can help us to find the version of python that we are currently working on?

Option A. sys.version
Option B. sys.version()
Option C. sys.version(0)
Option D. sys.version(1)
True Answer A

Explanation :
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.

Which of the following functions can help us to find the version of python that we are currently working on?

Question No# : 06 out of 20



Which of the following is equivalent to random.randint(3, 6)?

Option A. random.choice([3, 6])
Option B. random.randrange(3, 6)
Option C. 3 + random.randrange(3)
Option D. 3 + random.randrange(4)
True Answer D

Explanation :
random.randint(3, 6) can return any one of 3, 4, 5 and 6.

Which of the following is equivalent to random.randint(3, 6)?

Question No# : 07 out of 20



What will be the output of the following Python code if the system date is 18th June, 2017 (Sunday)?

tday=datetime.date.today()

print(tday.weekday())

Option A. 6
Option B. 1
Option C. 0
Option D. 7
True Answer A

Explanation :
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.

What will be the output of the following Python code if the system date is 18th June, 2017 (Sunday)?

tday=datetime.date.today()

print(tday.weekday())

Question No# : 08 out of 20



To include the use of functions which are present in the random library, we must use the option:

Option A. import random
Option B. random.h
Option C. import.random
Option D. random.random
True Answer A

Explanation :
The command import random is used to import the random module, which enables us to use the functions which are present in the random library.

To include the use of functions which are present in the random library, we must use the option:

Question No# : 09 out of 20



The output of the function len(sys.argv) is ____________

Option A. Error
Option B. 1
Option C. 0
Option D. Junk value
True Answer B

Explanation :
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.

The output of the function len(sys.argv) is ____________

Question No# : 10 out of 20



What will be the output of the following Python code if the system date is 21st June, 2017 (Wednesday)?

tday=datetime.date.today()

print(tday.isoweekday())

Option A. Wed
Option B. Wednesday
Option C. 2
Option D. 3
True Answer D

Explanation :
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.

What will be the output of the following Python code if the system date is 21st June, 2017 (Wednesday)?

tday=datetime.date.today()

print(tday.isoweekday())

Question No# : 11 out of 20



What will be the output of the following Python code?

import datetime

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

print(d)

Option A. Error
Option B. 2017-06-18
Option C. 18-06-2017
Option D. 06-18-2017
True Answer A

Explanation :
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.

What will be the output of the following Python code?

import datetime

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

print(d)

Question No# : 12 out of 20



Which of the following functions is not defined under the sys module?

Option A. sys.platform
Option B. sys.path
Option C. sys.readline
Option D. sys.argv
True Answer C

Explanation :
The functions sys.platform, sys.path and sys.argv are defined under the sys module. The function sys.readline is not defined. However, sys.stdin.readline is defined.

Which of the following functions is not defined under the sys module?

Question No# : 13 out of 20



What will be the output of the following Python code

import sys

sys.argv

Option A. ‘ ‘
Option B. [ ]
Option C. [‘ ‘]
Option D. Error
True Answer C

Explanation :
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 [‘ ‘].

What will be the output of the following Python code

import sys

sys.argv

Question No# : 14 out of 20



What will be the output of the following Python code if the system date is 18th June, 2017 (Sunday)?

import datetime

  tday=datetime.date.today() 

print(tday)

Option A. 18-06-2017
Option B. 06-18-2017
Option C. 2017-06-18
Option D. Error
True Answer C

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

What will be the output of the following Python code if the system date is 18th June, 2017 (Sunday)?

import datetime

  tday=datetime.date.today() 

print(tday)

Question No# : 15 out of 20



What will be the output of the following Python code?

import sys

eval(sys.stdin.readline()) 

"India"

Option A. India5
Option B. India
Option C. ‘India ’
Option D. ‘India’
True Answer D

Explanation :
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’.

What will be the output of the following Python code?

import sys

eval(sys.stdin.readline()) 

"India"

Question No# : 16 out of 20



What will be the output of the following Python function (random module has already been imported)?

random.choice('sun')

Option A. sun
Option B. u
Option C. either s, u or n
Option D. error
True Answer C

Explanation :
The above function works with alphabets just as it does with numbers. The output of this expression will be either s, u or n.

What will be the output of the following Python function (random module has already been imported)?

random.choice('sun')

Question No# : 17 out of 20



Which of the following will throw an error if used after the following Python code?

tday=datetime.date.today()

  bday=datetime.date(2017,9,18) 

t_day=bday-tday

Option A. print(t_day.seconds)
Option B. print(t_day.months)
Option C. print(t_day.max)
Option D. print(t_day.resolution)
True Answer B

Explanation :
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.

Which of the following will throw an error if used after the following Python code?

tday=datetime.date.today()

  bday=datetime.date(2017,9,18) 

t_day=bday-tday

Question No# : 18 out of 20



Both the functions randint and uniform accept ____________ parameters.

Option A. 0
Option B. 1
Option C. 3
Option D. 2
True Answer C

Explanation :
Both of these functions, that is, randint and uniform are included in the random module and both of these functions accept 3 parameters. For example: random.uniform(self,a,b) where ‘a’ and ‘b’ specify the range and self is an imaginary parameter.

Both the functions randint and uniform accept ____________ parameters.

Question No# : 19 out of 20



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

Option A. August
Option B. Aug
Option C. 08
Option D. 8
True Answer D

Explanation :
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.

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

Question No# : 20 out of 20