Midterm Flashcards

1
Q

What are the 5 criteria that make an algorithm good?

A

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)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

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?

A

1) Initialize counter
2) Test counter
3) Increment counter

i = 0
while i < n:
# do something
i = i + 1

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

When using functions in a python program, what do we mean by Modular Design?

A

Programs are designed by breaking a problem into smaller problems and solving the smaller problems first

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

In Python, what is the difference between the ‘=’ operator and the ‘==’ operator?

A

= is for assigning a value to a variable
== is for testing equality

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

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.

A

print(“Birdman was released in “, yearList[2])

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

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.

A

outfile = open(‘customers.txt’, ‘w’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

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().

A

d = sqrt(y2-y1)2 + (x2-x1)2)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What does it mean that an algorithm needs to halt? Illustrate your answer by a simple example:

A

It should not go into an infinite loop.

Example:
i=0
while(i < 100):
print(i)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Briefly describe the difference between a syntax error and a run-time error:

A

-Syntax error causes the program to not compile.
-Run-time error occurs as the program is running.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Briefly describe when it is preferred to use a ‘while loop’ and when it is preferred to use a ‘for
loop’:

A

-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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is the difference between a boolean variable and an int variable?

A

-A boolen takes a value of true or false
-An int stores an integer

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

“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.

A

Because functions can take as many input parameters as you need to make them execute correctly.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Write an if statement that would check if variable k is greater than or equal to 100 AND less than 200.

A

if k >= 100 and k < 200:

How well did you know this?
1
Not at all
2
3
4
5
Perfectly