QUIZ ON : PROGRAMMING AND PROBLEM SOLVING THROUGH PYTHON - MODULES IN PYTHON

MODULES IN PYTHON

00:00:00

What will be the output of the following Python code?

import time

  t=(2010, 9, 20, 8, 15, 12, 6) 

time.asctime(t)

Option A. ‘20 Sep 2010 8:15:12 Sun’
Option B. ‘2010 20 Sept 08:15:12 Sun’
Option C. ‘Sun Sept 20 8:15:12 2010’
Option D. Error
True Answer D

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

What will be the output of the following Python code?

import time

  t=(2010, 9, 20, 8, 15, 12, 6) 

time.asctime(t)

Question No# : 01 out of 20



What will be the output if we try to extract only the year from the following Python code? (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))

import time

  t=time.localtime() 

print(t)

Option A. t[1]
Option B. tm_year
Option C. t[0]
Option D. t_year
True Answer C

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

What will be the output if we try to extract only the year from the following Python code? (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))

import time

  t=time.localtime() 

print(t)

Question No# : 02 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# : 03 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# : 04 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# : 05 out of 20



Which of the following will never be displayed on executing print(random.choice({0: 1, 2: 3}))?

Option A. 0
Option B. 1
Option C. KeyError: 1
Option D. none of the mentioned
True Answer A

Explanation :
It will not print 0 but dict[0] i.e. 1 may be printed.

Which of the following will never be displayed on executing print(random.choice({0: 1, 2: 3}))?

Question No# : 06 out of 20



What will be the output of the following Python code?

import time

  t=(2010, 9, 20, 8, 45, 12, 6, 0, 0) 

time.asctime(t)

Option A. ‘Sep 20 2010 08:45:12 Sun’
Option B. ‘Sun Sep 20 08:45:12 2010’
Option C. ’20 Sep 08:45:12 Sun 2010’
Option D. ‘2010 20 Sep 08:45:12 Sun’
True Answer B

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

What will be the output of the following Python code?

import time

  t=(2010, 9, 20, 8, 45, 12, 6, 0, 0) 

time.asctime(t)

Question No# : 07 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# : 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



What will be the output of the following Python code?

random.seed(3)  random.randint(1,5)  2  random.seed(3)  random.randint(1,5)

Option A. 3
Option B. 2
Option C. Any integer between 1 and 5, including 1 and 5
Option D. Any integer between 1 and 5, excluding 1 and 5
True Answer B

Explanation :
We use the seed function when we want to use the same random number once again in our program. Hence the output of the code shown above will be 2, since 2 was generated previously following which we used the seed function.

What will be the output of the following Python code?

random.seed(3)  random.randint(1,5)  2  random.seed(3)  random.randint(1,5)

Question No# : 10 out of 20



What will be the output of the following Python code?

import time

time.time()

Option A. The number of hours passed since 1st January, 1970
Option B. The number of days passed since 1st January, 1970
Option C. The number of seconds passed since 1st January, 1970
Option D. The number of minutes passed since 1st January, 1970
True Answer C

Explanation :
The code shown above will return the number of seconds passed since 1st January, 1970.

What will be the output of the following Python code?

import time

time.time()

Question No# : 11 out of 20



Which of the following functions helps us to randomize the items of a list?

Option A. seed
Option B. randomise
Option C. shuffle
Option D. uniform
True Answer C

Explanation :
The function shuffle, which is included in the random module, helps us to randomize the items of a list. This function takes the list as a parameter.

Which of the following functions helps us to randomize the items of a list?

Question No# : 12 out of 20



What will be the output of the following Python code?

import random

random.choice([10.4, 56.99, 76])

Option A. Error
Option B. Either 10.4, 56.99 or 76
Option C. Any number other than 10.4, 56.99 and 76
Option D. 56.99 only
True Answer B

Explanation :
The function random.choice(a,b,c,d) returns a random number which is selected from a, b, c and d. The output can be either a, b, c or d. Hence the output of the snippet of code shown above can be either 10.4, 56.99 or 76.

What will be the output of the following Python code?

import random

random.choice([10.4, 56.99, 76])

Question No# : 13 out of 20



Which of the following functions can be used to find the coordinated universal time, assuming that the datetime module has already been imported?

Option A. datetime.utc()
Option B. datetime.datetime.utc()
Option C. datetime.utcnow()
Option D. datetime.datetime.utcnow()
True Answer D

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

Which of the following functions can be used to find the coordinated universal time, assuming that the datetime module has already been imported?

Question No# : 14 out of 20



The function random.randint(4) can return only one of the following values. Which?

Option A. 4
Option B. 3.4
Option C. error
Option D. 5
True Answer C

Explanation :
Error, the function takes two arguments.

The function random.randint(4) can return only one of the following values. Which?

Question No# : 15 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# : 16 out of 20



What will be the output of the following Python code?

import sys

sys.stdin.readline() 

Sanfoundry

Option A. ‘Sanfoundry ’
Option B. ‘Sanfoundry’
Option C. ‘Sanfoundry10’
Option D. Error
True Answer A

Explanation :
The function shown above works just like raw_input. Hence it automatically adds a ‘ ’ character to the input string. Therefore, the output of the function shown above will be: Sanfoundry .

What will be the output of the following Python code?

import sys

sys.stdin.readline() 

Sanfoundry

Question No# : 17 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# : 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?

import time

time.asctime()

Option A. Current date only
Option B. UTC time
Option C. Current date and time
Option D. Current time only
True Answer C

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

What will be the output of the following Python code?

import time

time.asctime()

Question No# : 20 out of 20