CSci 150: Foundations of computer science
Home Syllabus Readings Projects Tests

Test 1: Questions

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

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
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.

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

X1.5.

[8 pts] What is the output of the following program?

a = '0'
for i in range(5):
    a = a + 'i'
    print(a)
X1.6.

[12 pts] Using a for loop, write a program that computes and displays

1/ + 1/ + 1/ + … + 1/1000² .
X1.7.

[6 pts] What will be in the list nums following execution of the below code?

nums = [11]
for i in range(4):
    nums = nums + [i]
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)]

X1.9.

[10 pts] Tabulate how the variables i, x, and y change while executing the below program fragment.

data = [23571113]
y = 0
for i in range(5):
    x = data[i] % 4
    if x >= y:
        y = y + x
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())

Test 1: Solutions

X1.1.
a.x % 8is 2
b.(x // 15) * 15is 45
c.x + x ** 2is 2,550
X1.2.
a=20
b=5
c=20
X1.3.
splat = (ball / 16) ** 0.5
X1.4.
a.s[4]is i
b.s[1:5]is arri
c.'s' + sis swarrior
X1.5.
0i
0ii
0iii
0iiii
0iiiii
X1.6.
total = 0
for i in range(11001):
    total = total + 1 / (i ** 2)
print(total)
X1.7.
[1, 1, 0, 1, 2, 3]
X1.8.
a.nums[1] + nums[-1]is 9
b.sorted(nums[1:4])is [3, 5, 7]
c.nums[min(nums)]is 5
X1.9.
i: 0 1 2 3 4
x: 2 3 1 3 3
y: 0 2 5
X1.10.
total = 0
for index in range(50):
    num = int(input())
    if num < 100:
        total = total + num
print(total)