CSCI 110
Home | | Course Schedule | | Assignments | | Lecture NotesSolutions to exam 2 review problems
Problem 1:Given the function definition: def Mystery( someVal): answer = 0 if someVal > 2.0: answer = 3.0 * someVal else: answer = 0.0 return answerWhat is the value is assigned to x after execution of the following statement?
x = Mystery(2.5);
Answer: x = 7.5
Problem 2:
Consider the following program:
def Demo( theList, answer): theList[0] = theList[0]*2 answer = theList[0] + 3.5 myList = [20] myResult = 4.8 Demo(myList, myResult) print myList print myResultWhat is printed out?
Answer: [40] 4.8
Problem 3: a)Write a function,
def sumOfIntegers( n ):that returns the sum of all integers from one to n.
def sumOfIntegers(n): answer = 0 for i in range(1, n+1): answer = answer + i return answer
b) Write a Python statement to compute the sum of integers from 1 to 100 using the above function and store the answer in the variable, hundredSum.
Answer:
4) What is the output of the following program?
#program: review.py import sys def reverse(word, n): if n < 0: print else: sys.stdout.write(word[n]) reverse(word, n-1) myWord = "back" reverse(myWord, len(myWord) -1)
Answer: kcab
5) a) Write a class, Student, that has three properties, gradYear, name and gpa.
The class should hava a constructor function that sets the values of these properties
when the object is created.
The class should have a function, printInfo( ), that prints out the values of
the students name, graduation year and GPA.
Answer:
class Student: def __init__(self, my_name, graduationYear, my_gpa): self.name = my_name self.gradYear = graduationYear self.gpa = my_gpa def printInfo(self): print self.name print self. gradYear print self.gpab) Write Python code to creat a Student object, setting the name to "Jane Doe", the graduation year to 2010 and the gpa to 3.75. Invoke the printInfo( ) function for this object to print out the student's information.
Answer:
aStudent = Student("Jane Doe", 2010, 2.75) aStudent.printInfo( )
Problem 6: a) Convert to binary
237 = 11101101
45 = 00101101
63 = 00111111
b) Convert to hexadecimal
237 = ED
45 = 2D
63 = 3F
Problem 7: Convert to signed magnitude, 1's complement, and 2's complement:
Decimal | Signed magnitude | 1's complement | 2's complement |
22 | 00010110 | 00010110 | 00010110 |
-31 | 10011111 | 11100000 | 11100001 |
-22 | 10010110 | 11101001 | 11101010 |
49 | 00110001 | 00110001 | 00110001 |
Problem 8: Add the following two binary numbers:
Binary | Decimal |
01110001 | 113 |
+ 00011100 | + 28 |
= 10001101 | = 141 |
Problem 9: draw a circuit that computes the truth table:
a | b | c |
0 | 0 | 0 |
0 | 1 | 1 |
1 | 0 | 1 |
1 | 1 | 0 |
There are several possible solutions. The simplest uses an exclusive or (XOR) gate.
Another possible solution uses AND and OR gates and Inverters.
Problem 10: Write an expression for the output of the given circuit. Draw the truth table: