Docsity

CS 235 Quiz #2: Fall 2007 - Prof. Joseph D. Sloan, Quizzes of Computer Science

Toggle sidebar
bg1
CS 235 Quiz #2: Page 1 of 5
COSC235 Fall 2007
Test #2
Please read each question carefully and be sure to give complete answers. Work quickly
and good luck!
1. (2 pt.) Print your name: ___________________________________________
2. (6 pts.) Given that x is 5 and y is 7, what is the value of x after each statement is
executed? Assume each part is independent of the preceding parts.
a) x /= y
b) x *= x + 2
c) x %= x – 4
3. (6 pts.) Write the equivalent while-loop for the following for-loop:
sum = 0
for i in range(1, 100, 3):
sum += i
4. (8 pts.) Write a while-loop that will
a) find the sum of the first 100 even numbers, i.e., 2 + 4 + 6 + … for 100 different
numbers.
b) count how many time a positive integer can be divided by two before it is zero.
pf3
pf4
pf5

Partial preview of the text

Download CS 235 Quiz #2: Fall 2007 - Prof. Joseph D. Sloan and more Quizzes Computer Science in PDF only on Docsity!

COSC 235 Fall 2007

Test

Please read each question carefully and be sure to give complete answers. Work quickly and good luck!

  1. (2 pt.) Print your name: ___________________________________________
  2. (6 pts.) Given that x is 5 and y is 7 , what is the value of x after each statement is executed? Assume each part is independent of the preceding parts. a) x /= y b) x *= x + 2 c) x %= x – 4
  3. (6 pts.) Write the equivalent while- loop for the following for- loop: sum = 0 for i in range(1, 100, 3): sum += i
  4. (8 pts.) Write a while- loop that will a) find the sum of the first 100 even numbers, i.e., 2 + 4 + 6 + … for 100 different numbers. b) count how many time a positive integer can be divided by two before it is zero.
  1. (6 pts.) In the following truth table, each column is either the logical NOT of a previous column, or the logical AND or OR of two previous columns. Correctly label the last three columns. p q r T T T F T F T T F F T F T F T F T F T F F F F F F T T T T T F T F T T T F F T T T T F F F T F F
  2. (8 pts.) Rewrite the following post-test loop as a standard Python while loop, i.e., a while- loop without True or break: while True: weight = input("Enter a positive number: ") if weight > 0: break else: print "Please follow directions! "
  3. (6 pts.) For each of the following, give the result or explain why the action is illegal: a) a, b = 1, 2, 3 c) (a, b) = 3
  4. (4 pts.) If you want to add information to the end of a file data.txt, what command would you use to open the file?
  1. (16 pts.) With the graphics package loaded, the figure on the left was created by the following lines of code: win = GraphWin("Quiz #2",400,400) Rectangle(Point(50,50), Point(350,350)).draw(win) In the next few steps, you will write code so that the figure on the right is produced. a) What code would you write to add the diagonal line from the upper left corner to the lower right? b) What code would you write to add the small square (with an edge of 50 pixels) at the center? c) What code would you write to add the circle? d) What code would you add to display “Box” to the left of the figure?
  1. (6 pts) Write a Boolean function triple() that takes a single number as an argument returns True if the number is divisible by three. E.g., triple(19) would return False.
  2. (6 pts.) Write a function bmiCk() that takes a numeric argument and displays one of three messages. If the argument is less than 25, display "Fit and trim". If the argument is 25 or more but is less than 30, display "A little over weight". If the argument is 30 or more, display "Seriously overweight". For example, bmiCk(29.0) would display "A little over weight".
  3. (8 pts.) Write a function fileLength() that takes a file name as an argument and returns the number of lines in that file. E.g., fileLength("data.txt ") would return 4 for the file in Problem #9. Pledged: ________________________________________________________________