Download Exam 1

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Computer Science 101
Survey of Computer Science
Exam 1
Fall 2009
Name:
Pledged:
Note: You should not discuss this exam, in any way, with students who have not
taken the exam.
1. (40 points) Complete the following:
a. An important difference between pseudocode and a “real” programming language
is
b. The term
means that all intended computing
agents will interpret each step of the algorithm in exactly the same way.
c. Using the set of pseudocode statements covered in class, give a statement that will
allow the user to input a value for the variable age.
d. Repeat part c, but give a Python statement.
e. Write a pseudocode statement that assigns to the variable newValue, the sum of the
values of the variables firstValue and secondValue in the case that the value of
firstValue is larger than 100, and assigns to newValue the product of firstValue
and secondValue otherwise.
f. Repeat part g, but with Python.
g. Give the value of the following expressions as computed in Python:
37 / 4
37 - 4
37*4
37%4
h. A compiler is a computer program whose purpose is to
i. When a computer program runs to completion, but gives the wrong result, we say
the program has a/an
error.
j. In Python, to make use of one of the Python libraries like math or random, we
must use a/an
statement.
k. In Python, the symbol(s)
would be used instead of “≠”.
l. Both selection sort and bubble sort make passes through the list. We usually expect
sort to make many more exchanges than
sort.
m. A recursive algorithm is one that
One recursive algorithm that we have studied is
n. In the context of computer programming, the term
refers to what is supposed to happen when the various statements are executed,
while the term
statements in the language.
refers to the formal rules for legal
2. (15 points) a. Show what the following list of data would look like after the first pass
of the Selection Sort algorithm:
1
2
3
4
5
19
18
1
2
7
6
7
8
9
10
11
12
10
39
29
3
26
40
22
12
17
4
5
6
7
8
9
10
11
12
Pass 1:
3
b. Beginning with the original data, show what the list would like after the first pass of
the Selection Sort algorithm.
Pass 1:
1
2
3
4
5
6
7
8
9
10
11
12
c. Beginning with the original data, indicate which element would be chosen for the
Quick Sort pivot element using the scheme covered in the course, and show exactly
what the list would look like after the first partitioning pass in executing the Quick
Sort algorithm.
1
2
3
4
19
18
7
10
5
39
6
7
8
9
10
11
29
3
26
40
22
12
9
10
12
17
Pivot element:
After partitioning:
1
2
3
4
5
6
7
8
11
12
3 .(20 points) Consider the following algorithm given in pseudocode. For Step 5, we
mean that we should divide the whole number F into the whole number N and R
should become the remainder that is left after this division. For example, if N=20 and
F= 8, then the quotient of N divided by F is 2 with a remainder of 4; so R would
become 4.
Step
Operation
1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
Get N
Set P to 1
Set F to 2
While F2  N do
Set R to the remainder of N divided by F
If R = 0 then
Set P to 0
Set F to F + 1
End-of-loop
Print P
Stop
a. Step through the algorithm carefully, assuming that the user inputs a value of 15 at
Step 1. In the table below show the step number of the step being executed and on
that line, show the value of any variable that is “Set” at that step. In the output
column, show the output given when step 10 is executed. Be sure to include steps
like step 4 and 6 even if they don’t give new values to variables
Step
1
N
15
P
F
R
Output
b. Convert the pseudocode algorithm of part a) into Python.
4. (15 points) Using the pseudocode covered in class or Python-like statements
a. Write an algorithm that has the user enter values for variables num1 and num2.
Then the algorithm should assign to the variable product the product of num1 and
num2. Finally the value of product should be displayed.
b. Write an algorithm (using a loop) that has the user input a series of numbers (both
positive and negative) ending with a 0. The algorithm should display the number
that has the largest square. For example, if the numbers are 5, -6, 7, -3, 0, the
program should output 7. If the numbers are 5, -6, -3, 4, 0, the program should
output -6. For partial credit, just have the program output the largest number.
Note: You should not use lists or lots of variables.
5. (10 points) For this problem, assume that myList is a Python list that already has 300
numbers in it.
a. Write a Python statement that will print out the smaller of the first two numbers in
the list.
b. Write statements that will determine and print out the smallest of the numbers in
the list.