conditionals/questions.rst
author bhanu
Mon, 15 Nov 2010 15:07:22 +0530
changeset 505 394c3642cf9c
parent 438 4523b2048663
permissions -rw-r--r--
Language check done for `getting started with tuples`

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