python for data science ai & development coursera week 4 quiz answers

Practice Quiz

1. What are the most common modes used when opening a file?

  • (a)ppend, (r)edline, (w)rite
  • (s)ave, (r)ead, (w)rite
  • (a)ppend, (r)ead, (w)rite
  • (a)ppend, (c)lose, (w)rite

2. What is the data attribute that will return the title of the file?

  • File1.open()
  • File1.mode
  • File1.name
  • File1.close()

3. What is the command that tells Python to begin a new line?

  • \n
  • \q
  • \b
  • \e

4. What attribute is used to input data into a file?

  • File1.close()
  • File1.open()
  • File1.write()
  • File1.read()

5. What python object do you cast to a dataframe?

  • set
  • tuple
  • dictionary

6. How would you access the first-row and first column in the dataframe df?

  • df.ix[0,0]
  • df.ix[0,1]
  • df.ix[1,0]

7. What is the proper way to load a CSV file using pandas?

  • pandas.from_csv(‘data.csv’)
  • pandas.load_csv(‘data.csv’)
  • pandas.read_csv(‘data.csv’)
  • pandas.import_csv(‘data.csv’)

8. Use this dataframe to answer the question.

How would you select the Genre disco? Select all that apply.

  • df.iloc[6, ‘genre’]
  • df.loc[6, 5]
  • df.iloc[6, 4]
  • df.loc[‘Bee Gees’, ‘Genre’]

9. Use this dataframe to answer the question.

Which will NOT evaluate to 20.6? Select all that apply.

  • df.iloc[4,5]
  • df.iloc[6,5]
  • df.loc[4,’Music Recording Sales’]
  • df.iloc[6, ‘Music Recording Sales (millions)’]

10. Use this dataframe to answer the question.

How do we select Albums The Dark Side of the Moon to Their Greatest Hits (1971-1975)? Select all that apply.

  • df.iloc[2:5, ‘Album’]
  • df.loc[2:5, ‘Album’]
  • df.iloc[2:6, 1]
  • df.loc[2:5, 1]

11. What is the Python library used for scientific computing and is a basis for Pandas?

  • Requests
  • Tkinter
  • Numpy
  • datetime

12. What attribute is used to retrieve the number of elements in an array?

  • a.size
  • a.ndim
  • a.shape
  • a.dtype

13. How would you change the first element to "10" in this array c:array([100,1,2,3,0])?

  • c[0]=10
  • c[4]=10
  • c[2]=10
  • c[1]=10

14. What attribute is used to return the number of dimensions in an array?

  • a.ndim
  • a.shape
  • a.size
  • a.dtype

Module 4 Graded Quiz

15. What is the result of the following lines of code?

a=np.array([-1,1])
b=np.array([1,1])
np.dot(a,b)

  • array([0,2])
  • 1
  • 0

16. How do you perform matrix multiplication on the numpy arrays A and B ?

  • A+B
  • A*B
  • np.dot(A,B)

17. What values does the variable out take if the following lines of code are run?

X=np.array([[1,0,1],[2,2,2]])
out=X[0:2,2]
out

  • array([1,0])
  • array([1,2])
  • array([1,1])

18. What is the value of Z after the following code is run?

X=np.array([[1,0],[0,1]])
Y=np.array([[2,1],[1,2]])
Z=np.dot(X,Y)

  • array([[2,1],[1,2] ])
  • array([[2,0],[1,0]])
  • array([[3,1],[1,3] ])

19. Consider the following text file: Example1.txt:

This is line 1

This is line 2

This is line 3

What is the output of the following lines of code?

with open("Example1.txt","r") as File1:

file_stuff=File1.readline ()

print(file_stuff)

  • This is line 1
  • This is line 1

    This is line 2

    This is line 3

  • This is line 1

    This is line 2

20. What do the following lines of code do?

with open("Example1.txt","r") as file1:

FileContent=file1.readlines()

print(FileContent)

  • Read the file “Example1.txt”
  • Write to the file “Example1.txt”
  • Append the file “Example1.txt”

21. What do the following lines of code do?

with open("Example.txt","a") as writefile:

writefile.write("This is line A\n")
writefile.write("This is line B\n")

  • Write to the file “Example.txt”
  • Read the file “Example.txt”
  • Append the file “Example.txt”

22 What task do the following lines of code perform?

with open('Example2.txt','r') as readfile:
with open('Example3.txt','w') as writefile:
for line in readfile:
writefile.write(line)

  • Copy the text from Example2.txt to Example3.txt.
  • Print out the content of Example2.txt.
  • Check the mode of the open function for each file object.

23. Consider the dataframe df. How would you access the element in the 2nd row and 1st column?

  • df.iloc[1,0]
  • df.iloc[2,1]
  • df.iloc[0,1]

24. In the lab, you learned you can also obtain a series from a dataframe df, select the correct way to assign the column with the header Length to a pandas series to the variable x.

  • x=df[‘Length’]
  • x=df[[‘Length’]]
  • x=df.[[‘Length’]]

Leave a Reply