CSCI 110

    Home | | Course Schedule | | Assignments | | Lecture Notes

    Solutions 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 answer
    
    What 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 myResult
    
    What 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.
    Answer:

    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:

      hundredSum = sumOfIntegers(100)


    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.gpa
    
    b) 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:

    DecimalSigned magnitude1's complement2's complement
    22000101100001011000010110
    -31100111111110000011100001
    -22100101101110100111101010
    49001100010011000100110001


    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:

    abc
    000
    011
    101
    110

    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: