getting-started-with-arrays/questions.rst
changeset 324 4054b1a6392d
parent 261 c7f0069d698a
equal deleted inserted replaced
323:e675f9208b91 324:4054b1a6392d
       
     1 Objective Questions
       
     2 -------------------
       
     3 
       
     4 .. A mininum of 8 questions here (along with answers)
       
     5 
       
     6 1. An array in Python is the same as a Python list
       
     7 
       
     8    a. True
       
     9    #. False
       
    10 
       
    11 Answer: False
       
    12 
       
    13 2. ``x = array([1, 2, 3], [5, 6, 7])`` is a valid statement
       
    14 
       
    15    a. True
       
    16    #. False
       
    17 
       
    18 Answer: False
       
    19 
       
    20 3. What will be the output of the following code,
       
    21    ::
       
    22 
       
    23       x = array([[1, 2, 3], ['a', 2, 'c']])
       
    24       print x[0][0] + x[0][1] + x[0][2] 
       
    25 
       
    26    a. 6
       
    27    #. 123
       
    28    #. a2c
       
    29    #. Error as array takes only homogeneous elements
       
    30 
       
    31 Answer: 123
       
    32       
       
    33 4. What will be the output of the following code,
       
    34    ::
       
    35 
       
    36 	x = [[1, 2, 3], [4.1, 4.2, 4.3], ['6','7',8]]
       
    37 	y = array(x)
       
    38 	print y[-1][-2] + y[-1][-1] + y[-2][0] + y[0][-2]
       
    39 
       
    40    a. 21.1
       
    41    #. 12.5
       
    42    #. 784.12
       
    43    #. Error as array takes only homogeneous elements
       
    44 
       
    45    .. 4.2 4.3 2 2
       
    46 
       
    47 Answer: 784.12
       
    48 
       
    49 5. What is the output of the following code,
       
    50    ::
       
    51 
       
    52       x = array([[1, 2, 3], ['a', 2, 'c']])
       
    53       identity(x.shape)
       
    54 
       
    55    a. Will create an identity matrix of shape (2, 3).
       
    56    #. ``identity()`` function takes an integer as argument and a tuple
       
    57       is passed.
       
    58    #. Will return, array([[1,0,1],[0,1,0]])
       
    59    #. Will return, array([[0,1,0],[0,1,0]])
       
    60 
       
    61 Answer: ``identity()`` function takes an integer as argument and a
       
    62       	tuple is passed.
       
    63 
       
    64 6. ``ones_like()`` function?
       
    65    
       
    66    (A) Returns an array of ones with the same shape and type as a
       
    67        given array.
       
    68    (B) Return a new array of given shape and type, filled with ones.
       
    69 
       
    70    Read the statements and answer,
       
    71 
       
    72    a. Only statement A is correct.
       
    73    #. Only statement B is correct.
       
    74    #. Both statement A and B are correct.
       
    75    #. Both statement A and B are incorrect.
       
    76 
       
    77 Answer: Only statement A is correct.
       
    78 
       
    79 7. ``zeros_like()`` function?
       
    80 
       
    81    (A) Return a new array of given shape and type, filled with zeros.
       
    82    (B) Returns an array of zeros with the same shape and type as a
       
    83        given array.
       
    84 
       
    85 
       
    86    Read the statements and answer,
       
    87 
       
    88    a. Only statement A is correct.
       
    89    #. Only statement B is correct.
       
    90    #. Both statement A and B are correct.
       
    91    #. Both statement A and B are incorrect.
       
    92 
       
    93 Answer: Only statement B is correct.
       
    94 
       
    95 8. What will be output of the following code snippet.
       
    96    ::
       
    97 
       
    98       x = linspace(1,10,10).reshape(5,2)
       
    99       print (x[-3]+x[-4]).sum()
       
   100 
       
   101    a. 10.0 
       
   102    #. 18.0
       
   103    #. 14.0
       
   104    #. 16.44
       
   105    #. Error
       
   106 
       
   107 Answer: 18
       
   108 
       
   109 Larger Questions
       
   110 ----------------
       
   111 
       
   112 .. A minimum of 2 questions here (along with answers)
       
   113 
       
   114 1. Write a python script to create a 15x15 array of equally spaced 225
       
   115    elements from 1 to 1000, add 5 to each of the diagonal elements and
       
   116    find the sum of all odd rows of the array. Say for example the
       
   117    array,
       
   118    ::
       
   119 
       
   120        x = array([[1, 2, 3],
       
   121 		  [4, 5, 6],
       
   122 		  [7, 8, 9]])
       
   123 
       
   124    will give answer 40 ((1+5) + 2 + 3 + 7 + 8 + (9+5)).
       
   125 
       
   126 2. For any given array([a1, a2, a3, .. , an]) the Vandermonde matrix
       
   127     will be [[1, a1, a1**2, .. , a1**(n-1)], [1, a2, a2**2, .. ,
       
   128     a2**(n-1)], .. , [1, an, an**2, .. ,an**(n-1)]]. Write a python
       
   129     script to generate the Vandermonde matrix and find the determinant
       
   130     of the matrix for [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
       
   131     16, 17, 18, 19, 20].  [Hint: to find the determinant use the
       
   132     function ``det()`` from ``linalg`` module.]