Compsci 06/101, Spring 2012, January 24

By entering your name/net-id below you indicate you are in class on January 24 to answer these questions and that you have answered them. Your name should not appear unless you are in class when answering these questions.
Name_________________   net-id _______   Name_________________   net-id _______


Name_________________   net-id _______   Name_________________   net-id _______

  1. What is the value of the Python 2.7 expression
        16/7 + 16%3
    

    1. 2

    2. 3

    3. other


    The function point below is part of the snarfable code in the Pydev project spring12-pistuff. Questions follow the code.

    
    def point(n):
        inside = 0;
        plt.axis('scaled')
        plt.axis([0,1.1,0,1.1])
        for dummy in range(n):
            x = random.random()
            y = random.random()
            
            if x*x + y*y <= 1.0:
                inside += 1
                plt.plot(x,y,'ro')
            else:
                plt.plot(x,y,'bo')
        return inside
    
    
  2. What is the purpose of the variable inside?

    1. it is the number of virtual "darts" inside the circle

    2. it is the number of times the loop executes

    3. it is the number of virtual "darts" thrown at the simulated board

  3. Why is the variable inside initialized to zero before the loop rather than within the loop body?

    1. in the loop body would reset the value so it would always be zero or one when the loop finishes

    2. variables cannot be initialized in a loop body

    3. it could be set to zero in the loop body

  4. What is a reasonable description of .plot and .axis? (more than one possible)

    1. They are methods in some module/library

    2. Their use isn't needed to approximate PI, but to visualize the process

    3. They cause lines and circles to be displayed when the program is run