loops/questions.rst
author anand
Mon, 15 Nov 2010 15:01:21 +0530
changeset 504 340fafcb92f6
parent 462 3a1575a45152
permissions -rw-r--r--
checklist not OK; one more long answer question has to be added.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
254
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
     1
Objective Questions
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
     2
-------------------
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     3
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     4
.. A mininum of 8 questions here. 
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
     5
254
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
     6
1. Braces are used to indicate blocks in Python. True or False?
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
     7
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
     8
   Answer: False
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
     9
366
894591150f7b reviewed questions for loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 293
diff changeset
    10
#. ``for`` loop can iterate over, 
254
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    11
   
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    12
   a. list of numbers
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    13
   #. list of strings
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    14
   #. strings
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    15
   #. tuples
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    16
   #. all of the above
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    17
462
3a1575a45152 Added question to loops.
Puneeth Chaganti <punchagan@fossee.in>
parents: 453
diff changeset
    18
   Answer: all of the above
3a1575a45152 Added question to loops.
Puneeth Chaganti <punchagan@fossee.in>
parents: 453
diff changeset
    19
254
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    20
.. I was not sure of how to frame this question. Can someone fix it?
462
3a1575a45152 Added question to loops.
Puneeth Chaganti <punchagan@fossee.in>
parents: 453
diff changeset
    21
.. #[bhanu: it works for every `sequence` or an iterator for that matter right?]
254
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    22
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    23
#. ``x = range(20)``. What is x?
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    24
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    25
   Answer: A list of numbers from 0 to 19. 
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    26
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    27
#. ``x = range(5, 20)``. What is x?
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    28
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    29
   Answer: A list of numbers from 5 to 19. 
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    30
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    31
#. ``x = range(0, 20, 5)``. What is x?
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    32
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    33
   a. [5, 10, 15, 20]
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    34
   #. [0, 5, 10, 15, 20]
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    35
   #. [0, 5, 10, 15]
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    36
   #. Empty list
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    37
   #. None of the Above
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    38
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    39
   Answer: [0, 5, 10, 15]
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    40
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    41
#. ``x = range(20, 5)``. What is x?
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    42
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    43
   a. [5, 10, 15, 20]
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    44
   #. [0, 5, 10, 15, 20]
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    45
   #. [0, 5, 10, 15]
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    46
   #. Empty list
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    47
   #. None of the Above
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    48
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    49
   Answer: Empty list
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    50
254
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    51
#. ``x = range(20, 5, -1)``. What is x?
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    52
254
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    53
   Answer: A list of numbers from 20 to 6.
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    54
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    55
#. What is the output of the following code block?
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    56
   ::
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    57
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    58
     for i in range(1, 4):
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    59
         for j in range(1, 4):
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    60
             print i * j
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    61
             break
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    62
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    63
   Answer: 1 to 3 is printed
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    64
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    65
#. What is the output of the following code block?
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    66
   ::
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    67
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    68
     for i in range(1, 4):
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    69
         for j in range(1, 4):
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    70
             pass
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    71
             print i * j
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    72
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    73
   Answer::
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    74
     
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    75
     3
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    76
     6
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    77
     9
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    78
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    79
#. What is the output of the following code block?
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    80
   ::
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    81
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    82
     for i in range(1, 4):
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    83
         for j in range(1, 4):
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    84
             continue
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    85
             print i * j
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    86
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    87
   Answer: Nothing is printed
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    88
453
b0e3f24bd78a One more long answer question has to be added.
anand
parents: 366
diff changeset
    89
.. #[[Anoop: I think more questions on while loop have to be added as
366
894591150f7b reviewed questions for loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 293
diff changeset
    90
   for loop was already covered in another LO, these questions can be
894591150f7b reviewed questions for loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 293
diff changeset
    91
   kept, but it will good if we add few more on while loop]]
894591150f7b reviewed questions for loops.
Anoop Jacob Thomas<anoop@fossee.in>
parents: 293
diff changeset
    92
254
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    93
Larger Questions
c43677920502 added questions and pre-reqs to loops LO.
Puneeth Chaganti <punchagan@fossee.in>
parents: 217
diff changeset
    94
----------------
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    95
293
ca701f1ef7fb reviewed questions
Nishanth <nishanth@fossee.in>
parents: 254
diff changeset
    96
1. A number is called Armstrong number if the sum of cubes of its digits is
ca701f1ef7fb reviewed questions
Nishanth <nishanth@fossee.in>
parents: 254
diff changeset
    97
   equal to the number itself. Find all the three digit Armstrong numbers.
217
b595f90016c5 Changed structure of my scripts.
Puneeth Chaganti <punchagan@fossee.in>
parents:
diff changeset
    98
462
3a1575a45152 Added question to loops.
Puneeth Chaganti <punchagan@fossee.in>
parents: 453
diff changeset
    99
2. Collatz sequence - Given a number ``n``, multiply by 3 and add 1 to
3a1575a45152 Added question to loops.
Puneeth Chaganti <punchagan@fossee.in>
parents: 453
diff changeset
   100
   it, if it is odd, otherwise divide it by two. With whatever ``n``
3a1575a45152 Added question to loops.
Puneeth Chaganti <punchagan@fossee.in>
parents: 453
diff changeset
   101
   we start with, we finally end with the sequence 4, 2, 1. Write a
3a1575a45152 Added question to loops.
Puneeth Chaganti <punchagan@fossee.in>
parents: 453
diff changeset
   102
   program to print this, given some number ``n``.