Midterm Flashcards
What are the 5 criteria that make an algorithm good?
1) Well-ordered
2) Unambiguous: Clear/precise directions for the computer
3)Effectively Computable
4)Produces a Result
5)Finite Amount of time/Halts (has to stop)
When using a while loop to so something n times, what are the three things you should remember to do in order to make the loop work properly?
1) Initialize counter
2) Test counter
3) Increment counter
i = 0
while i < n:
# do something
i = i + 1
When using functions in a python program, what do we mean by Modular Design?
Programs are designed by breaking a problem into smaller problems and solving the smaller problems first
In Python, what is the difference between the ‘=’ operator and the ‘==’ operator?
= is for assigning a value to a variable
== is for testing equality
Given the following parallel lists of movie titles and years they were released:
movieTitle = [‘Moonlight’, ‘Spotlight’, ‘Birdman’, ‘12 Years a Slave’, ‘Argo’]
yearList = [2016, 2015, 2014, 2013, 2012]
* Write a line of Python code to print the year that Birdman was released.
print(“Birdman was released in “, yearList[2])
Suppose you are writing a function to create an output file to store a list of customers who are
interested in purchasing a particular product. Write a line of python code to open a file called
customers.txt so that you can write to it.
outfile = open(‘customers.txt’, ‘w’)
Write a line of Python code to execute the following mathematical assignment. Note: Python has a
square root function that you can use here sqrt().
d = sqrt(y2-y1)2 + (x2-x1)2)
What does it mean that an algorithm needs to halt? Illustrate your answer by a simple example:
It should not go into an infinite loop.
Example:
i=0
while(i < 100):
print(i)
Briefly describe the difference between a syntax error and a run-time error:
-Syntax error causes the program to not compile.
-Run-time error occurs as the program is running.
Briefly describe when it is preferred to use a ‘while loop’ and when it is preferred to use a ‘for
loop’:
-Use a while loop when you have more than one condition that will keep the loop going.
-Use a for loop when you want to iterate through a list, a file or a certain number of times
What is the difference between a boolean variable and an int variable?
-A boolen takes a value of true or false
-An int stores an integer
“Functions can only take one input parameter. It is illegal to define functions with multiple input
parameters or with no input parameters”. Briefly discuss why this statement is false.
Because functions can take as many input parameters as you need to make them execute correctly.
Write an if statement that would check if variable k is greater than or equal to 100 AND less than 200.
if k >= 100 and k < 200: