Quiz 2b#

Question 1#

In the following code, what does the number 100 represent?

my_button = frame.add_button("My Label", button_handler, 100)

Use the CodeSkulptor documentation to look it up.

Height of the button in pixels
Horizontal position of the button in pixels
Vertical position of the button in pixels
Width of the button in pixels \


Width of the button in pixels \


Question 2#

How many control objects are allowed in a frame?

0
1
Unlimited, i.e., 0 or more \


Unlimited, i.e., 0 or more


Question 3#

Which of the following are valid ways of defining and using a label?

Note that ellipses denote code that is omitted from and irrelevant for the question. Feel free to fill in plausible parameters and test each choice in CodeSkulptor.

  import simplegui

frame = simplegui.create_frame(...)
frame.add_label("Label one")
frame.add_label("Label two")
 

\

  import simplegui

simplegui.create_frame(...)
l1 = simplegui.add_label("Label one")
l2 = simplegui.add_label("Label two")
 

\

  import simplegui

f = simplegui.create_frame(...)
f.add_label("My label")
 

\

  import simplegui

simplegui.create_frame(...)
simplegui.add_label("My label")
 

1, 3


Question 4#

When you enter text into an input field and press enter, the text is passed to the input field’s event handler. What is the data type of the text?

A number
A string or a number, depending on the text entered
A string \


string


Question 5#

Consider the following conditional statement.

if p == False:
    return False
elif q == False:
    return False
else:
    return True

That is equivalent to which of the following simpler statements?

Try to reason logically about each of the statements, but also try each in CodeSkulptor .

  return p and (not q)
 

\

  return (not p) or (not q)
 

\

  return q and p
 

\

  return not(p or q)
 

  return q and p
 

Question 6#

Which of the following describes the mistake in the following code?

def volume_cube(side):
    """ Returns the volume of a cube, given the length of its side. """
    print side ** 3
    
s = 5
print "The volume of a cube with sides", s, "long is", volume_cube(s), "."

The call to volume_cube shouldn’t be within a print statement. More generally, function calls usually shouldn’t be within print statements.
The function should return, not print, its result.
All of the printing should be done within the function. \


The function should return, not print, its result. \


Question 7#

What kind of errors can happen if you are missing a needed global declaration in one of your function definitions? For this question, you need only consider the case where the problem is in the function that is missing the global declaration.

If you are having trouble with this question, watch this week’s Programming Tips video again.

An incorrect computation that generates no error message
NameError
AttributeError
SyntaxError
Error: local variable '…' referenced before assignment


An incorrect computation that generates no error message \

x = 0
def f():
	x = 1
	print(x)
print(x)
f()
print(x)

outputs:

0
1
0

with global:

x = 0
def f():
	global x
	x = 1
	print(x)
print(x)
f()
print(x)

outputs:

0
1
1

Error: local variable '…' referenced before assignment. Example:

x = 0
def f():
	x += 1
	print(x)
print(x)
f()

outputs:

0
...
UnboundLocalError: local variable 'x' referenced before assignment
x = 0
def f():
	global x
	x += 1
	print(x)
print(x)
f()

outputs:

0
1

Question 8#

Which of the following function definitions are in the recommended code style ?

  def f (x, y):
    """ Add the two inputs. """
    return x + y
 

\

  def f(x, y):
    """ Add the two inputs. """
    return x + y
 

\

  def f(x, y):
    return x + y # Add the two inputs.
 

\

  def f(x,y):
    """ Add the two inputs. """
    return x + y
 

  def f(x, y):
    """ Add the two inputs. """
    return x + y
 

Question 9#

Cut and paste the following code into CodeSkulptor . Run it and make an attempt to understand how it works.

# Simple interactive application

import simplegui

# Define globals.

message = "Welcome!"
count = 0

# Define event handlers.

def button_handler():
    """Count number of button presses."""
    global count
    count  = 1
    print message," You have clicked", count, "times."

def input_handler(text):
    """Get text to be displayed."""
    global message
    message = text

# Create frame and register event handlers.

frame = simplegui.create_frame("Home", 100, 200)
frame.add_button("Click me", button_handler)
frame.add_input("New message:", input_handler, 100)

# Start frame.

frame.start()

We’d like to modify the code so that the count is reset to zero whenever a new message is entered. Where would you need to modify this code to implement this change?

Add an assignment to count in the initialization of global variables.
Add an assignment to count in the event handler for the button.
Add an assignment to count in the event handler for the input field. Also add a global count declaration there.
Add an assignment to count at the end of this code. \


Add an assignment to count in the event handler for the input field. Also add a global count declaration there. \


Question 10#

In the game “Guess the number”, what is the minimum number of guesses necessary to guarantee that the guesser can always win if the secret number is chosen in range(0, 400)?

Review the mini-project description for “Guess the number” if you are having trouble with this problem.

8 guesses
9 guesses
10 guesses
12 guesses
It’s impossible to guarantee that you can always win at “Guess the number”. \


9 guesses \

log_2(400) ~= 8.xxx next integer is 9.

You can also try by simulating a game with a smaller number like range(0, 4) with pen and paper.

Assume that the number to find is 0. If there are even number of numbers, then everytime you have to assume the worst case by selecting the integer right after the half, e.g., 0 1 2 3 - you select 2.

If there are uneven numbers, e.g., 0 1 2 3 4, we select the number in the middle by dividing by two - 2