Quiz Solutions for COSC 235B - Quiz #4 by David A. Sykes - Prof. David A. Sykes, Quizzes of Computer Science

The solutions to quiz #4 for the cosc 235b course taught by david a. Sykes. It includes the results of various print statements and the code to complete the definition of the log2 function.

Typology: Quizzes

Pre 2010

Uploaded on 08/19/2009

koofers-user-aln-1
koofers-user-aln-1 🇺🇸

9 documents

1 / 1

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
COSC 235B David A. Sykes
- 1 –
Quiz #4
November 3, 2006
Name: Pledged:
Answer all questions. Write legibly.
1. Show the result of each of the following statements:
print 2 > 1 or 1 < 0
print 2 > 1 and 1 < 0
print not True
2. Show the output of the following code segment:
s = "Don't forget to *, y'all!"
i = 0
while s[i] != '*':
i = i + 1;
print s[0:i] + "VOTE" + s[i+1:]
3. Write a while loop that completes the definition of log2(n). The function calculates the number of
times a whole number n can be divided by 2 (using integer division) until the result reaches 1. For
example, log2(16) is 4 since 16 / 2 / 2 / 2 / 2 == 1. log2(23) is also 4 because 23 / 2 / 2 /
2 / 2. And log2(1) is 0. Hint: Use variable result as an accumulator to count the number of
divisions by 2.
def log2(n):
result = 0
return result

Partial preview of the text

Download Quiz Solutions for COSC 235B - Quiz #4 by David A. Sykes - Prof. David A. Sykes and more Quizzes Computer Science in PDF only on Docsity!

COSC 235B David A. Sykes

- 1 –

Quiz

November 3, 2006

Name: Pledged:

Answer all questions. Write legibly.

  1. Show the result of each of the following statements:

print 2 > 1 or 1 < 0

print 2 > 1 and 1 < 0

print not True

  1. Show the output of the following code segment:

s = "Don't forget to , y'all!" i = 0 while s[i] != '': i = i + 1; print s[0:i] + "VOTE" + s[i+1:]

  1. Write a while loop that completes the definition of log2(n). The function calculates the number of times a whole number n can be divided by 2 (using integer division) until the result reaches 1. For example, log2(16) is 4 since 16 / 2 / 2 / 2 / 2 == 1. log2(23) is also 4 because 23 / 2 / 2 / 2 / 2. And log2(1) is 0. Hint: Use variable result as an accumulator to count the number of divisions by 2.

def log2(n): result = 0

return result