CS 235 Quiz #2: Truth Tables, Loops, Functions, and Lists, Quizzes of Computer Science

The cs 235 quiz #2 from spring 2007. The quiz covers various topics including truth tables, while-loops, functions, and lists. It includes questions on completing a truth table, writing while-loops to find sums and count occurrences, rewriting loops using python's post-test idiom, and writing functions for dice rolls and list manipulations.

Typology: Quizzes

Pre 2010

Uploaded on 08/16/2009

koofers-user-15a
koofers-user-15a 🇺🇸

10 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
CS 235 Quiz #2 Pag e 1 of 5
COSC 235 Spring 2007
Quiz #3
Please read each question carefully and be sure to give complete answers. Good luck!
1. (1 pt.) What is your name? ___________________________________________
2. (8 pts.) Complete the following truth table:
p
q
r
p and q
not r
(p and q) or (not r)
T
T
T
T
T
F
T
F
T
T
F
F
F
T
T
F
T
F
F
F
T
F
F
F
3. (12 pts.) Write a while-loop to do each of the following:
a) Find the sum of the first n counting numbers: 1 + 2 + 3+ … + n
b) Find the sum of the first n even numbers: 2 + 4 + 6 + … + 2n
c) Count how many times 5 can be subtracted from a number x before the number is
negative.
pf3
pf4
pf5

Partial preview of the text

Download CS 235 Quiz #2: Truth Tables, Loops, Functions, and Lists and more Quizzes Computer Science in PDF only on Docsity!

COSC 235 Spring 2007

Quiz

Please read each question carefully and be sure to give complete answers. Good luck!

  1. (1 pt.) What is your name? ___________________________________________
  2. (8 pts.) Complete the following truth table: p q r p and q not r (p and q) or (not r) T T T T T F T F T T F F F T T F T F F F T F F F
  3. (12 pts.) Write a while- loop to do each of the following: a) Find the sum of the first n counting numbers: 1 + 2 + 3+ … + n b) Find the sum of the first n even numbers: 2 + 4 + 6 + … + 2n c) Count how many times 5 can be subtracted from a number x before the number is negative.
  1. (8 pts) Rewrite the following pre-test loop using Python’s “post-test” loop idiom. cash = - 1 while cash < 0: cash = input(“Enter a positive number: “) if cash < 0: print “Try again”
  2. (16 pts.) Write two functions die() and dice() that model a pair of dice. die() should generate a random number, from 1 to 6 inclusive for the value of a roll of a single die. dice() should use die() to generate a pair of random number corresponding to the roll of a pair of dice. It should return the values generated as a pair and should display the values, their sum, and, if appropriate a message “You rolled doubles!”. For example: >> d1, d2 = dice() Your rolled doubles. You rolled 6 and 6, a total of 12. >> d1, d2 = dice() You rolled 3 and 5, a total of 8. Note: This function both returns and displays values!
  1. (8 pts.) The Fibonacci sequence starts 1, 1, 2, 3, 5, 8, …. That is, each number is the sum of the two previous numbers. Thus, thee next number is 5 + 8 or 13. Write a function that returns the n th Fibonacci number. For example, fib(6) would return 8. The code on the next page is an incomplete class definition for a class Tests that records the test grades for a course. Use this code to answer the following questions.
  2. (3 pts.) In the appropriate space, add a docstring of your choosing to the class.
  3. (8 pts.) In the appropriate space, write a method average that calculates and returns the grade average for the tests taken in a course. Your code should return 0 in no tests have been recorded and the arithmetic mean otherwise.
  4. (12 pts) In the space below the code, give code to a) create two instances of tests, COSC235 and MATH b) recorded the grades 98, 92, and 96 for COSC c) recording the grades 90 and 92 for MATH d) display the second recorded grade for COSC e) display the average for each course f) display the highest grade recorded for MATH

Attached is the code for the preceding problems

class definition

class tests: ##constructor def init(self): self.grades = []

record a grade

def record(self, score): self.grades.append(score)

return a grade based on it position in the list

def report(self, number): return self.grades[number - 1]

return the current average grade

utilitiy function

def listMax(self, lst): x = lst[0] for i in lst: if x < i: x = i return x

find the highest grade

def maxGrade(self): return self.listMax(self.grades) Pledged: ________________________________________________________________