Study questions for Midterm Exam CS 115
Not guaranteed to be complete but good to work on!
These will not be collected, but you can certainly discuss
them with your classmates or your instructor.
The format of these questions is NOT always what you would see
on the test. We avoid open-ended, broad questions that would be very
hard to grade.
The test will contain some of: multiple choice questions (some),
fill in the blank with a word or two, short answer - a sentence or two,
what's wrong with this code?, what is the output of this code?, write a statement or
expression to do this.
Possibly there will be a test plan or truth table to fill out.
- Do you know the difference between RAM and a hard drive, and the units to measure capacity and speed?
- Write down three legal identifiers. Can you state why each one is legal?
Try to use all the rules of identifier creation.
Write three invalid identifiers; make each one invalid in a different way.
- What does 'case sensitive' mean? does it apply to Python?
-
Write an assignment statement that has the formula below stored in a variable called result. Use no more parentheses than needed.
a + b - 4e - f
2a
- Trace this code. That is, keep track of what is in memory
(Label it "RAM") and what is output (label it "shell").
Another thing to note: can you tell what type of data each variable
contains at each point in the program? Variables can change their type!
v1 = 15.7
v3 = int(3 * 5.2)
v2 = v1 + v3
print ("v2 =", v2, end=" ")
v3 = v2 - 5
print ("v3 =", v3, sep="")
v1 = 8
print ("v1 =", v1)
v2 = 3 * v1
print ("v2 =", v2)
- What is a block of code? How do you indicate it in Python?
What kind of statements use blocks?
- Calculate the values of the expressions. Indicate what type the answer is.
a. 5 + 4 * (3.2 + 4.5)
b. 7 % 5 + 12 // 7
c. 8 + 4.0 ** 3
- Could you answer these questions about the modulus operator? about the // operator?
about the ** operator? about the < operator? about the "or" operator?
- what type or types of data does it operate on?
- what type or types does it return?
- what does it do - what are its semantics?
- what is its precedence?
- What are the "three P's"?
- If you want to make a floating point number into an integer, how do
you do it? what's the syntax? What about vice versa?
- Why is this typecast redundant? x = str(input("what's your name?"))
- Why is this typecast redundant? x = float(1/2)
- How many times will each of these for loops repeat their body?
What values will the variable i go through?
- for i in range(10):
- for i in range(3, 10, 2):
- for i in range(2, 5):
- for i in [2,3,5,1,0]:
- Tautologies and Contradictions: Can you figure out if an expression is one or the other?
Which of these are tautologies? which are contradictions?
var > 10 or var < 20
var == 5 or 9
var < 20 and var > 100
- Write an expression logically equivalent to
not (a > b and c > d) which does not use the not operator. "logically equivalent"
means that when one expression is True, the other is, and the same with False. Hint: DeMorgan's Laws
- Describe the difference between integers and floating point numbers.
Which one does faster arithmetic? why?
What is a mantissa?
- What is type coercion? when does it happen? What is the output of this program always, regardless of the value of x?
x = 23
if x == 10 or 19:
print("right")
else:
print("no")
- What is an escaped character? Can you name 3 different ones?
What does each one mean? (Hint: "\n" is a newline character, that's one)
-
What are the values of a and b after this code?
y = 2.3
a = int (y * 2)
b = y % a
-
Write a complete program that performs the interaction given. The user enters a number of dollars and the program tells how many pennies that is. Use at least one variable and two assignment statements.
-
What's the difference between syntax and semantics?
How do you find syntax errors? semantics errors?
- Who is Guido van Rossum?
- Who was George Boole?
- List the parts of the IDE that you have used so far. What did you use each one for?
- Comments - how do you write them? why do you write them? who reads them?
-
Name the different data types in Python. How are they different? what are they used for? What do constants of each type look like?
- Describe precisely the actions that happen when an assignment statement
is executed. That is, what happens first? second?
- Describe an accumulator - how is it different from an ordinary assignment statement?
- What is an "augmented assignment operator"? What are their precedences?
- How do you import a library? there are 3 different
forms of the statement. How do they differ? When do you
import a library? Hint: ONLY when you need it!
- Name some library functions. What type do they require for
arguments? what type do they return?
How do you call them? (what is the syntax if they return a
value? what if they do NOT return a value?)
- Describe pseudocode. When is it written? How is it written?
- You might see the special value None when?
- You have a program that will output "over the limit", "ok" or
"too slow" depending on the input of a speed (of a car). It should
output "over the limit" when the speed is greater than 70, "ok"
between 70 and 40 (inclusive), and "too slow" for speeds less than 40.
Write a test plan to test this program. Make sure you include
the boundary cases.
- What is regression testing? a test plan? What are the columns in a test plan?
- What is the output of this code?
num = 12.3456
print("The num is {:.3f}".format(num))
-
What is the output of this code with speed = 55?:
if speed < 40:
print("slow")
elif speed < 55:
print("legal")
else:
print("ticket")
Write a test plan for this code.
- Know the difference between logical and relational operators.
- Put parentheses in the expression below so that they show
what order the operations will be done in:
a < 0 and b >= 5 + y or z > 3 and q == p
- What is the difference in operation between a "=" operator
and a "==" operator? When do you use one and when do you use the other?
- Write a design for this problem: Read in three numbers and
output the ones that are larger than their average. This should be
done with pseudocode, NOT code! no Python at all,
just English. Number the steps if you like.
- Write the code for the design in the previous problem.
- Write a program that will input a number (float) and tell if it is a
perfect square or not. Numbers are perfect squares if their square root
is an integer. 25 is a perfect square because 5 squared is 25 and 25
is an integer. 2 is not a perfect square because 1.41421 squared is
2 and it is not an integer.
- What's the difference between an interpreter and a compiler?
Which one will find as many syntax errors as possible when you run it?
Which one must be present to do translation every time you run
the program?
- What's an overloaded symbol? Name one and its various meanings.
- Comparisons
- comparing ints and floats
- comparing floats - what's the danger?
- how do you compare strings? how is ASCII involved? What's Unicode?
- What's the difference between a control structure and a data structure?
- Name the basic control structures.
- What's dead code?
- What causes an infinite loop?
- What are the guarantees true for EACH control structure? What is the guarantee that
is true for ALL control structures?
- Trace this code:
x = 9 // 2 # Line 1
y = x + 4 # Line 2
print(x,y) # Line 3
if x > 4: # Line 4
y = 17 # Line 5
if y > 10: # Line 6
print("ok") # Line 7
else: # Line 8
print("no") # Line 9
print("yes") # Line 10