using python modules/questions.rst
changeset 261 c7f0069d698a
equal deleted inserted replaced
260:25b4e962b55e 261:c7f0069d698a
       
     1 Objective Questions
       
     2 -------------------
       
     3 
       
     4 .. A mininum of 8 questions here (along with answers)
       
     5 
       
     6 1. What will be output of the following code snippet,
       
     7    ::
       
     8 
       
     9 	from math import sqrt
       
    10         
       
    11 	def sqrt(i):
       
    12        	    return i
       
    13 	       
       
    14         print sqrt(49)
       
    15 
       
    16    a. 7.0
       
    17    #. 7
       
    18    #. 49
       
    19    #. 49.0
       
    20    #. Error
       
    21 
       
    22 Answer: 49
       
    23 
       
    24 2. What will be the output of the following code snippet,
       
    25    ::
       
    26 
       
    27         import math
       
    28         
       
    29         def sqrt(i):
       
    30             x = math.sqrt(i)
       
    31 	    if int(x) == x:
       
    32 	        return int(x)
       
    33 	    else:
       
    34 	        return x
       
    35 
       
    36         print math.sqrt(50), sqrt(50), math.sqrt(49), sqrt(49)
       
    37 
       
    38    a. 7.0710678118654755 7 7 7
       
    39    #. 7.0710678118654755 7 7.0 7
       
    40    #. 7 7 7 7
       
    41    #. 7.0710678118654755 7.0710678118654755 7.0 7
       
    42 
       
    43 Answer: 7.0710678118654755, 7.0710678118654755, 7.0, 7
       
    44 
       
    45 3. ``from math import *`` and ``import math`` does the same,
       
    46 
       
    47    a. True
       
    48    #. False
       
    49 
       
    50 Answer: False
       
    51 
       
    52 4. Which among these libraries is part of python standard library,
       
    53 
       
    54    a. Mayavi
       
    55    #. scipy
       
    56    #. matplotlib
       
    57    #. urllib2
       
    58 
       
    59 Answer: urllib2
       
    60 
       
    61 5. ``pylab.plot(x,sin(x))`` can be used in a script with ``from pylab
       
    62    import *``
       
    63 
       
    64    a. True
       
    65    #. False
       
    66 
       
    67 Answer: False
       
    68 
       
    69 6. Which among this is correct,
       
    70 
       
    71    a. from scipy import plot
       
    72    #. from numpy import plot
       
    73    #. from matplotlib import plot
       
    74    #. from pylab import plot
       
    75    #. None of the above
       
    76 
       
    77 Answer: from pylab import plot
       
    78 
       
    79 7. Functions ``xlim()`` and ``ylim()`` can be imported to the current
       
    80    name-space as,
       
    81 
       
    82    a. from pylab import xlim, ylim
       
    83    #. import pylab
       
    84    #. from scipy import xlim, ylim
       
    85    #. import scipy
       
    86 
       
    87 Answer: from pylab import xlim, ylim
       
    88 
       
    89 8. ``scipy.linspace(-5*scipy.pi, 5*scipy.pi, 500)``
       
    90 
       
    91    a. creates an array of 500 equally spaced elements from -5*scipy.pi
       
    92       to 5*scipy.pi(excluded)
       
    93    #. creates an array of 500 equally spaced elements from
       
    94       -5*scipy.pi(excluded) to 5*scipy.pi(included)
       
    95    #. creates an array of 500 equally spaced elements from -5*scipy.pi
       
    96       to 5*scipy.pi, both end points included
       
    97    #. created an array of 500 equally spaced elements from -5*scipy.pi
       
    98       to 5*scipy.pi, both end points excluded.
       
    99    #. None of the above
       
   100 
       
   101 Answer: creates an array of 500 equally spaced elements from
       
   102 	-5*scipy.pi to 5*scipy.pi, both end points included
       
   103 
       
   104 
       
   105 Larger Questions
       
   106 ----------------
       
   107 
       
   108 .. A minimum of 2 questions here (along with answers)
       
   109 
       
   110 1. Write a python script to plot a red colour tan plot between -pi to
       
   111    pi, with x limits from -pi to pi. Label the figure appropriately
       
   112    and with a legend 'tan(x)' and title 'tangent plot'. Label the axes
       
   113    x as 'x' and y as 'tan(x)'. Make sure the script can be executed as
       
   114    a python script.
       
   115 
       
   116 2. Write a python script to plot a parabola of the form y^2=4ax with a
       
   117    = 0.5(a is the directrix), plot the line in green color add the
       
   118    legend as 'y^2=4ax' and title as 'parabola'. For x from -20 to 20
       
   119    with 100 equidistant points. Make sure the script can be executed
       
   120    as a python script. [`Hint`: Use parametric equations]