Test Series - python

Test Number 82/108

Q: The output of both of the print statements is the same.

import datetime
dt_1 = datetime.datetime.today()
dt_2 = datetime.datetime.now()
print(dt_1)
print(dt_2)
A. True
B. False
C. ..
D. ..
Solution: The output of the two print statements is not the same because of the difference in time between the execution of the two print statements. There is a difference in the order of milliseconds between the two statements and this is reflected in the output.
Q: Which of the following functions can be used to find the coordinated universal time, assuming that the datetime module has already been imported?
A. datetime.utc()
B. datetime.datetime.utc()
C. datetime.utcnow()
D. datetime.datetime.utcnow()
Solution: 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.
Q:  What will be the output of the following Python code?

import time
time.time()
A. The number of hours passed since 1st January, 1970
B. The number of days passed since 1st January, 1970
C. The number of seconds passed since 1st January, 1970
D. The number of minutes passed since 1st January, 1970
Solution: The code shown above will return the number of seconds passed since 1st January, 1970.
Q: What will be the output of the following Python code, if the time module has already been imported?

def num(m):
	t1 = time.time()
	for i in range(0,m):
		print(i)
	t2 = time.time()
	print(str(t2-t1))
 
    num(3)
A. 1 2 3 The time taken for the execution of the code
B.  3 The time taken for the execution of the code
C. 1 2 3 UTC time
D.  3 UTC time
Solution: The code shown above will return the numbers 1, 2, 3, followed by the time taken in the execution of the code.
Output:
1
2
3
The time taken for the execution of the code
Q: What will be the output of the following Python code?

import time
time.asctime()
A. Current date only
B. UTC time
C. Current date and time
D. Current time only
Solution: 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.
Q: What will be the output of the following Python code?

import time
t=(2010, 9, 20, 8, 15, 12, 6)
time.asctime(t)
A. ‘20 Sep 2010 8:15:12 Sun’
B. ‘2010 20 Sept 08:15:12 Sun’
C. ‘Sun Sept 20 8:15:12 2010’
D. Error
Solution: 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.
Q: 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)
A. ‘Sep 20 2010 08:45:12 Sun’
B. ‘Sun Sep 20 08:45:12 2010’
C. ’20 Sep 08:45:12 Sun 2010’
D. ‘2010 20 Sep 08:45:12 Sun’
Solution: 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’.
Q: The sleep function (under the time module) is used to ___________
A. Pause the code for the specified number of seconds
B. Return the specified number of seconds, in terms of milliseconds
C. Stop the execution of the code
D. Return the output of the code had it been executed earlier by the specified number of seconds
Solution: The sleep function (under the time module) is used to pause the code for the specified number of seconds. The number of seconds is taken as an argument by this function.
Q: What will be the output of the following Python code?

import time
for i in range(0,5):
	print(i)
	time.sleep(2)
A. After an interval of 2 seconds, the numbers 1, 2, 3, 4, 5 are printed all together
B. After an interval of 2 seconds, the numbers 0, 1, 2, 3, 4 are printed all together
C. Prints the numbers 1, 2, 3, 4, 5 at an interval of 2 seconds between each number
D. Prints the numbers 0, 1, 2, 3, 4 at an interval of 2 seconds between each number
Solution: The output of the code shown above will be the numbers 0, 1, 2, 3, 4 at an interval of 2 seconds each.
Q: 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)
A. t[1]
B. tm_year
C. t[0]
D. t_year
Solution: 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.
Q: What does os.name contain?
A. the name of the operating system dependent module imported
B. the address of the module os
C. error, it should’ve been os.name()
D. none of the mentioned
Solution: It contains the name of the operating system dependent module imported such as ‘posix’, ‘java’ etc.
Q: What does print(os.geteuid()) print?
A. the group id of the current process
B. the user id of the current process
C. both the group id and the user of the current process
D. none of the mentioned
Solution: os.geteuid() gives the user id while the os.getegid() gives the group id.
Q: What does os.getlogin() return?
A. name of the current user logged in
B. name of the superuser
C. gets a form to login as a different user
D. all of the mentioned
Solution: It returns the name of the user who is currently logged in and is running the script.
Q:  What does os.close(f) do?
A. terminate the process f
B. terminate the process f if f is not responding
C. close the file descriptor f
D. return an integer telling how close the file pointer is to the end of file
Solution: When a file descriptor is passed as an argument to os.close() it will be closed.
Q: What does os.fchmod(fd, mode) do?
A. change permission bits of the file
B. change permission bits of the directory
C. change permission bits of either the file or the directory
D. none of the mentioned
Solution: The arguments to the function are a file descriptor and the new mode.
Q: Which of the following functions can be used to read data from a file using a file descriptor?
A. os.reader()
B. os.read()
C. os.quick_read()
D. os.scan()
Solution: None of the other functions exist.
Q: Which of the following returns a string that represents the present working directory?
A. os.getcwd()
B. os.cwd()
C. os.getpwd()
D. os.pwd()
Solution: The function getcwd() (get current working directory) returns a string that represents the present working directory.
Q: What does os.link() do?
A. create a symbolic link
B. create a hard link
C. create a soft link
D. none of the mentioned
Solution: os.link(source, destination) will create a hard link from source to destination.
Q: Which of the following can be used to create a directory?
A. os.mkdir()
B. os.creat_dir()
C. os.create_dir()
D. os.make_dir()
Solution: The function mkdir() creates a directory in the path specified.
Q: Which of the following can be used to create a symbolic link?
A. os.symlink()
B. os.symb_link()
C. os.symblin()
D. os.ln()
Solution: It is the function that allows you to create a symbolic link.

You Have Score    /20