input_output.rst
changeset 231 e78c284d644b
parent 141 05a7b0506134
equal deleted inserted replaced
230:91852d9745d7 231:e78c284d644b
   178  
   178  
   179 .. Author              : Nishanth
   179 .. Author              : Nishanth
   180    Internal Reviewer 1 : 
   180    Internal Reviewer 1 : 
   181    Internal Reviewer 2 : 
   181    Internal Reviewer 2 : 
   182    External Reviewer   :
   182    External Reviewer   :
       
   183 
       
   184 Questions
       
   185 =========
       
   186 
       
   187  1. ``a = 2.5``. What is the output of ``print "a is %d"%(a)``
       
   188 
       
   189    a. a is 2.5
       
   190    #. a is 2.0
       
   191    #. 2.0
       
   192    #. a is 2
       
   193 
       
   194    Answer: a is 2
       
   195 
       
   196  2. What does ``print "This is",     "a line ", "with  spaces"`` print?
       
   197 
       
   198    a. This is a line with spaces
       
   199    #. This is a line with  spaces
       
   200    #. This is     a line   with   spaces
       
   201    #. This is a line  with  spaces
       
   202 
       
   203    Answer: This is a line  with  spaces
       
   204 
       
   205  3. What does ``print "%2.5f"%(1.2)`` print?
       
   206 
       
   207    a. 1.2
       
   208    #. 1.20
       
   209    #. 1.20000
       
   210    #. 00001.2
       
   211 
       
   212    Answer: 1.20000
       
   213 
       
   214  4. What is the output of the following code::
       
   215 
       
   216      for i in range(1,10,2):
       
   217          print i,
       
   218 
       
   219     Answer::
       
   220 
       
   221       1 3 5 7 9
       
   222 
       
   223  5. ``a = 2`` and ``b = 4.5``. What does ``print "a is %d and b is %2.1f"%(b, a)``
       
   224     print?
       
   225 
       
   226    a. a is 2 and b is 4.5
       
   227    #. a is 4 and b is 2
       
   228    #. a is 4 and b is 2.0
       
   229    #. a is 4.5 and b is 2
       
   230 
       
   231    Answer: a is 4 and b is 2.0
       
   232 
       
   233  6. What is the prompt displayed by ``raw_input("Say something\nType here:")``
       
   234 
       
   235    Answer::
       
   236 
       
   237      Say something 
       
   238      Type here:
       
   239 
       
   240  6. What is the prompt displayed by ``raw_input("value of a is %d\nInput b
       
   241     value:"a)`` and ``a = 2.5``
       
   242 
       
   243    Answer::
       
   244 
       
   245      value of a is 2
       
   246      Input ba value:
       
   247 
       
   248  7. ``a = raw_input()`` and user enters ``2.5``. What is the type of a?
       
   249 
       
   250    a. str
       
   251    #. int
       
   252    #. float
       
   253    #. char
       
   254 
       
   255    Answer: str
       
   256 
       
   257  8. ``a = int(raw_input())`` and user enters ``4.5``. What happens?
       
   258 
       
   259    a. a = 4.5
       
   260    #. a = 4
       
   261    #. a = 4.0
       
   262    #. Error
       
   263 
       
   264    Answer: Error
       
   265 
       
   266  9. ``a = raw_input()`` and user enters ``"this is a string"``. What does 
       
   267     ``print a`` produce?
       
   268 
       
   269    a. 'this is a string'
       
   270    b. 'this is a string"
       
   271    c. "this is a string"
       
   272    #. this is a string
       
   273 
       
   274    Answer: "this is a string"
       
   275 
       
   276 Problems
       
   277 ========
       
   278 
       
   279  1. Answer to universe and everything. Keep taking input from user and print it
       
   280     back until the input is 42.
       
   281 
       
   282   Answer::
       
   283 
       
   284     ip = raw_input()
       
   285     while ip != "42":
       
   286         print ip
       
   287 
       
   288  2.