conditionals/questions.rst
author bhanu
Mon, 15 Nov 2010 14:53:10 +0530
changeset 501 2c30d4a242ee
parent 438 4523b2048663
permissions -rw-r--r--
language check done for `using sage for teaching`

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

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

1. Given a variable ``time``, print ``Good Morning`` if it is less
   than 12, otherwise ``Hello``. 

   Answer::
     
     if time < 12:
         print "Good Morning"

     else:
         print "Hello"

#. Every ``if`` block must be followed by an ``else`` block. T or F?

   Answer: F

#. Every ``if/elif/else`` ladder MUST end with an ``else`` block. T/F?

   Answer: F

#. An if/elif/else ladder can have any number of elif blocks. T or F?

   Answer: T

#. What will be printed at the end this code block::
   
     x = 20

     if x > 10:
     print x * 100
   
   Answer: IndentationError - Expected and indented block.. 

#. What will be printed at the end this code block::
   
     x = 20

     if x > 10:
         print x * 100
         else:
             print x

   Answer: SyntaxError

#. What will be printed at the end this code block::
   
     x = 20

     if x > 10:
         print x * 100
     else:
         print x

   Answer: 2000

#. Convert the if else ladder below into a ternary conditional
   statement::
   
     x = 20

     if x > 10:
         print x * 100
     else:
         print x

   Answer: print x * 100 if x > 10 else x


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

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

1. Given a number, say, n. If it is divisible by 10, print the
   quotient when divided by 10, otherwise if it is divisible by 5,
   print the corresponding quotient, otherwise, print the number.


   Answer::

        if n%10==0:
            print n/10
        elif n%5==0:
            print n/5
        else:
            print n

2. Given a number, say, n. Write an if block to multiply n by three
   and add 1, if it is odd, otherwise halve it. 

   Answer::

        if n % 2:
            n = n*3+1
        else:
            n /= 2