Вы находитесь на странице: 1из 9

Spring Semester 09, Dr. Punch.

Exam #1 (2/19), form 1 A

Last name (printed):

First name (printed):

Directions:

a) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD
TO BEGIN.
b) You have 80 minutes to complete the exam (8:30-9:50 am)
c) This exam booklet contains 26 multiple choice questions, each weighted equally
(5 points) and one 20 point “write some code” question. Nine pages total
d) You may use one 8.5" x 11" note sheet during the exam. No other reference
materials or calculating devices may be used during the examination.
e) Questions will not be interpreted during the examination.
f) You should choose the single best alternative for each question, even if you
believe that a question is ambiguous or contains a typographic error.
g) Please fill in the requested information at the top of this exam booklet.
h) Use a #2 pencil to encode any information on the OMR form.
i) Please encode the following on the OMR form:
 Last name and first initial
 MSU PID
 Exam form (see the title of this page)
j) Please sign the OMR form.
k) Only answers recorded on your OMR form will be counted for credit.
l) Completely erase any responses on the OMR form that you wish to delete.
m) You must turn in this exam booklet and the OMR form when you have completed
the exam. When leaving, please be courteous to those still taking the exam.

Good luck.

Timing tip. A rate of 2.5 minute per multiple choice problem leaves 15 minutes to write
the 20 point program at the end

Key:
1 2 3 4 5 6 7 8 9 10
C E D A D A A D C A

11 12 13 14 15 16 17 18 19 20
D B D B B B A B E C

21 22 23 24 25 26
C A E C E C
Figure 1
1) Given user input of 9, what value does Line 3 in program Figure 1 print?
a) 1
b) 2
c) 3
d) 4
e) None of the above.
2) Given user input of 9, what value does Line 4 in program Figure 1 print?
a) 1
b) 2
c) 3
d) 4
e) None of the above.
3) Given user input of 7, what value does Line 3 in program Figure 1 print?
a) 1
b) 2
c) 3
d) 4
e) None of the above.
4) Given user input of 1, what value does Line 3 in program Figure 1 print?
a) 1
b) 2
c) 3
d) 4
e) None of the above.
5) If the else clause on Line 1 and Line 2 were removed, what effect would that
have on the program in Figure 1 on the input value 1?
a) no effect, program would give the same results
b) cnt would be larger
c) cnt would be smaller
d) the while loop would not end
e) None of the above.

6) Which of the following statements are true about Python’s for statement?
a) is used to iterate through sequences
b) Is more general than a Python while statement
c) is never used in conjunction with a Python range statement
d) All of the above
e) None of the above

7) What is meant by the phrase “half-open range” with respect to Python?


a) All ranges (range, slices) include the first but not the last element
b) All ranges (range, slices) include the last but not the first element
c) All ranges (range, slices) include both the first and the last element
d) All ranges (range, slices) include neither the first nor the last element
e) None of the above

8) Which of the following most accurately reflects “Moore’s Law”


a) A description about trends in computer hardware
b) penned by Gordon Moore, Intel cofounder
c) relates that transistor density increases by a factor of 2 about every 2 years for the
same price.
d) All of the above
e) None of the above.

9) Python commonly uses the term namespace. What is a namespace?


a) A list of the predefined functions in Python
b) The list of predefined variables in Python
c) A list of Python names and the values with which they are associated
d) All of the above
e) None of the above

10) What is the difference between a “method” and a “function”?


a) A method is called in the context of an object using the “dot call” format.
b) A function has a return value but a method does not
c) A method has a return value but a function does not
d) Methods do not pass values.
e) None of the above

11) Which of the following are true about the tuple data structure?
a) when printed is enclosed in parenthesis
b) Is an immutable data structure
c) Is created by the use of commas
d) All of the above
e) None of the above

Figure 2
12) Given user input of ‘abc123!’ what output is produced by Line 3 in Figure 2?
a) [3, 3, 1]
b) [1, 3, 3]
c) [7, 1, 1]
d) [6, 1, 0]
e) None of the above
13) Given user input of ‘THIS == 44’ what output is produced by Line 3 in Fig 2?
a) [4, 2, 4]
b) [2, 2, 4]
c) [2, 2, 4]
d) [2, 2, 6]
e) None of the above
14) Given user input of‘cse231’ what output is produced by Line 4 in Figure 2?
a) 5
b) 6
c) 7
d) 8
e) None of the above
15) What would be the effect of changing the < on Line 1 to a <= in Figure 2?
a) no effect
b) index out of range error
c) illegal operation error
d) cntC final value will be increased by one
e) None of the above
16) What would be the effect of removing Line 2 in Figure 2?
a) No effect
b) infinite loop
c) syntax error
d) cntA final value will be decreased by one.
e) None of the above

Figure 3
17) Given user input of 10 what output is produced by Line 1 in Figure 3?
a) 6
b) 12
c) 18
d) 24
e) None of the above
18) Given user input of 13 what output is produced by Line 2 in Figure 3?
a) 6
b) 12
c) 18
d) 24
e) None of the above
19) Given user input of 6 what output is produced by Line 1 in Figure 3?
a) 6
b) 12
c) 18
d) 24
e) None of the above
20) Under what integer input values would Line 3 print a value of 0 in Figure 3?
a) values less than 0
b) values between 0 and 5
c) no such value, it would never happen
d) values between -5 and 0
e) None of the above
Figure 4

21) Given user input of 3 what output is produced by Line 2 in Figure 4?


a) [1, 2, 3, 4, 5, 6, 7]
b) [3, 4, 5, 6, 7]
c) [3, 4, 5, 6, 7, 1, 2]
d) [4, 5, 6, 7, 1, 2, 3]
e) None of the above
22) Given user input of 2 what output is produced by Line 3 in Figure 4?
a) 1
b) 2
c) 3
d) 4
e) None of the above
23) Which of the following user inputs would produce the Line 2 output
[1, 2, 3, 4, 5, 6, 7] in Figure 4?
a) 7
b) 14
c) 21
d) All of the above
e) None of the above
24) Which of the following statements describes the operation being performed by the +
sign on Line 1 of Figure 4?
a) addition
b) repetition
c) concatenation
d) inversion
e) None of the above
25) What type does the variable myLst contain in Figure 4?
a) integer
b) floating point value
c) string
d) Boolean
e) None of the above

26) Which of the following are true statements the continue command?
a) in a loop, exits the present loop suite immediately
b) in an if statement, exits the present if suite immediately
c) immediately goes back to the top of the loop
d) All of the above
e) None of the above
27) Write a small program question

20pts. Please, write on scrap paper first, then copy your answer to the next page

Requirements
Write a program that:
• prompt the user for a string
• extract all the digits from the string.
• If there are digits:
o sum the collected digits together
o print out:
 the original string
 the digits
 the sum of the digits
• If there are no digits:
o print the original string and a message “has no digits”

Example
• given the input: abc123
o prints abc123 has the digits 123 which sum to 6
• given the input: abcd
o prints abcd has no digits
CSE231, Spring 09, Exam 1 Written Part

Name:

Section:

myStr = raw_input("Give me a string: ")


digStr = ''

for c in myStr:
if c.isdigit():
digStr = digStr + c

if not digStr:
print myStr,'has no digits'
else:
tot = 0
for dig in digStr:
tot = tot + int(dig)
print myStr,'has the digits',digStr,'which
sum to',tot

Вам также может понравиться