loops/questions.rst
author Amit Sethi
Fri, 12 Nov 2010 15:23:54 +0530
changeset 489 bc8d01c3c9b3
parent 462 3a1575a45152
permissions -rw-r--r--
Added quickref for testing-debugging

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

.. A mininum of 8 questions here. 

1. Braces are used to indicate blocks in Python. True or False?

   Answer: False

#. ``for`` loop can iterate over, 
   
   a. list of numbers
   #. list of strings
   #. strings
   #. tuples
   #. all of the above

   Answer: all of the above

.. I was not sure of how to frame this question. Can someone fix it?
.. #[bhanu: it works for every `sequence` or an iterator for that matter right?]

#. ``x = range(20)``. What is x?

   Answer: A list of numbers from 0 to 19. 

#. ``x = range(5, 20)``. What is x?

   Answer: A list of numbers from 5 to 19. 

#. ``x = range(0, 20, 5)``. What is x?

   a. [5, 10, 15, 20]
   #. [0, 5, 10, 15, 20]
   #. [0, 5, 10, 15]
   #. Empty list
   #. None of the Above

   Answer: [0, 5, 10, 15]

#. ``x = range(20, 5)``. What is x?

   a. [5, 10, 15, 20]
   #. [0, 5, 10, 15, 20]
   #. [0, 5, 10, 15]
   #. Empty list
   #. None of the Above

   Answer: Empty list

#. ``x = range(20, 5, -1)``. What is x?

   Answer: A list of numbers from 20 to 6.

#. What is the output of the following code block?
   ::

     for i in range(1, 4):
         for j in range(1, 4):
             print i * j
             break

   Answer: 1 to 3 is printed

#. What is the output of the following code block?
   ::

     for i in range(1, 4):
         for j in range(1, 4):
             pass
             print i * j

   Answer::
     
     3
     6
     9

#. What is the output of the following code block?
   ::

     for i in range(1, 4):
         for j in range(1, 4):
             continue
             print i * j

   Answer: Nothing is printed

.. #[[Anoop: I think more questions on while loop have to be added as
   for loop was already covered in another LO, these questions can be
   kept, but it will good if we add few more on while loop]]

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

1. A number is called Armstrong number if the sum of cubes of its digits is
   equal to the number itself. Find all the three digit Armstrong numbers.

2. Collatz sequence - Given a number ``n``, multiply by 3 and add 1 to
   it, if it is odd, otherwise divide it by two. With whatever ``n``
   we start with, we finally end with the sequence 4, 2, 1. Write a
   program to print this, given some number ``n``.