In: Computer Science
Python Questions
Suppose we run the Python program my_program.py from command line like this:
Python my_program.py 14 CSC121
Which of the following statement in my_program.py will display CSC121?
A. |
import sys print(sys.argv) |
|
B. |
import sys print(sys.argv[0]) |
|
C. |
import sys print(sys.argv[1]) |
|
D. |
import sys print(sys.argv[2]) |
from datetime import datetime dob = datetime(2015, 1, 1)
Which of the following statements will change year to 2018?
A. |
dob.year=2018 |
|
B. |
dob.replace(year=2018) |
|
C. |
dob = dob.replace(year=2018) |
|
D. |
dob.set_year(2018) |
10 points
QUESTION 4
from datetime import datetime dob = datetime(2011,12,10) today = datetime.now() age = today - dob
What is the type of age?
A. |
int |
|||||||||||||
B. |
float |
|||||||||||||
C. |
datetime |
|||||||||||||
D. |
timedelta Which of the following functions returns integers between 0 and 20 that are divisible by both 2 and 3?
|
Answer 1: The correct answer is D
import sys print(sys.argv[2])
argv[] list is the list of arguments that are supplied to the Python executable command while executing any python code. The list index starts from 0, that means if someone types "python my_program.py 14 CSC121" to run my_program.py, argv[] list will contain my_program.py, 14 and CSC121 as 3 items and can be accessed by sys.argv[0], sys.argv[1] and sys.argv[2] respectively.
Option A will print the entire argv list while Option B will print my_program.py and Option C will print 14
------------------------------------
Answer 2: The correct answer is C.
dob = dob.replace(year=2018)
from datetime import datetime dob = datetime(2015, 1, 1)
Here dob is an object of datetime class. It gives an inbuilt method .replace to change the time. Not only the year, but months, hours, seconds, etc can also be changed. Once the method is used, the resultant object has to be assigned back to dob to get the changes reflected.
Option A and D use .year attribute and .set_year() method for dob object, which are invalid. Option B makes use of .replace but it doesn't assign the resultant object back to dob, hence it's not serving the purpose either.
------------------------------------
Answer 3: The correct answer is D.
timedelta
from datetime import datetime dob = datetime(2011,12,10) today = datetime.now() age = today - dob
datetime has 4 classes (actually 5, but 5th tzinfo class is not of our interest right now).
timedelta shows the difference between any two objects of one of the first three classes. In our question, age is the difference of two datetime objects, hence it's an object of datetime.timedelta class.
------------------------------------
Answer 4: The correct answer is A.
def divisible_2_3 (): for i in range(20): if i % 2 == 0 and i % 3 == 0: yield i
Let's see how. Notice that the function is not
returning but
yielding i.
yield in python is used when we won't be loading everything in the
memory, and the item being returned is generated on the fly. So the
generator is suspended until the
next call to the function.
return i means, return one value from the
function and quit.
We can get the output my just adding the following line in
main:
for item in divisible_2_3():
print item
Option B: returns only 1 value and the function exits.
Option C: It yields seq[] which is never updated in the loop, so
it's worthless.
Option D: Again it returns only 1 value and the function quits.
initializing seq[] has no effect as it's not being updated
anywhere