COSC 235B Test 1: Computer Science Concepts and Python Programming - Prof. David A. Sykes, Exams of Computer Science

A computer science test from cosc 235b, held on september 29, 2008. The test covers various computer science concepts, including algorithms, variables, data types, and python programming. It includes multiple-choice questions, a programming assignment, and a problem to be solved by completing and debugging a python script.

Typology: Exams

Pre 2010

Uploaded on 08/17/2009

koofers-user-ph4
koofers-user-ph4 🇺🇸

9 documents

1 / 4

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
COSC$235B$ $ September$29,$2008$
Test%1:%Chapters%1‐4%
Name:%______________________________% Pledged:%_________________________________%
Answer$all$questions$completely.$
1. Match$the$best$answer.$(There$are$two$extra$answers$in$the$right$column.)$
$
Computer$science$$
$
A$step‐by‐step$process$for$achieving$a$desired$result$
$
A$problem$for$which$known$algorithms$to$solve$
them$take$too$long$to$be$of$practical$value$
$
Notations$for$expressing$computations$in$an$exact$
and$unambiguous$way$
$
A$set$of$values$and$operations$on$those$values$
$
In$Python,$must$start$with$a$letter$or$an$underscore$
$
Reflects$the$structure$of$constructs$in$a$language$
$
Reflects$the$meaning$of$constructs$in$a$language$
$
Intended$for$human$readers$of$a$program$
$
Used$to$give$a$name$to$a$value$so$it$can$be$referred$
to$at$other$points$in$a$program$
A. variable$
B. type$
C. the$study$of$what$
can$be$computed.$
D. syntax$
E. semantics$
F. programming$
language$
G. intractable$
H. identifier$
I. definite$loop$
J. CPU$
K. comments$
L. algorithm$
M. $
2. Complete$the$program$given$below$according$to$what$the$comments$specify$
import string
def main():
# Get a name
name = raw_input("Enter your full name: ")
# Print the name in the form First-initial. Laste.g., B. Dunlap
main()
pf3
pf4

Partial preview of the text

Download COSC 235B Test 1: Computer Science Concepts and Python Programming - Prof. David A. Sykes and more Exams Computer Science in PDF only on Docsity!

Test 1: Chapters 1‐ 4

Name: ______________________________ Pledged: _________________________________

Answer all questions completely.

1. Match the best answer. (There are two extra answers in the right column.)

Computer science

A step‐by‐step process for achieving a desired result

A problem for which known algorithms to solve

them take too long to be of practical value

Notations for expressing computations in an exact

and unambiguous way

A set of values and operations on those values

In Python, must start with a letter or an underscore

Reflects the structure of constructs in a language

Reflects the meaning of constructs in a language

Intended for human readers of a program

Used to give a name to a value so it can be referred

to at other points in a program

A. variable

B. type

C. the study of what

can be computed.

D. syntax

E. semantics

F. programming

language

G. intractable

H. identifier

I. definite loop

J. CPU

K. comments

L. algorithm

M.

2. Complete the program given below according to what the comments specify

import string def main():

Get a name

name = raw_input("Enter your full name: ")

Print the name in the form First-initial. Last —e.g., B. Dunlap

main()

3. When the program shown below is executed, the result is as shown in the Python shell

window. Explain what the problem is and mark up the program to show how to fix the

problem.

Investment calculator

def main(): investment = input("Enter the initial investment: ") apr = input("Enter the APR: ") years = input("Enter the term of the investment (years): ")

Simulate the passing of years and accumulated interest

balance = float(investment) for year in range(years): balance = balance + (balance * apr) print "End of Year %2d: $%7.2" % (year, balance)

Display the ending amount of the investment

print "After %d years, an initial investment of $%0.2f at %0.1f"+ "interest yields $%0.2f." % (years, investment, apr*100, balance) main()

5. Using well chosen variable names and reasonable comments, write a program^1 to:

a. Prompt a user for the name N of a text file,

b. Prompt for some text T , and then

c. Display the number of lines in the text file named N and the number of occurrences of T

in the file. [Bonus: make the search for T case insensitive.]

For example, if a file foo.txt contains the text in

the box at the right and a user entered foo.txt

for N and pick for T , then the program would

determine there are 3 lines and 6 occurrences

of pick in the file.

The function count() should prove useful.

Function Meaning count(s, sub) Count the number of occurrences of sub in s (^1) By program I mean everything that would have to be typed into a .py file in order for Python to successfully run this code. This includes import statements and a call to main() if a function main() is defined. Peter Piper picked a peck of pickled peppers; If Peter Piper picked a peck of pickled peppers, How many pickled peppers did Peter Piper pick?