Exam 1

advertisement
Computer Science 101
Survey of Computer Science
1. (40 points) Complete the following:
a. A step by step procedure for solving a general class of problems is called a/an
algorithm
b. The term for the formal grammatical rules for a programming language is
syntax
. The term
semantics
refers to the action
that the computer would take when it executes the various types of programming
statements.
c. A computer program that converts a program written in some programming
language into the machine language for a specific type of computer is called a/an
compiler
d. Circle the items below that WOULD NOT be legal Python variable names:
i. R2D2 ii. pay Rate iii. 2BOr!2B iv. payRate v. else
e. Give the value of the following expressions as computed in Python:
37 * 8
296
37 / 8 4
37 - 8
29
37 % 8 5
f. Give a Python statement that stores in the variable price a value entered by the user.
price = input(“Enter price: “)
g. Give a Python statement that displays the message “Too high!” if the value of price
is more than 100 and displays the message “Fair enough!” otherwise.
if price > 100 :
print (“Too high!”)
else:
print (“Fair enough!”)
h. Circle the ones of the following that WOULD be legal in the given number system
decimal:
238
11011
463
2C4D
R2D2
binary:
238
11011
463
2C4D
R2D2
octal:
238
11011
463
2C4D
R2D2
hexadecimal: 238
11011
463
2C4D
R2D2
i. Suppose we have 3 computer files of sizes 100Kb, 100Mb, 1Gb. List these file
sizes in increasing order:
100K 100Mb 1Gb
j. What is the decimal value of the largest number that can be expressed in binary
with 8 bits?
255
k. Give three pieces of information that can be found in the header section of a BMP
picture file.
signature, file size, width, height, offset to picture data, etc.
l. How many bytes of data are required for each pixel in a BMP file?
2. (15 points) Do the following conversions:
a. Convert the decimal number 184 to binary
184 = 128 + 32 + 16 + 8 yields 10111000
b. Convert the octal number 654 to binary
Octal 654 yields 110 101 100 so we have 110101100
c. Convert the hexadecimal number B8 to decimal
Hexadecimal B8 in decimal would be 11*16 + 8 or 184
3
3. (15 points) Convert the binary number 10110 to
a. Decimal: 10110 yields 24 + 22 +21 = 22
b. Octal: 10110 in triples is 010 110 yielding 26
c. Hexadecimal: 10110 in four bit sections is 0001 0110 or 16
4. (15 points) Write a Python function called addNums that lets the user input 50
numbers. The function should output the sum of the numbers and tell how many of
the numbers were larger than 100. Write your code neatly and make the exact syntax
(indentations, etc.) very clear. Partial credit if you just get the sum part correct.
5. (10 points) Below is the Python code for a function called perfect. For each part, you
should assume that the user inputs the given number and then step through the
execution of the function with that input. Keep careful track of the value of each
variable and give the exact output that would be produced by the function.
a. User inputs the number 8:
num: 8
sum: 1 3 7
index: 2 3 4 5 6 7 8
output: Not Perfect!
b. User inputs the number 6:
num: 6
sum: 1 3 6
index: 2 3 4 5 6
output: Perfect!
c. User inputs the number 28:
num: 28
sum: 1 3 7 14 28
index: 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
output: Perfect!
d. User inputs the number 26:
num: 26
sum: 1 3 16
index: 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
output: Not Perfect!
6. (5 points) Suppose the following picture is stored in a BMP file with width and
height both 100 pixels; so each of the maroon and blue squares is 50 pixels by 50
pixels.
Suppose we change the values in the header so that the width is doubled and the
height is cut in half, but all of the color data is left in place. Describe in terms of
maroon and blue rectangles what the picture would like when we viewed it with
JES or Paint.NET.
We would now have rectangles that the same width and half the height of the
squares. Across the bottom we would have blue, maroon, blue, maroon. Above that
would be a row of maroon, blue, maroon, blue.
Download