CSC 127A - Exam1



ISTA130 Midterm Fall 11 Section Leader_______ Name____________________________150pts

1. Write an X To the left of each valid Python name (identifier). (4pts)

a) __ X pyhonIndetfriaR c) __ X a_B_C_d

b) ___ 9to5 d) ___ x*y all or none

2. Write an X To the left of each Python reserved word (keyword). (4pts)

a) _ X if c) ___ Arizona

b) ___ four d) _ X for all or none

3. For each Python expression, indicate its value in the space to its left. Be sure to write a resulting value of the appropriate type. For example, 2.0 is different than 2 which is also different than '2'. (12pts)

a) __3.5____ 14 / 4 f) _2____ 1 + 2 * 3 // 4

b) ___3___ 14 // 4 g) _1.0___ 1 / 4 * 4

c) ___2___ 14 % 4 h) _7.56__ eval('7.56') all or none

d) _18.0__ 14 + 4.0 i) _456__ eval('456') -1/2 if type is wrong (1 not 1.0)

e) _'144'__ '14' + '4' j) __24__ 99 % 50 % 25 (or 144 not '144')

4. Write the output that would be printed from each of the following code fragments. (8pts)

| x = 9 | a, b = 5, 9 | n = 4 |

|y = x / 2 |b, a = a, b |for i in range(1, n + 1, 1): |

|print('y ==', y) +1 4.5 |print(a, b) |print(i, end=' ') |

| | | |

|y == 4.5 |9 5 |1 2 3 4 |

5. For each Python expression, indicate its value in the space to its left. Be sure to list a resulting value of the appropriate type. For example, 2.0 is different than 2. Assume import math (6pts)

a) __3.0___ math.sqrt(9.0) d) __3.46__ round(3.45678, 2) all or none

b) ___3___ abs(5-8) e) __3.5___ min(3.5678, 3.5) -1/2 if type is wrong (3 not 3.0)

c) __3.0___ abs(8.0 - 5.0) f) __-3____ max(-5, -3)

6. Write the complete dialog (input and output) from this program when the user enters 5 for x followed by 6 for y. (4pts)

x = int(input('x? '))

y = int(input('y? '))

print('Max: ', max(x, y))

print('Min: ', min(x, y))

7. Write a complete Python program that reads two test scores and prints the average of the two. Your dialog should look like this when the user enters 90.0 followed by 81.0. (6pts)

Test1? 90.0

Test2? 81.0

Average: 85.5

8. Write the output generated by the following code. (4pts)

a = 20;

b = 8;

if a % 10 == 0:

b = b * 10

a = a + 10

if a < b:

b = b + 5 three All or none

print(a, b)

9. For each Python expression, write True if the expression evaluates to true. Write False if the expression evaluates to false. Assume the following variables always have the values shown. (6pts)

s = 'algorithm'

n = 12

x = 99.9

a) __False__ n + 0.001 == 12 d) _ False___ s >= 'apple' and s = 101.0 e) _True____ not(s.upper() == 'Algorithm') All or none

c) __True___ 10 ................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download