printable version
Quiz 2
[1]
[2]
[3]
[4]
Problem Q2.1.
[6 pts]
Perform each of the following conversions.
- the number −60(10) to an eight-bit
two's-complement pattern
- the two's-complement pattern 10001000 to a base-10
number
Problem Q2.2.
[8 pts]
Tabulate the values taken on by the variables
i, j,
k, and n as the program
fragment below executes. (This is not identical to the
Exam 1 question.)
n = 1
for i in range(3):
for j in range(3):
k = (i + 1) * (j + 1)
n = n + k
i: | | 0, | | | 1, | | | 2 | | |
j: | | 0, | 1, | 2, | 0, | 1, | 2, | 0, | 1, | 2 |
k: | | 1, | 2, | 3, | 2, | 4, | 6, | 3, | 6, | 9 |
n: | 1, | | | 4, | | | 10, | | | 19 |
Problem Q2.3.
[8 pts]
The following program is intended to deposit $100 into an empty
account and display the resulting balance, so it should display
100.0.
Explain why it displays 0.0
instead.
def deposit(account_balance, deposit_amount):
account_balance = account_balance + deposit_amount
my_account = 0.00
deposit(my_account, 100.00)
print my_account
At the time of the function call, my_account's value is copied
into the deposit function's account_balance variable;
but the call does not establish any link between the variables
beyond simply copying the values at this time.
Thus, when deposit later changes the value
associated with account_balance, this does not affect
the value associated with my_account, and so
my_account remains unchanged from its initial value of 0.
Problem Q2.4.
[8 pts]
Write a function latinize that takes a string
parameter and produces a string where the first letter has been
moved to the end and followed by ay. For example,
latinize('nix') should produce ixnay,
or latinize('road') should produce
oadray. The fragment below illustrates how your function
might be used to convert a sentence to its Latin
equivalent.
sentence = raw_input('Sentence: ')
result = ''
for word in sentence.split():
result = result + ' ' + latinize(word)
print result
def latinize(str):
first = str[0]
rest = str[1:len(str)]
return rest + first + 'ay'