manipulating-lists/questions.rst
changeset 522 d33698326409
parent 521 88a01948450d
child 523 54bdda4aefa5
equal deleted inserted replaced
521:88a01948450d 522:d33698326409
     1 Objective Questions
       
     2 -------------------
       
     3 
       
     4 .. A mininum of 8 questions here (along with answers)
       
     5 
       
     6 1. Given the list primes, ``primes = [2, 3, 5, 7, 11, 13, 17, 19, 23,
       
     7    29]``, How do you obtain the last 4 primes?
       
     8 
       
     9    Answer: primes[-4:]
       
    10 
       
    11 #. Given the list primes, ``primes = [2, 3, 5, 7, 11, 13, 17, 19, 23,
       
    12    29]``, What is the output of ``primes[::5]``?
       
    13 
       
    14    Answer: ``[2, 13]``
       
    15    
       
    16 #. Given a list, p, of unknown length, obtain the first 3 (or all, if
       
    17    there are fewer) characters of it. 
       
    18 
       
    19    Answer: p[:3]
       
    20 
       
    21 #. The method ``reverse`` reverses a list in-place. True or False?
       
    22 
       
    23    Answer: True
       
    24    
       
    25 #. ``reversed`` function reverses a list in-place. True or False?
       
    26 
       
    27    Answer: False
       
    28 
       
    29 #. Given the list ``p = [1, 2, 3]``. p[4] produces an IndexError. True
       
    30    or False?
       
    31 
       
    32    Answer: True
       
    33 
       
    34 #. Given the list ``p = [1, 2, 3]``. p[:4] produces an IndexError. True
       
    35    or False?
       
    36 
       
    37    Answer: False
       
    38 
       
    39 #. Given the list primes, ``primes = [2, 3, 5, 7, 11]``, What is the
       
    40    output of ``primes[::-1]``?
       
    41 
       
    42    Answer: [11, 7, 5, 3, 2]
       
    43 
       
    44 #. Given the list primes, ``primes = [2, 3, 5, 7, 11]``, What is the
       
    45    output of ``primes[::-3]``?
       
    46 
       
    47    Answer: [11, 3]
       
    48 
       
    49 
       
    50 Larger Questions
       
    51 ----------------
       
    52 
       
    53 .. A minimum of 2 questions here (along with answers)
       
    54 
       
    55 #. Given a list p. Append it's reverse to itself. 
       
    56 
       
    57    Answer::
       
    58 
       
    59       p = p + reversed(p)
       
    60 
       
    61 
       
    62 #. Marks is a list containing the roll numbers of students followed by
       
    63    marks. [This is not a recommended way to hold the marks details,
       
    64    but the teacher knows only so much Python!] Now she wants to get
       
    65    the average of the marks. Help her do it. 
       
    66 
       
    67    Answer::
       
    68 
       
    69      marks = [1, 9, 2, 8, 3, 3, 4, 10, 5, 2]
       
    70      average = sum(marks[1::2])/len(marks[1::2])
       
    71 
       
    72