The exam will focus on the topics covered in lecture from the material on gates and circuits up to and including the material on 2-D lists.
Remember that you can access all of the relevant pre-lecture videos in the Lectures section of the course’s Blackboard site, and that all of the relevant lecture notes are in the coursepack.
The exam will be held from 6:30-7:45 p.m. on Wednesday, November 13, in the same locations as Midterm 1:
We will not 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 75 minutes for the exam.
You may not use any materials during the exam. In particular, you should turn off and put away cell phones, watches, and other devices.
You should either use a pencil (recommended) or a pen with blue or black ink.
Once you leave the exam room, you may not return, so please use the restroom ahead of time.
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.
One thing that we highly recommend is to review the pre-lecture videos 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 Top Hat questions and the open-response ones.
the 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.)
the following questions from CodingBat:
Note: Some of these questions refer to an array, which is another name for a list.
When working on practice problems, try to come up with your answers on paper, rather than through a trial-and-error approach on Spyder 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
).
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.
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
and
y0
, and produce a true output if and only
if the two-bit number
x1
x0
is greater
than the two-bit number
y1
y0
.
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 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 reachThe 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 as
principal = principal * (1 + rate)
Hint: You don’t know ahead of time how many repetitions of the loop will be needed. Given that fact, what type of loop should you use?
Write a Python function stars(n)
where n
is a positive integer.
It should print n
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 use print('*', end= '')
to print one asterisk
at a time while remaining on the same line, and an empty print()
to go down to the next line.
Write a function all_perfect(lst)
that takes a list of numbers
lst
and returns True
if all of the numbers are 100 and False
otherwise. Use a loop.
Write a function index_nearest(n, lst)
that takes a number n
and a list of numbers lst
and returns the index of the element
in lst
whose value is closest to n
. Use one or more loops.
Write a function common_chars(s1, s2)
that takes in two
arbitrary strings s1
and s2
and returns a new string
containing only the characters that s1
and s2
have “in common”
– i.e., characters from positions in which both s1
and s2
have the same character. The characters in the returned string
should be in the same order with respect to each other as they
were in the original string. For example, common_chars('pretty',
'pleased')
should return 'pe'
because the only positions at
which the strings have the same character are position 0 ('p'
)
and position 2 ('e'
). Use a loop.
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 a 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 c = b[:] a[3] = 5 b[1] = 7 print('a is', a) print('b is', b) print('c is', c)
In addition, write a few sentences that refer to your diagram and that explain the result of the program.
Draw memory diagrams that demonstrate the differences between the following three Python programs:
### Program 1 ### def foo(a): a = 2 * a b = [1, 2, 3] foo(b[1]) print('b is', b) ### Program 2 ### def bar(a, i): a[i] = 2 * a[i] b = [1, 2, 3] bar(b, 1) print('b is', b) ### Program 3 ### def boo(a): a = [1, 4, 6] b = [1, 2, 3] boo(b) print('b is', b)
For each program, you should include two diagrams:
one that shows what things look like in memory at the start of the function call – before it has made any changes
one that shows what things look like at the end of the function call, just before it returns.
Make sure to include two stack frames in each diagram: one for the global scope, and one for the function.
Write a function num_negatives(grid)
that takes a rectangular 2-D
list grid
and returns the number of negative numbers in the 2-D list.
For example:
>>> grid1 = [[-2, 3, -5], [4, -1, -9]] >>> num_negatives(grid1) result: 4
Write a function add_one
that takes an input grid
that is a
rectagular 2-D list of numbers. Your function should add 1 to each
element of grid
, but it should not return anything. For
example:
>>> grid2 = [[2, 3, 5, 8], [4, 1, 9, 2], [0, 2, 4, 6]] >>> add_one(grid2) >>> grid2 result: [[3, 4, 6, 9], [5, 2, 10, 3], [1, 3, 5, 7]]
An optional two-part challenge problem:
First, write a function num_appearances(substring, s)
that
returns the number of appearances of the specified substring
(which you may assume is of length 2) in the string s
.
Hint: Use an index-based loop.
Next, write a function most_common_pair(s)
that returns the
two-character string in s
that appears most often as a
substring within s
. For example,
most_common_pair('alphabetical')
should return al
. Ties
may be broken arbitrarily. Hint: Use num_appearances
as a
helper function. In addition, most_common_pair
will need its
own index-based loop.
Last updated on October 31, 2024.