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