getting-started-with-arrays/questions.rst
author amit
Wed, 27 Oct 2010 15:13:17 +0530
changeset 362 a77a27916f81
parent 261 c7f0069d698a
permissions -rw-r--r--
Added Objectives and other metadata

Objective Questions
-------------------

.. A mininum of 8 questions here (along with answers)

1. An array in Python is the same as a Python list

   a. True
   #. False

Answer: False

2. ``x = array([1, 2, 3], [5, 6, 7])`` is a valid statement

   a. True
   #. False

Answer: False

3. What will be the output of the following code,
   ::

      x = array([[1, 2, 3], ['a', 2, 'c']])
      print x[0][0] + x[0][1] + x[0][2] 

   a. 6
   #. 123
   #. a2c
   #. Error as array takes only homogeneous elements

Answer: 123
      
4. What will be the output of the following code,
   ::

	x = [[1, 2, 3], [4.1, 4.2, 4.3], ['6','7',8]]
	y = array(x)
	print y[-1][-2] + y[-1][-1] + y[-2][0] + y[0][-2]

   a. 21.1
   #. 12.5
   #. 784.12
   #. Error as array takes only homogeneous elements

   .. 4.2 4.3 2 2

Answer: 784.12

5. What is the output of the following code,
   ::

      x = array([[1, 2, 3], ['a', 2, 'c']])
      identity(x.shape)

   a. Will create an identity matrix of shape (2, 3).
   #. ``identity()`` function takes an integer as argument and a tuple
      is passed.
   #. Will return, array([[1,0,1],[0,1,0]])
   #. Will return, array([[0,1,0],[0,1,0]])

Answer: ``identity()`` function takes an integer as argument and a
      	tuple is passed.

6. ``ones_like()`` function?
   
   (A) Returns an array of ones with the same shape and type as a
       given array.
   (B) Return a new array of given shape and type, filled with ones.

   Read the statements and answer,

   a. Only statement A is correct.
   #. Only statement B is correct.
   #. Both statement A and B are correct.
   #. Both statement A and B are incorrect.

Answer: Only statement A is correct.

7. ``zeros_like()`` function?

   (A) Return a new array of given shape and type, filled with zeros.
   (B) Returns an array of zeros with the same shape and type as a
       given array.


   Read the statements and answer,

   a. Only statement A is correct.
   #. Only statement B is correct.
   #. Both statement A and B are correct.
   #. Both statement A and B are incorrect.

Answer: Only statement B is correct.

8. What will be output of the following code snippet.
   ::

      x = linspace(1,10,10).reshape(5,2)
      print (x[-3]+x[-4]).sum()

   a. 10.0 
   #. 18.0
   #. 14.0
   #. 16.44
   #. Error

Answer: 18

Larger Questions
----------------

.. A minimum of 2 questions here (along with answers)

1. Write a python script to create a 15x15 array of equally spaced 225
   elements from 1 to 1000, add 5 to each of the diagonal elements and
   find the sum of all odd rows of the array. Say for example the
   array,
   ::

       x = array([[1, 2, 3],
		  [4, 5, 6],
		  [7, 8, 9]])

   will give answer 40 ((1+5) + 2 + 3 + 7 + 8 + (9+5)).

2. For any given array([a1, a2, a3, .. , an]) the Vandermonde matrix
    will be [[1, a1, a1**2, .. , a1**(n-1)], [1, a2, a2**2, .. ,
    a2**(n-1)], .. , [1, an, an**2, .. ,an**(n-1)]]. Write a python
    script to generate the Vandermonde matrix and find the determinant
    of the matrix for [2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
    16, 17, 18, 19, 20].  [Hint: to find the determinant use the
    function ``det()`` from ``linalg`` module.]