All Weeks Python for Genomic Data Science Coursera Quiz Answers
Table of Contents
Python for Genomic Data Science Week 01 Quiz Answers
Quiz 1: Lecture 1 Quiz
Q1. Which of the following is not a good programming strategy?
- List all the steps by which the program computes the output.
- Use external modules and libraries when they are available.
- Identify the required inputs, such as data or specifications from the user.
- Do not include many details in the overall design of the program.
Q2. Which of these is not true about pseudocode?
- It does not use the rigorous syntax of a programming language.
- It is an informal way to describe how you will solve a problem.
- It lists all steps that are required in order to compute the result.
- It can be read and interpreted by the computer.
Q3. Which feature is not true about Python?
- It is an interactive language
- It is extensible
- It has a large standard library
- It is a compiled language
Python for Genomic Data Science Week 02 Quiz Answers
Quiz 1: Lecture 3 Quiz
Q1. What data type is the object below?
[1e-10,(1,2),”BGP”,[3]]
- Array
- Dictionary
- Set
- List
Q2. What is the value returned when the code below is executed:
grades = [70,80.0,90,100]
(grades[1]+grades[3])/2
- 90
- 85
- 80
- 90.0
Q3. Suppose splice_site_pairs = [‘GT-AG’,’GC-AG’,’AT-AC’]. What is splice_site_pairs[:-1] ?
- [‘GT-AG’,’GC-AG’,’AT-AC’]
- [‘GT-AG’]
- Error
- [‘GT-AG’, ‘GC-AG’]
Q4. We want to add a new element to a list variable L. The new element is stored in a variable e. What command do we use?
- L.addLast(e)
- L.append(e)
- L.addEnd(e)
- L.add(e)
Q5. Suppose t = (‘a’, ‘c’, ‘g’, ‘t’). What will be the output of the following code:
t.append( (‘A’,’C’,’G’,’T’) )
print len(t)
- 5
- 8
- 2
- Error
Q6. What is the result of the print function in the following Python 3.xx code:
dna=input(“Enter DNA sequence:”)
dna_counts={‘t’:dna.count(‘t’),’c’:dna.count(‘c’),’g’:dna.count(‘g’),’a’:dna.count(‘a’)}
nt=sorted(dna_counts.keys())
print(nt[-1])
- ‘g’
- ‘a’
- ‘t’
- ‘c’
Q7. To delete an entry with the key ‘a’ from the dictionary dna_counts={‘g’: 13, ‘c’: 3,
‘t’: 1, ‘a’: 16} what command do we use:
- del dna_counts[‘a’:16]
- dna_counts.delete(‘a’)
- del dna_counts[‘a’]
- dna_counts[-1]=”
Q8. Suppose dna is a string variable that contains only ‘a’,’c’,’g’ or ‘t’ characters. What Python code below can we use to find the frequency (max_freq) of the most frequent character in string DNA?
Answer:
dna_counts= {'t':dna.count('t'),'c':dna.count('c'),'g':dna.count('g'),'a':dna.count('a')}
max_freq = sorted(dna_counts.values())[-1]
Q9. Suppose L1 and L2 are two list variables. What does the list variable
L3 = list(set(L1)&set(L2)) contain?
- All elements common to lists L1 and L2
- All elements common between lists L1 and L2 without duplicates
- All elements in lists L1 and L2
- A list of sets formed with the elements of lists L1 and L2
Q10. How many elements are in the dictionary someData after the following code has been executed?
someData = { }
someData['cheese'] = 'dairy'
someData['Cheese'] = 'dairy'
someData['Cheese'] = 'Dairy'
someData['cheese'] = 'Dairy'
- 0
- 3
- 2
- Can’t say – an error message was issued.
Quiz 2: Lecture 4 Quiz
Q1. The following expression is true when rnatype is ‘ncRNA’ and length is at least 200, or rnatype is ‘ncRNA’ and length is 22:
(rnatype is 'ncRNA' and length>=200) or (rnatype is 'ncRNA' and length==22)
What Boolean expression below represents a negation of the above expression?
- rnatype is not ‘ncRNA’ or ( length <200 and length != 22)
- rnatype is not ‘ncRNA’ and ( length <200 or length != 22)
- (rnatype is not ‘ncRNA’ and length < 200) and (rnatype is not ‘ncRNA’ and length != 22)
- (rnatype is not ‘ncRNA’ and length < 200) or (rnatype is not ‘ncRNA’ and length != 22)
Q2. For what values of the variable fold would the following code print ‘condition B’?
if fold > 2 : print(’condition A’)
elif fold>100: print(’condition B’)
if fold> 2 or fold<2 : print('condition A')
else : print(’condition B’)
- if fold is 2
- if fold is bigger than 100 or fold is 2
- if fold is bigger than 100
- if fold is less than 2
Q3. How many times will Python execute the code inside the following while loop?
i=1
while i< 2048 :
i=2*i
- 12
- 2048
- 11
- 10
Q4. What sequence of numbers does the range(1,-23,-3) expression evaluate to?
- -23, -20, -17, -14, -11, -8, -5, -2, 1
- 1, -2, -5, -8, -11, -14, -17, -20
- -23, -20, -17, -14, -11, -8, -5, -2
- 1, -1, -3, -5, -7, -9, -11, -13, -15, -17, -19, -21
Q5. A substring in programming represents all characters from a string, between two specified indices. Given a variable string called seq, a student writes the following program that will generate all nonempty substrings of seq:
for i in range(len(seq)) : # line 1
for j in range(i) : # line 2
print(seq[j:i]) # line 3
Which of the following changes make the above program correct?
A. Program is correct as it is.
B. Change line 1 to:
for i in range(len(seq)+1) :
C. Change line 3 to:
print(seq[j:i+1])
D. Change line 2 to:
for j in range(i+1) :
- Only A
- Only C
- Only D
- Only B
Q6. While and for loops are equivalent: whatever you can do with one you can do with the other. Given the for loop written by the student in problem 5, which of the following while loops are equivalent to it:
A.
i=0
while i<len(seq) :
j=0
while(j<i) :
print(seq[j:i])
B.
i=1
while i<len(seq) :
j=1
while(j<i) :
print(seq[j:i])
j=j+1
i=i+1
C.
i=0
while i<len(seq) :
j=0
while(j<i) :
print(seq[j:i])
j+=1
i+=1
D.
i=0
while i<len(seq)+1 :
j=0
while(j<i+1) :
print(seq[j:i])
j=j+1
i=i+1
E.
i=1
while i<len(seq)+1 :
j=1
while(j<i+1) :
print(seq[j:i])
F.
i=0
while i0) :
print(seq[j:i])
j=j+1
i=i+1
- A, C, and E only
- E only
- A and B only
- C only
Q7. A student writes a program that for any two lists L1
and L2, computes a list L3 that contains only the elements that are
common between the two lists without duplicates. Which following
statement makes the following portion of code that computes L3
correct:
L3 = [] # line 1
for elem in L1: # line 2
if elem in L2: # line 3
L3.append(elem) # line 4
Add the following line (with the correct indentation) between lines 2 and 3:
if elem not in L3:
The following two lines are introduced with the correct indentation after line 2:
if elem in L3:
pass
Change line 4 to:
L3=L3+elem
Change line3 to be:
if elem in L2 and elem not in L3:
Q8. Study the following two Python code fragments:
Version 1.
d = {}
result = False
for x in mylist:
if x in d:
result=True
break
d[x] = True
Version 2.
d = {}
result = False
for x in mylist:
if not x in d:
d[x]=True
continue
result = True
Both versions should determine if there is any element that appears more than once in the list my list. If there is such an element than the variable result should be True, otherwise it should be False. For instance, if mylist=[1,2,2,3,4,5] the result variable should be True.
Which of the following statements is True for any value of the list mylist after the execution of both versions of code?
- Version 1 is not computing the result variable correctly.
- Both the result and d variables have the same value.
- The value of the result variable is the same, but the variable d is different.
- Version 2 does not compute the result variable correctly.
Q9. Study the following if statement:
if x>10 or x<-10: print('big') elif x>1000000: print('very big')
elif x<-1000000: print('very big')
else : print('small')
For what values of x will the above code print ‘very big’?
- For x > 1000000 or x < -1000000
- For x < -1000000
- For x > 1000000
- For no value
Q10. What will be the value of the variable i after the following code is executed:
i = 1
while i < 100:
if i%2 == 0 : break
i += 1
else:
i=1000
- 1
- 98
- 2
- 99
Python for Genomic Data Science Week 03 Quiz Answers
Quiz 1: Lecture 5 Quiz
Q1. A student writes several functions to swap the values of the two variables x and y, i.e. if x=1 and y=2, after calling the swap function x=2 and y=1. The different functions that the student writes are given below:
def swap1(x,y) :
x=y
y=x
return(x,y)
def swap2(x,y) :
return(y,x)
def swap3(x,y) :
z=x
x=y
y=z
return(x,y)
def swap4(x,y) :
x,y=y,x
return(x,y)
Which of the functions swap1, swap2, swap3, and swap4 is correct?
- Function swap2 only
- Functions swap1, and swap2 only
- Functions swap2, and swap4 only
- Functions swap2, swap3, and swap4 only
Q2. Consider the following two functions:
def f1(x):
if (x > 0):
x = 3*x
x = x / 2
return x
def f2(x):
if (x > 0):
x = 3*x
x = x / 2
For what values of x will f1 and f2 return the same value?
- For x< 3/2
- When x is zero or positive
- For negative values of x only
- For x > 3/2
Q3. A recursive function in programming is a function that calls itself during its execution. The following two functions are examples of recursive functions:
def function1(length):
if length > 0:
print(length)
function1(length - 1)
def function2(length):
while length > 0:
print(length)
function2(length - 1)
What can you say about the output of function1(3) and function2(3)?
- function1 produces the output: 3 2 1 and function2 runs infinitely.
- function1 produces the output:
Q4. The following recursive function takes three positive integer arguments:
def compute(n,x,y) :
if n==0 : return x
return compute(n-1,x+y,y)
What is the value returned by the compute function?
- x
- n*(x+y)
- x+n*y
- x+y
Q5. What will the returned value be for the compute function defined in Question 4 if the argument n is negative?
- x+n*y
- x
- The function will never return a value.
- x-n*y
Q6. The following functions are all intended to check whether a string representing a dna sequence contains any characters that are not ‘a’,’c’,’g’,’t’, ‘A’, ‘C’, ‘G’, or ‘T’. At least some of these functions are wrong. Which ones are correct?
def valid_dna1(dna):
for c in dna:
if c in 'acgtACGT':
return True
else:
return False
def valid_dna2(dna):
for c in dna:
if 'c' in 'acgtACGT':
return 'True'
else:
return 'False'
def valid_dna3(dna):
for c in dna:
flag = c in 'acgtACGT'
return flag
def valid_dna4(dna):
for c in dna:
if not c in 'acgtACGT':
return False
return True
- valid_dna1, and valid_dna3 only
- valid_dna4 only
- valid_dna1, valid_dna2, and valid_dna4 only
- valid_dna2 only
Q7. What is the type of variable L3 and what is its value if L1 and L2 are lists?
L3 = [i for i in set(L1) if i in L2]
- L3 is a set with elements common between the lists L2 and L3.
- L3 is a list that contains only the elements that are common between the lists (without duplicates).
- L3 is a tuple with elements that are both in L1 and L2
- L3 is a list with all the elements in L1 and L2
Q8. What will be printed after executing the following code?
>>>def f(mystring):
print(message)
print(mystring)
message="Inside function now!"
print(message)
>>>message="Outside function!"
>>>f("Test function:")
- Outside function!
- Outside function!
- Test function:
- Inside function now!
- Test function:
- then an error message
- An error message.
Q9. Which statement below is true about a function:
- its arguments always appear within brackets
- may have no parameters
- must have at least one parameter
- must always have a return statement
Q10. Which of the following function headers is correct?
A. def afunction(a1 = 1, a2):
B. def afunction(a1 = 1, a2, a3 = 3):
C. def afunction(a1 = 1, a2 = 2, a3 = 3):
- C
- A
- A,B
- None is correct.
Quiz 2: Lecture 6 Quiz
Q1. Which of the following is a correct Python program to obtain the Python version you are using?
A.
print(version)
B.
import sys
print(sys.version)
C.
print(version)
D.
import sys
print(sys.version)
- A, B, C
- B, C, D
- B
- A, B
Q2. What does the following code do?
import random
def create_dna(n, alphabet=’acgt’):
return ’’.join([random.choice(alphabet) for i in range(n)])
dna = create_dna(1000000)
- Creates a dna variable containing a string of length 1000000, and with the a,c,g,t characters.
- Creates a dna variable containing a string of length 999999, and with the a,c,g,t characters.
- Creates a dna variable containing a string of length less than 1000000, and with the a,c,g,t characters.
- Creates a dna variable containing a string of length 1000000, containing the ‘acgt’ substring repeated.
Q3. The following functions are all supposed to count how many times a certain base (represented as a character variable in Python) appears in a dna sequence (represented as a string variable in Python):
def count1(dna, base):
i = 0
for c in dna:
if c == base:
i += 1
return i
def count2(dna, base):
i = 0
for j in range(len(dna)):
if dna[j] == base:
i += 1
return i
def count3(dna, base):
match = [c == base for c in dna]
return sum(match)
def count4(dna, base):
return dna.count(base)
def count5(dna, base):
return len([i for i in range(len(dna)) if dna[i] == base])
def count6(dna,base):
return sum(c == base for c in dna)
Which of them is correct?
- count4, count5 only
- count1, count2, count3, and count4 only
- count2, count3 only
- All of them are correct.
Q4. Which of the correct functions defined in the previous exercise is the fastest?
Hint. You will need to generate a very large string to test them on, and the function clock() from the time module to time each function.
- count4
- count2
- count3
- count1
Q5. If the PYTHONPATH environment variable is set, which of the following directories are searched for modules?
A) PYTHONPATH directory
B) current directory
C) home directory
D) installation dependent default path
- B and D only
- A, B, and C
- B only
- A, B, and D
Q6. A student imports a module called dnautil in Python using the following command:
import dnautil
What does the following call to the dir function do?
dir(dnautil)
- Prints all the documentation associated with the function dir
- Lists all the attributes of the dnautil module
- Lists the gc and has_stop_codon functions
- Lists all the functions in the dnautil module
Python for Genomic Data Science Week 04 Quiz Answers
Quiz 1: Lecture 7 Quiz
Q1. A student wants to write into a file called myfile, without deleting its existing content. Which one of the following functions should he or she use?
- f = open(‘myfile’, ‘r’)
- f = open(‘myfile’, ‘a’)
- f = open(‘myfile’, ‘b’)
- f = open(‘myfile’, ‘+’)
Q2. Which of the following statements are true?
A) When you open a file for reading, if the file does not exist, an error occurs.
B) When you open a file for writing, if the file does not exist, a new file is created.
C) When you open a file for reading, if the file does not exist, the program will open an empty file.
D) When you open a file for writing, if the file exists, the existing file is overwritten with the new file.
E) When you open a file for writing, if the file does not exist, an error occurs.
- B, C, and D only
- A, B, and D only
- A and B only
- All of them are correct
Q3. Examine the following three functions that take as argument a file name and return the extension of that file. For instance, if the file name is ‘myfile.tar.gz’ the returned value of the function should be ‘gz’. If the file name has no extention, i.e. when the file name is just ‘myfile’, the function should return an empty string.
def get_extension1(filename):
return(filename.split(".")[-1])
def get_extension2(filename):
import os.path
return(os.path.splitext(filename)[1])
def get_extension3(filename):
return filename[filename.rfind('.'):][1:]
Which of the these functions are doing exactly what they are supposed to do according to the description above?
- get_extension1 only
- get_extension3 only
- get_extension2 only
- None of them.
Q4. A student is running a Python program like this:
python mergefasta.py myfile1.fa myfile2.fa
In the mergefasta.py program the following lines are present:
import sys
tocheck=sys.argv[1]
What is the value of the variable tocheck?
- ‘myfile1.fa’
- mergefasta.py
- ‘mergefasta.py’
- ‘myfile2.fa’
Q5.
A student launches the Python interpreter from his home directory. His home directory contains another directory called 'mydir', and 'mydir' contains two files called 'foo' and 'bar'. The home directory does not contain any files, only other directories. What will happen when he writes the following code at the Python prompt:>>> import os
>>> filenames = os.listdir('mydir')
>>> f= open(filenames[0])
- An error will be produced stating that the file to be opened does not exist.
- A variable f representing a file object will be created, and the first file in the directory ‘mydir’ will be opened.
- An error will be produced stating that filename is not subscriptable.
- A variable f representing a file object will be created, and the first file in the directory ‘mydir’ will be opened for reading in text mode.
Get All Course Quiz Answers of the Genomic Data Science Specialization
Introduction to Genomic Technologies Coursera Quiz Answers
Python for Genomic Data Science Coursera Quiz Answers
Algorithms for DNA Sequencing Coursera Quiz Answers
Command Line Tools for Genomic Data Science Coursera Quiz Answers
Bioconductor for Genomic Data Science Coursera Quiz Answers
Statistics for Genomic Data Science