Test 1 Review

Harold Nelson

2/12/2018

Problem 1:

Consider the following sequence of assignment statements: >>> a = 10 >>> b = 20 >>> a = b

What is the value of a after the last statement?

a = 10
b = 20
a = b

print(a)
## 20

Problem 2:

Consider the following for loop template. How many times will statement1 be executed?

for i in range(5):
    print(1)
    print(2)
## 1
## 2
## 1
## 2
## 1
## 2
## 1
## 2
## 1
## 2

Problem 3:

Given the following function definition, what would you supply for the second and third arguments to get a square with 100 square units of area?

def drawPolygon(myTurtle,sideLength,numSides): turnAngle = 360 / numSides for i in range(numSides): myTurtle.forward(sideLength) myTurtle.right(turnAngle)

The area of a square is given by the square of the side lenth, so in this cases the side length must be 10. There are 4 sides to a square.

This code won’t run in the presentation environment.

Problem 4:

Which of the following are legal python variable names?

Problem 5:

What would you write in python to create a turtle named Alex? Note that two commands are necessary.

import turtle

Alex = turtle.Turtle()

# Test

print(type(Alex))
## <class 'turtle.Turtle'>

Problem 6:

What two commands do you need to enter in python to obtain the square root of 3? You may not refer to fractional exponentiation.

import math
x = math.sqrt(3)
print(x)
## 1.73205080757

Problem 7:

What will the following code print?

for i in range(8,100,20):
    print(i)
## 8
## 28
## 48
## 68
## 88

Problem 8:

What will the following code print?

s = 0
for i in range(3):
    s = s + 2 * i - 1
print(s)    
## 3

Problem 9:

What will the following code print?

cond1 = False
cond2 = True
cond3 = True
if cond1:
    print("A")
elif cond2:
    print("B")
elif cond3:
    print("C")
else:
    print("D")

print("E") 
## B
## E

Problem 10:

What will the following code print?

a = "S"
b = "Y"

c = 3*(2 * a + b)
print(c)
## SSYSSYSSY