printable version
Test 1
[1]
[2]
[3]
[4]
[5]
[6]
[7]
[8]
[9]
[10]
Problem X1.1.
[6 pts]
If x
is the integer 50,
what is the value of each of the following Python
expressions?
a. x % 8
b. (x // 15) * 15
c. x + x ** 2
a. | x % 8 | is |
2 |
b. | (x // 15) * 15 | is |
45 |
c. | x + x ** 2 | is |
2,550 |
Problem X1.2.
[6 pts]
List the values of a
, b
, and c
following the
execution of the below fragment.
a = 10
b = 20
c = 30
a = b
c = a
b = 5
Problem X1.3.
[8 pts]
Recall from physics that if you drop an object, after t seconds
it will have fallen 16 t² feet. Suppose the variable ball
has been set up holding a ball's initial height, measured in feet from the
ground. Create a variable splat
holding the number of seconds it
will take between when the ball is released and when it hits the
ground.
splat = (ball / 16) ** 0.5
Problem X1.4.
[6 pts]
If s
is the string “warrior” (no quotes),
what is the value of each of the following Python expressions?
a. s[4]
b. s[1:5]
c. 's' + s
a. | s[4] | is |
i |
b. | s[1:5] | is |
arri |
c. | 's' + s | is |
swarrior |
Problem X1.5.
[8 pts]
What is the output of the following program?
a = '0'
for i in range(5):
a = a + 'i'
print(a)
0i
0ii
0iii
0iiii
0iiiii
Problem X1.6.
[12 pts]
Using a for
loop, write a program that computes and
displays
1/1² + 1/2² + 1/3² + … + 1/1000² .
total = 0
for i in range(1, 1001):
total = total + 1 / (i ** 2)
print(total)
Problem X1.7.
[6 pts]
What will be in the list nums
following execution of the
below code?
nums = [1, 1]
for i in range(4):
nums = nums + [i]
[1, 1, 0, 1, 2, 3]
Problem X1.8.
[6 pts]
If nums
is [11, 7, 5, 3, 2], what is the value of each
of the following expressions?
a. nums[1] + nums[-1]
b. sorted(nums[1:4])
c. nums[min(nums)]
a. | nums[1] + nums[-1] | is |
9 |
b. | sorted(nums[1:4]) | is |
[3, 5, 7] |
c. | nums[min(nums)] | is |
5 |
Problem X1.9.
[10 pts]
Tabulate how the variables i
, x
, and y
change
while executing the below program fragment.
data = [2, 3, 5, 7, 11, 13]
y = 0
for i in range(5):
x = data[i] % 4
if x >= y:
y = y + x
i : |
|
0 |
|
|
1 |
|
|
2 |
|
3 |
|
4 |
|
x : |
|
|
2 |
|
|
3 |
|
|
1 |
|
3 |
|
3 |
y : |
0 |
|
|
2 |
|
|
5 |
Problem X1.10.
[12 pts]
The below program reads 50 integers from the user.
Modify the program so that after reading the fiftieth integer,
it displays the sum of all those integers that are less than 100.
For example, if the user enters 200, 59, 99, 105, and 10, then
enters 150 forty-five times, the output should be 168
(from 59 + 99 + 10)
for index in range(50):
num = int(input())
total = 0
for index in range(50):
num = int(input())
if num < 100:
total = total + num
print(total)