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?

13-Mar-2020 16:42:58

(a) from datetime import date 

      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.
  • today=date.today() bday=date(today.year,6,30) diff=??? if diff>0: print("Birthday in %d days" % diff) else: print("Birthday in %d days" % (???))

    (a) (bday-today).days; diff+365

    (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


Ques 4: You create a simple calendar program that needs to print tomorrow's day of the week. Which code will work under all circumstances?

(a) today=date.today() 
     days=["Sun","Mon","Tue","Wed","Thu","Fri","Sat"] 
     print("Tomorrow will be "+days[(today.weekday()+1])

(b) today=date.today() 
     days=["Mon","Tue","Wed","Thu","Fri","Sat","Sun"] 
     print("Tomorrow will be "+days[(today.weekday()+1)%7])

(c) today=date.today() 
     days=["Mon","Tue","Wed","Thu","Fri","Sat","Sun"] 
     print("Tomorrow will be "+days[today.weekday()+1])

(d) today=date.today() 
     days=["Sun","Mon","Tue","Wed","Thu","Fri","Sat"] 
     print("Tomorrow will be "+days[(today.weekday()+1)/7])

Answer: Option(B)
  • 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())



Ques 6: You need to print the content of the "list.txt" file to the console. Which code can you use, assuming the file must already exist and must not be overwritten or created?

(a) f=open("read","list.txt") 
     f.print()

(b) f=open("list.txt",'w+') 
      f.read()

(c) f=open('r+',"list.txt")
      print(f.read())

(d) f=open("list.txt",'r') 
     print(f.read())

Answer: Option(D) f=open("list.txt",'r')
                                 print(f.read())


Ques 7: While _____ 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()

Answer: Option(B) path.exists(); path.isfile()


Ques 8: How would you improve the code below?

f = open("myfile.txt", "r")contents = f.read()

(a) Close the file handler using f.close() immediately after opening the file.

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

Answer: Option(D): Check that f.mode == 'r' before calling f.read() to read from the file.



Ques 9: Your program already imported the ZipFile module using 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 
        newzip: newzip.add("file.txt")

(b) newzip=ZipFile.create("archive.zip","w") 
     newzip.add("file.txt")

(c) with ZipFile("archive.zip","w") as newzip: 
        newzip.write("file.txt")

(d) ZipFile.create("archive.zip",newzip) 
     newzip.include("file.txt")

Answer: Option(C): 
  • with ZipFile("archive.zip","w") as newzip: newzip.write("file.txt")


  • Ques 10: What is the difference between shutil.copy() and shutil.copystat() functions?(a) While shutil.copy() copies the file content, shutil.copystat() copies the file metadata.

    (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

Popular posts from this blog

Python Course Quiz 1

Supervised Learning Quiz