Old version
This is the CS 111 site as it appeared on May 10, 2018.
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 pre-lecture videos and lecture notes in the Lectures section of the course’s Blackboard site.
Exam details
-
The exam will be held from 6:30-7:30 p.m. on Wednesday, April 4, in the following locations:
-
We 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 open-response ones. However, these questions will not be multiple-choice. 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 pre-lecture 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 open-response 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 trial-and-error 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 (cs111-staff@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 2-bit greater-than function. Your function should take 4 bits of input,
x1
,x0
,y1
andy0
, and produce a true output if and only if the two-bit numberx1
x0
is greater than the two-bit numbery1
y0
. -
Write a Hmmm assembly-language 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 assembly-language 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 interest-bearing 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. -
A two-part problem:
-
First, write a function
num_appearances(substring, s)
that returns the number of appearances of the specifiedsubstring
(which you may assume is of length 2) in the strings
. Hint: Use an index-based loop. -
Next, write a function
most_common_pair(s)
that returns the two-character string ins
that appears most often as a substring withins
. For example,most_common_pair('alphabetical')
should returnal
. Ties may be broken arbitrarily. Hint: Usenum_appearances
as a helper function. In addition,most_common_pair
will need its own index-based 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.