Question

In: Computer Science

Python Questions Suppose we run the Python program my_program.py from command line like this: Python my_program.py...

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])
  1. 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

  1. 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?

    A.
    def divisible_2_3 ():
        for i in range(20):
            if i % 2 == 0 and i % 3 == 0:
                yield i
    B.
     
     
     
    def divisible_2_3 ():
        for i in range(20):
            if i % 2 == 0 and i % 3 == 0:
                return i
    C.
     
     
    def divisible_2_3 ():
        seq = []
        for i in range(20):
            if i % 2 == 0 and i % 3 == 0:
                yield seq
     
     
    D.
    def divisible_2_3 ():
        seq = []
        for i in range(20):
            if i % 2 == 0 and i % 3 == 0:
                return i

Solutions

Expert Solution

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

  1. datetime.date
  2. datetime.time
  3. datetime.datetime
  4. datetime.timedelta

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


Related Solutions

Python programming: Instructions: The python program should respond to user input on a command line Below...
Python programming: Instructions: The python program should respond to user input on a command line Below are the four options - if the user input is A, the program will quit -if the user input is B, the program will display the number of times the prompt has been displayed -if the user input is C, will display whatever is stored. -if the user inputs something else, it will store that user input as a string and append it to...
Compile and run the following code then answer the following questions: a.) Run this command from...
Compile and run the following code then answer the following questions: a.) Run this command from the shell prompt: ./a.out ls -F Explain, in your own words, why you see the screen output you do and how that output relates to both the content of the program AND the nature of the shell command used to invoke it. Be sure to comment on the pointer arithmetic done inside the line: execvp(*(argv+1), argv+1); b.) Run this command from the shell prompt:...
Python Write a program that takes a text filename as command line argument, and prints number...
Python Write a program that takes a text filename as command line argument, and prints number of times each letter occurred in this file.
IN PYTHON: Compose a recursive program to draw Sierpinski triangles. Use a command-line argument to control...
IN PYTHON: Compose a recursive program to draw Sierpinski triangles. Use a command-line argument to control the depth of the recursion.
Here we will be taking data in as a file instead of from the command line...
Here we will be taking data in as a file instead of from the command line (no cin). Note that the sample file provided here is not the file that I will use to grade your assignment but the formatting will be the same. File input is very similar to how data comes in from the user through cin (using the same operators) but we will need to setup the stream ourselves. First we need to include <fstream> at the...
Here we will be taking data in as a file instead of from the command line...
Here we will be taking data in as a file instead of from the command line (no cin). Note that the sample file provided here is not the file that I will use to grade your assignment but the formatting will be the same. File input is very similar to how data comes in from the user through cin (using the same operators) but we will need to setup the stream ourselves. First we need to include <fstream> at the...
Create a python program that will: prompt a user for a command Command get_data Level 1:...
Create a python program that will: prompt a user for a command Command get_data Level 1: Take one of the commands my_max my_min my_range my_sum mean median mode fib factorize prime Requirements: Your commands should be case-insensitive You should use python lists to store data You should NOT use built-in python math functions, or math libraries to compute these values Tips: Write one function that will convert a string with comma-separated numbers into a python list with the numbers. You...
Modify program so that input comes in as command line arguments. Sample run: ./a.out W8 4...
Modify program so that input comes in as command line arguments. Sample run: ./a.out W8 4 ME 2 finish 2! Output: Consonants: WMfnsh 1) Name your program command_consonants.c. 2) If no command line arguments was provided, your program should print a usage message “Usage: ./a.out input #include<stdio.h> int main() { printf("Input: "); int i = 0; char ch; // array to store consonants char consonant[100]={""}; //do loop to take input do { ch = getchar(); //checks to see if consonants...
Write a program that takes an integer N from the command line and uses StdRandom.uniform() to...
Write a program that takes an integer N from the command line and uses StdRandom.uniform() to generate a random sequence of integers be- tween 0 and N – 1. Run experiments to validate the hypothesis that the number of integers generated before the first repeated value is found is ~√?N/2.
Use C++ to write a program that reads in a binary string from the command line...
Use C++ to write a program that reads in a binary string from the command line and applies the following (00, 1101) tag-system: if the first bit is 0, deletes the first three bits and append 00; if the first bit is 1, delete the first three bits and append 1101. Repeat as long as the string has at least 3 bits. Try to determine whether the following inputs will halt or go into an infinite loop: 10010, 100100100100100100. Use...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT