Midterm 2 Information
Material covered
The exam will focus on the topics covered in lecture from the material on gates and circuits up to and including the material on references. The use of the stack and recursion in the context of assembly language will not be included on the exam.
Remember that you can access all of the relevant prelecture videos and lecture notes in the Lectures section of the course’s Blackboard site.
Exam details

The exam will be held from 6:307:30 p.m. on Wednesday, November 15, in the same locations as the first exam:
 A1 lecture (MWF 10:1011:00): Law Auditorium
 B1 lecture (MWF 12:201:10):
 students with last names AK: Morse Auditorium
 students with last names LZ: CAS 522
 C1 lecture (TuTh 11:0012:15): Morse Auditorium
 students with special accommodations: MCS 148

The A1 and B1 lectures will meet for lecture on the day of the midterm.

You must bring your BU ID to the exam, so that we can check it when you turn in your exam.

You will have the full 60 minutes for the exam.

We will provide a page with the full list of Hmmm instructions that you can use during the exam.

You may not use any other materials during the exam. In particular, you should turn off cell phones and other electronic devices and put them away.

The exam will include questions similar to the ones posed in lecture–both the Top Hat questions and the openresponse ones. However, these questions will not be multiplechoice. You will need to determine and write the answer without a list of options to choose from.

In addition, there will be questions that ask you to write a function, a short program, or a simple circuit, similar to the problems from the homework.
Preparing for the exam

One way to prepare is to review the prelecture materials and lecture notes and make a summary of the key points in your own words. “Boiling down” the material in this way is a great way to ensure that you really understand the key concepts.

We also encourage you to do practice problems. Options include:

redoing the questions posed in lecture–both the clicker questions and the openresponse ones. The online lecture notes include both the questions and–on the next slide–the answers.

the following questions from CodingBat:
Note: Some of these questions refer to an array, which is another name for a list.

the additional practice problems found below. (Note: Solutions to these additional practice problems will be posted under Other Content on Blackboard as we get closer to the exam.)
When working on practice problems, try to come up with your answers on paper, rather than through a trialanderror approach on IDLE or in another programming environment. This will be give you an experience that is similar to the one that you will have during the exam.


Feel free to post questions on Piazza (using the
midterm_exam2
tag) or to email the course account (cs111staff@cs.bu.edu
).
Additional practice problems
Solutions to these problems will be posted under Other Content on Blackboard as we get closer to the exam.

Write the complete truth table for the boolean function XYZ + XYZ.

Given the following truth table, perform the minterm expansion for the function
f
. Don’t simplify!x y f(x,y)  0 0 1 0 1 0 1 0 1 1 1 0

Design the circuit for the function from the previous problem.

Add the following two binary numbers:
1 1 0 1 1 0 1 1 1 

Multiply the following two binary numbers:
1 0 1 1 1 1 1 0 1 

Design a truth table, minterm formula, and circuit that will implement a 2bit greaterthan function. Your function should take 4 bits of input,
x_{1}
,x_{0}
,y_{1}
andy_{0}
, and produce a true output if and only if the twobit numberx_{1}
x_{0}
is greater than the twobit numbery_{1}
y_{0}
. 
Write a Hmmm assemblylanguage program that gets two positive integers from the user, subtracts the second integer from the first, and writes out the square of the result.

Write a Hmmm assemblylanguage program that uses a loop that allows the user to enter one or more integers. When the user enters a zero, the program should stop looping and write out two counts: how many of the numbers were positive, and how many were negative. Don’t count the zero.

Write a Hmmm assembly language program that takes in a positive integer as input and calls a separate function that many times. The function should simply write out the number 111. You must use
call
andjumpr
instructions to implement your function. 
Write a Python function
years_needed
that takes three inputs:principal
, which is the initial amount of money deposited in an interestbearing accountrate
, which is the annual interest rate in decimal formtarget
, which is the final value that the investor wants to reach
The function should use a loop to determine the number of years of compounded annual interest that are needed for the investment to reach or exceed the specified
target
. Note: After each year, the new principal is computed asprincipal = principal * (1 + rate)

Write a Python function
find(ch, s)
that returns the index of the first occurrence of a characterch
in a strings
, or −1 if the character is not found. You may assume thatch
is a single character. Use a loop. 
Write a Python function
count_vowels(s)
that counts and returns the number of vowels in a string. Use a loop. 
Write a Python function
stars(n)
wheren
is a positive integer. It should printn
lines of stars with 1 star on first line, 2 stars on second line, and so forth. For example,stars(4)
should print* ** *** ****
Use nested loops. You are not allowed to use the
*
operator (*). You should useprint('*', end= '')
to print a single asterisk at a time while remaining on the same line, and an emptyprint()
to go down to the next line. 
Write a function
all_perfect(lst)
that takes a list of numberslst
and returnsTrue
if all of the numbers are 100 andFalse
otherwise. Use a loop. 
Write a function
index_nearest(n, lst)
that takes a numbern
and a list of numberslst
and returns the index of the element inlst
whose value is closest ton
. Use one or more loops. 
Write a function
most_common_pair(s)
that returns the twocharacter string ins
that appears most often as a substring withins
. For example,most_common_pair('alphabetical')
should returnal
. Ties may be broken arbitrarily. Hint: Use an indexbased loop. 
Optional challenge: Write a function
longest_dna(s)
that takes a strings
and returns the longest substring that consists only of the characters ‘A’, ‘C’, ‘G’, and ‘T’. For example,longest_dna('ACCGXXCXXGTTACTGGGCXTTGT')
should return
'GTTACTGGGC'
. Ties may be broken arbitrarily. 
What is printed by the following Python program?
def loopy(x, y): print('starting loopy:', x, y) while x < y: x += 1 y = 2 print('after loop:', x, y) return x x = 1 y = 8 y = loopy(x, y) print('after first call:', x, y) loopy(y, x) print('after second call:', x, y)
Hint: Use two different tables – one for the global scope and one for
loopy
– to keep track of the values of the variables. 
Draw one or more memory diagram like the ones we have used in lecture to illustrate the execution of the following Python program:
a = [1, 2, 3, 4] b = a a[3] = 5 b[1] = 7 print('a is', a) print('b is', b)
In addition, write a few sentences that refer to your diagram(s) and that explain the result of the program.

Draw memory diagrams that demonstrate why we get different results from the following two Python programs:
### Program 1 ### def foo(a): a = 2 * a return b = [1, 2, 3] for i in range(len(b)): foo(b[i]) print('b is', b) ### Program 2 ### def bar(lst, i): lst[i] = 2 * lst[i] return b = [1, 2, 3] for i in range(len(b)): bar(b, i) print('b is', b)
In addition, write a few sentences that refer to your diagrams and that explain the difference in output.