Test Series - python

Test Number 81/108

Q: What will be the output of the following Python code?

import datetime
d=datetime.date(2016,7,24)
print(d)
A. Error
B. 2017-07-24
C. 2017-7-24
D. 24-7-2017
Solution: 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.
Q: What will be the output of the following Python code?

import datetime
d=datetime.date(2017,06,18)
print(d)
A. Error
B. 2017-06-18
C. 18-06-2017
D. 06-18-2017
Solution: 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.
Q: 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())
A. August
B. Aug
C. 08
D. 8
Solution: 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.
Q: 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)
A. 18-06-2017
B. 06-18-2017
C. 2017-06-18
D. Error
Solution: The code shown above prints the system date in the format yyyy-mm-dd. Hence the output of this code is: 2017-06-18.
Q: 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())
A. 6
B. 1
C. 0
D. 7
Solution: 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.
Q: 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())
A. Wed
B. Wednesday
C. 2
D. 3
Solution: 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.
Q: 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
Solution: 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.
Q: 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
Solution: 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).
Q: 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
A. print(t_day.seconds)
B. print(t_day.months)
C. print(t_day.max)
D. print(t_day.resolution)
Solution: 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.
Q: 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)
A. 2017-16-19
B. 2017-06-9
C. 2017-06-29
D. Error
Solution: 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.
Q: Which of the following functions can help us to find the version of python that we are currently working on?
A. sys.version
B. sys.version()
C. sys.version(0)
D. sys.version(1)
Solution: 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.
Q: Which of the following functions is not defined under the sys module?
A. sys.platform
B. sys.path
C. sys.readline
D. sys.argv
Solution: 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.
Q: The output of the functions len(“abc”) and sys.getsizeof(“abc”) will be the same.
A. True
B. False
C. none
D. ...
Solution: The function len returns the length of the string passed, and hence it’s output will be 3. The function getsizeof, present under the sys module returns the size of the object passed. It’s output will be a value much larger than 3. Hence the above statement is false.
Q: What will be the output of the following Python code, if the sys module has already been imported?

sys.stdout.write("hello world")
A. helloworld
B. hello world10
C. hello world11
D. error
Solution: The function shown above prints the given string along with the length of the string. Hence the output of the function shown above will be hello world11.
Q: What will be the output of the following Python code?

import sys
sys.stdin.readline()
Sanfoundry
A. ‘Sanfoundry ’
B. ‘Sanfoundry’
C. ‘Sanfoundry10’
D. Error
Solution: 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
.
Q: What will be the output of the following Python code?

import sys
eval(sys.stdin.readline())
"India"
A. India5
B. India
C. ‘India ’
D. ‘India’
Solution: 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’.
Q: What will be the output of the following Python code?

import sys
eval(sys.stdin.readline())
Computer
A. Error
B. ‘Computer ’
C. Computer8
D. Computer
Solution: The code shown above will result in an error. This is because this particular function accepts only strings enclosed in single or double inverted quotes, or numbers. Since the string entered above is not enclosed in single or double inverted quotes, an error will be thrown.
Q: What will be the output of the following Python code?

import sys
sys.argv[0]
A. Junk value
B. ‘ ‘
C. No output
D. Error
Solution: The output of the function shown above will be a blank space enclosed in single quotes. Hence the output of the code shown above is ‘ ‘.
Q: What will be the output of the following Python code?

import sys
sys.stderr.write(“hello”)
A. ‘hello’
B. ‘hello ’
C. hello
D. hello5
Solution: The code shown above returns the string, followed by the length of the string. Hence the output of the code shown above is hello5.

You Have Score    /19