CSci 150: Foundations of computer science I
Home Syllabus Assignments Tests

Quiz 2: Questions

Q2.1.

[6 pts] Perform each of the following conversions.

  1. the number −60(10) to an eight-bit two's-complement pattern
  2. the two's-complement pattern 10001000 to a base-10 number
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
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
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

Quiz 2: Solutions

Q2.1.
  1. 11000100
  2. −120(10)
Q2.2.
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
Q2.3.

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.

Q2.4.
def latinize(str):
    first = str[0]
    rest = str[1:len(str)]
    return rest + first + 'ay'