Quiz

Block 1, Week 3

Download Quiz Answer Sheet

The code in each one of the questions below has an error in it. What error message is produced by each code snippet? Assume the numpy and matplotlib.pyplot modules have been imported as:

import numpy as np
import matplotlib.pyplot as plt

Question 1

y = np.sin('np.pi')
  1. TypeError
  2. ZeroDivisionError
  3. SyntaxError
  4. NameError
  5. ImportError
  6. AttributeError
  7. IndexError

Question 2

x = np.array([1.2,3.4,6.1])
print(x[4])
  1. TypeError
  2. ZeroDivisionError
  3. SyntaxError
  4. NameError
  5. ImportError
  6. AttributeError
  7. IndexError

Question 3

x = np.array([1.2,3.4,6.1)
print(x[2])
  1. TypeError
  2. ZeroDivisionError
  3. SyntaxError
  4. NameError
  5. ImportError
  6. AttributeError
  7. IndexError

Question 4

x = 1
y = 0
z = x/y
  1. TypeError
  2. ZeroDivisionError
  3. SyntaxError
  4. NameError
  5. ImportError
  6. AttributeError
  7. IndexError

Question 5

from numpy import np
  1. TypeError
  2. ZeroDivisionError
  3. SyntaxError
  4. NameError
  5. ImportError
  6. AttributeError
  7. IndexError

Question 6

x = np.linspace(0,10*np.pi,1000)
y = np.snc(x)
  1. TypeError
  2. ZeroDivisionError
  3. SyntaxError
  4. NameError
  5. ImportError
  6. AttributeError
  7. IndexError

Question 7

Suppose you run the program junk.py from Spyder by clicking the green arrow and you get the following error message:

runfile('/Users/sburns/junk.py', wdir='/Users/sburns')
Traceback (most recent call last):

  File "<ipython-input-53-cb18af20b767>", line 1, in <module>
    runfile('/Users/sburns/junk.py', wdir='/Users/sburns')

  File "/Users/sburns/anaconda3/lib/python3.6/site-packages/spyder_kernels/customize/spydercustomize.py", line 668, in runfile
    execfile(filename, namespace)

  File "/Users/sburns/anaconda3/lib/python3.6/site-packages/spyder_kernels/customize/spydercustomize.py", line 108, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "/Users/sburns/junk.py", line 17, in <module>
    plotit(x,y)

  File "/Users/sburns/junk.py", line 13, in plotit
    plt.plot(x,y)

  File "/Users/sburns/anaconda3/lib/python3.6/site-packages/matplotlib/pyplot.py", line 3363, in plot
    ret = ax.plot(*args, **kwargs)

  File "/Users/sburns/anaconda3/lib/python3.6/site-packages/matplotlib/__init__.py", line 1867, in inner
    return func(ax, *args, **kwargs)

  File "/Users/sburns/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_axes.py", line 1528, in plot
    for line in self._get_lines(*args, **kwargs):

  File "/Users/sburns/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_base.py", line 406, in _grab_next_args
    for seg in self._plot_args(this, kwargs):

  File "/Users/sburns/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_base.py", line 383, in _plot_args
    x, y = self._xy_from_xy(x, y)

  File "/Users/sburns/anaconda3/lib/python3.6/site-packages/matplotlib/axes/_base.py", line 242, in _xy_from_xy
    "have shapes {} and {}".format(x.shape, y.shape))

ValueError: x and y must have same first dimension, but have shapes (1000,) and (1,)

On what line number in your script are you likely to find your error?

  1. line 1
  2. line 17
  3. line 13
  4. line 406
  5. line 383
  6. line 242

Question 8

Will the following code generate an error? (If you're not sure, try it.)

import numpy as np
a = [1,2,3]
b = np.array([4,5,6])
c = [4,5,6]

d = a + b
e = a + c

x = d - e
print(x)
  1. Will not generate an error.
  2. Will generate a TypeError
  3. Will generate a ValueError
  4. Will generate a SyntaxError
  5. Will generate an AttributeError

Question 9

What will happen if you try to run the code below? (Try it if you're not sure.)

def isInfinity(x,y):
    try:
        x/y
    except ZeroDivisionError:
        return True
    return False

x = 1
y = 0

myValue = isInfinity(x,y)
  1. The code generates a ZeroDivisionError
  2. The code will run and myValue will be True.
  3. The code will run and myValue will be False.

Question 10

What will happen if you try to run the code below? (Try it if you're not sure.)

def isInfinity(x,y):
    try:
        x/y
    except ZeroDivisionError:
        return True
    return False

x = 1
y = 1

myValue = isInfinity(x,y)
  1. The code generates an AttributeError
  2. The code will run and myValue will be True.
  3. The code will run and myValue will be False.