Python Quiz 2
Python Quiz 2
Ques 1: Given that now=datetime.now(), which call may produce different results on different computers?
(a) print(now.strftime("%d"))
(b) print(now.strftime("%Y"))
(c) print(now.strftime("%M"))
(d) print(now.strftime("%c"))
Answer: Option(D): print(now.strftime("%c")
Ques 2: Which code will you use to generate a date and time output in the following format?
now=date.now()
print(now.strftime("%B-%D-%Y %H:%M:%S"))
(b) from datetime import datetime
now=datetime.now()
print(now.strftime("%d-%b-%Y %h:%m:%S"))
(c) from datetime import datetime
now=datetime.now()
print(now.strftime("%d-%b-%Y %H:%M:%S"))
(d) from datetime import datetime
print(datetime("%d-%B-%Y %H:%m:%S"))
Answer: Option(C):
from datetime import datetime now=datetime.now() print(now.strftime("%d-%b-%Y %H:%M:%S"))
Ques 3: What should come instead of the ??? placeholders for this code to correctly print the number of days until your birthday on Jun 30? Hint: the number of days in a timedelta object can be returned using its
days
attribute. (a) (bday-today).days; diff+365today=date.today() bday=date(today.year,6,30) diff=??? if diff>0: print("Birthday in %d days" % diff) else: print("Birthday in %d days" % (???))
(b) (bday-today).days+365; diff
(c) bday-today; 365-diff
(d) bday.days-today.days; diff+365- Answer: Option(A)
(bday-today).days
;diff+365
(a) today=date.today()
(b) today=date.today()
(c) today=date.today()
(d) today=date.today()
today=date.today() days=["Mon","Tue","Wed","Thu","Fri","Sat","Sun"] print("Tomorrow will be "+days[(today.weekday()+1)%7])
Ques 5: Which call will return the same result like
date.today()
?(a) datetime(date.today())
(b) datetime.date(datetime.now())
(c) datetime.time(datetime.now)
(d) datetime.date()
Answer: Option(B): datetime.date(datetime.now())
(a) f=open("read","list.txt")
(b) f=open("list.txt",'w+')
(c) f=open('r+',"list.txt")
(d) f=open("list.txt",'r')
_____
checks whether a path exists, _____
checks whether a path is a file.(a) path.exists(); path.realpath()
(b) path.exists(); path.isfile()
(c) path.isdir(); path.isfile()
(d) path.realpath(); path.exists()
f = open("myfile.txt", "r")
contents = f.read()
(b) Test that the file exists using f.write() before trying to read from it.
(c) Check that f.mode == True before calling f.read() to read from the file.
(d) Check that f.mode == 'r' before calling f.read() to read from the file.
f.mode == 'r'
before calling f.read() to read from the file.from zipfile import ZipFile
. How can you leverage this module to create a new zip archive and add a file to it?(a) with ZipFile("archive.zip") as
(b) newzip=ZipFile.create("archive.zip","w")
(c) with ZipFile("archive.zip","w") as newzip:
(d) ZipFile.create("archive.zip",newzip)
with ZipFile("archive.zip","w") as newzip: newzip.write("file.txt")
Ques 10: What is the difference between
(a) While shutil.copy() copies the file content, shutil.copystat() copies the file metadata.shutil.copy()
andshutil.copystat()
functions?
(b) While shutil.copy() copies the textual content, shutil.copystat() copies images and videos.
(c) While shutil.copy() copies the file in text mode, shutil.copystat() copies the file in binary mode.
(d) While shutil.copy() can copy the file to the same folder, shutil.copystat() can copy the file to any folder.- Answer: Option(A): While
shutil.copy()
copies the file content,shutil.copystat()
copies the file metadata.
Comments
Post a Comment