Comp 150 Midterm Study Guide

Midterm is Tuesday, June 9
Some sample exercises (answers and python questions coming soon!)
Comp 150 exam 1 study guide
 
 
Sections of Computer Science Illuminated:
Section 2.2, 3.2:
    Binary representation of numbers
Section 3.3:
    ASCII representation of characters (omit Unicode)
Section 4.2-4.5:
    Basics of gates, truth-tables, and circuits
Section 5.2:
    Basics of CPU operation
Section 7.3, 7.4
    We'll use PIP machine language (generally simpler than PEP/7)
    Assembler
    Encoding instructions
    Some PIP or PEP/7 programs
   
Topics:
    binary numbers
    PIP code (you will be given the summary in the Lab 5 handout)
    Logic & Gates
 

Sections of Python: 

There will be a couple simple python functions to write on the exam. You will also be asked to describe the OUTPUT of some simple python functions. 
 
For PIP, you may be asked to write assembly code that would leave a desired value in the Accumulator (eg 3*x+y). You may also be asked to describe the output generated by a very simple program.
 
=======================================================================
 
1. What is the value of s after execution of the following python code:
 
n=1
s=1
while n<7:
   s = s + n*n
   n = n+2
   
 
2. What is the value of L after execution of the following python code?  Note L is a list.
 
n = 0
L = []
while n < 5:
    L = L + [2*n]
    n = n + 1
 
   
3. What is printed by the following:
 
for i in range(1,5):
    print range(0,i)
   
 
4. What is string t after the following?
t = ""
s = "foobar"
for i in range(len(s)):
    t = t + s[i]
   

5. Write PIP code to calculate 1+A*(2+B*(3+C))


6. Suppose the following PIP code is run. What will be in the accumulator at the end? (Hint: A is decremented by 1 each time through; B is increased by 2*A each time through.)

            LOD #5
            STO A
            LOD #10
            STO B
LOOP: ADD A
             ADD A    ;; yes, twice
             STO B
             LOD A
             SUB #1
             STO A
             JMZ DONE
             LOD B
             JMP LOOP
DONE:  HLT
     
 
7. Give the output of the following for all inputs.
 
x----+------+ 
     | NAND |--+
y----+------+  |
               +----+------+
                    | NAND |--output
z-------------------+------+
 
 
x  y  z  |  output
0  0  0  |  
0  0  1  |  
0  1  0  |  
0  1  1  |  
1  0  0  |  
1  0  1  |  
1  1  0  |  
1  1  1  |  
 
The truth table for one NAND gate is as follows:
 
x  y  output
0  0   1
0  1   1
1  0   1
1  1   0
 
   
 
8. Write a python function to multiply all the numbers in a list L:
 
def mulnums(L):
 
 
mulnums([2,3,7]) should return 42.
 
 
9. Suppose we define
    def isOdd(n):
       if n % 2 == 1: return True
       else: return False

    def filter(L):         # L is a list of numbers
       res = [ ]
       for x in L:
          if isOdd(x): res = res + [x]
       return res

What is returned by filter([1, 2, 4, 5, 7, 12, 16, 17])?