Download exam2S11AKey.pdf

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
Spring Semester 11
Dr. Dillon. (04/05)
Exam #2
Form 2 A
Name (Last, First ):
Section Number:
Directions:
a) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD
TO BEGIN.
b) You may use one 8.5" x 11" note sheet during the exam. No other reference
materials or electronic devices may be used during the examination.
c) Questions will not be interpreted during the examination. You should choose the
single best alternative for each question, even if you believe that a question has
multiple correct answers, is ambiguous or contains a typographic error.
d) Write your name and section number at the top of this exam booklet and on the
answer sheet for the coding problem.
e) Use a #2 pencil to encode information on the OMR form:
 Last name and first initial
 MSU PID
 Exam form: 2A
 Answers to the multiple choice questions
f) Sign the OMR form.
g) Only answers recorded on your OMR form will be counted for credit for multiplechoice questions. Completely erase any responses that you wish to delete.
h) You have 80 minutes to complete the exam. This exam booklet contains 25
multiple choice questions, each weighted equally (5 points) and one programming
problem (totaling 25 points). (26 questions, 9 total pages)
i) Turn in this exam booklet, your note sheet, your scratch paper, and the OMR form
when you are done. Write your name on anything you would like returned to you
after the exam is graded. When leaving, please be courteous to those still taking
the exam.
Good luck!
Timing tip. A rate of 2 minutes per multiple choice problem leaves 30 minutes to write
the program (worth 25 points) at the end.
----------------------------------------------------Form A
1
2
3
4
5
6
7
8
9 10 11 12 13
D
D
E
C
A
A
C
B
C
B
D
B
A
14 15 16 17 18 19 20 21 22 23 24 25
A
B
D
A
D
C
C
B
B
B
A
B
-----------------------------------------------------
1) Which of the following is not a characteristic of OOP?
a) Encapsulation.
b) Inheritance.
c) Polymorphism.
d) Aliasing.
e) None of the above (i.e., all are characteristics of OOP)
2) Which of the following statements about file I/O in Python is correct?
a) To read from or write to a file, a program must import the file.py module.
b) An attempt to read from a file that does not exist returns an empty string.
c) An attempt to write to a file that does not exist causes an error.
d) A file object that is opened in mode “w” must be closed to ensure that the write
buffer is flushed to disk.
e) None of the above.
3) Given s1 = set(“Kansas”), what is the value of len(s1)?
a) 6
b) 5
c) len(“Kansas”)
d) s1
e) None of the above
4) Given s1 = set(“Kansas”) and s2 = set(“South Dakota”), what is
the value of s1.intersection(s2)?
a) set([‘k’, ‘a’, ‘s’])
b) {‘k’, ‘a’, ‘s’}
c) set([‘a’])
d) {‘a’}
e) None of the above
5) Which of the following does Python use to determine what an overloaded operator
means?
a) Introspection
b) Aliasing
c) Garbage collection
d) Retrospection
e) None of the above
Figure 1
6) What is output by Line 1 when the program in
Figure 1 is executed?
a) 0
b) 1
c) [0, 1, 2]
d) [1, 2, 3]
e) None of the above
7) What is output by Line 2 when the program in
Figure 1 is executed?
a) 0
b) 1
c) [0, 1, 2]
d) [1, 2, 3]
e) None of the above
8) What is output by Line 3 when the program in Figure 1 is executed?
a) 0
b) 1
c) [0, 1, 2]
d) [1, 2, 3]
e) None of the above
9) What is output by Line 4 when the program in Figure 1 is executed?
a) 0
b) 1
c) [0, 1, 2]
d) [1, 2, 3]
e) None of the above
10) What is output by Line 5 when the program in Figure 1 is executed?
a) 0
b) 1
c) x
d) y
e) None of the above
11) What is output by Line 6 when the program in Figure 1 is executed?
a) 0
b) 1
c) y
d) x
e) None of the above
Figure 2
12) What is output by Line 1 when the program in Figure 2 is executed?
a) [0, 1, 2]
b) [2, 1, 0]
c) [1, 2, 3]
d) An error
e) None of the above
13) What is output by Line 2 when the program in Figure 2 is executed?
a) [0, 1, 2]
b) [2, 1, 0]
c) [1, 2, 3]
d) An error
e) None of the above
Figure 3
14) What is output by Line 1 when the program in Figure 3 is executed?
a) 5
b) 3
c) 2
d) 0
e) None of the above
15) What is output by Line 2 when the program in Figure 3 is executed?
a) True, True
b) True, False
c) False, True
e) None of the above
d) False, False
16) What is output by Line 3 when the program in Figure 3 is executed?
a) False, False
b) True, False
c) False, True
e) None of the above
d) True, True
Figure 4
data.txt
17) What is output by Line 1 when the program in Figure 4 is executed?
a) 4
b) 5
c) 0
d) 10
e) None of the above
18) What is output by Line 2 when the program in Figure 4 is executed?
a) An error
b) 10
c) 8
d) 3
e) None of the above
19) What is output by Line 3 when the program in Figure 4 is executed?
a) An error
b) 10
c) 8
d) 3
e) None of the above
20) What is output by Line 4 when the program in Figure 4 is executed?
a) An error
b) True
c) False
d) Garbage
e) None of the above
21) What is output by Line 5 when the program in Figure 4 is executed?
a) An error
b) Goofy
c) 3
d) Garbage
e) None of the above
Figure 5
22) Which of the following methods
defined in Figure 5 is an example
of a setter method (also known
as a modifier method)
a) __str__
b) crop
c) select
d) __add__
e) None of the above
23) The right side of Figure 5 illustrates an interaction with the Python Shell in Idle. The
responses to the last three commands have been deleted. (The response to the first
command is left in as an example.) For this interaction, what will the shell display in
response to command 2?
a) Python.is
b) in init
c) in init
d) An error
Python.is
e) None of the above
24) For the interaction on the right side of Figure 5, what will the shell display in
response to command 3?
a) in repr
b) in str
c) 'Python.is' d) in str
in str
in repr
Python.is
'Python.is'
Python.is
e) None of the above
25) For the interaction on the right side of Figure 5, what will the shell display in
response to command 4?
a) in add
b) in add
in str
in init
Python.is.Python.is
in str
Python.is.Python.is
c) Python.is.Python.is
e) None of the above
d) in add
25pts. Please, write on scrap paper first, then copy your answer to the answer sheet .
Make sure you put your name and section number on the solution and hand it in at
the end of the test.
Overview
Your program must read an input file and write the word frequency information, on a perline basis, to an output file.
Detailed requirements
More specifically, your program must:
1. Prompt the user to indicate if the words in each line should be listed in ascending
(‘a’) or descending (‘d’) alphabetical order. Keep prompting until the user enters
‘a’ or ‘d’ or just enters “return”. If the user just enters “return”, use descending
alphabetical order. (See example interaction below.)
2. For each line in the file named “input.dat”, write the words that appear in the line
and the number of occurrences of that word in parenthesis. Output the words for
each line in descending or ascending alphabetical order, as requested in step 1,
and separated by a comma. Count words that differ only by case (e.g. "All" and
"all") as the same word. (See example interaction and output files below.)
Additionally, to receive full credit:
3. Structure your program as four functions:
a. A function that encapsulates all interaction with the user to learn whether
words should be sorted in ascending alphabetical order or in descending
alphabetical order. (5 points)
b. A function that builds a data structure that records the word frequency
information for a line of input. (5 points)
c. A function that returns the string to be output for the data structure produced
by the previous function. (5 points)
d. A main function that performs the file I/O and calls the other functions to
accomplish the tasks. (10 points)
4. Do not use global variables or nested functions. (Global constants are fine.)
5. Note: To run your program, a user will load it and then call your main function by
typing main(). (See example interaction below.)
Hints
1. A dictionary is a good data structure to use for 3 b. (But it is not required.)
2. Write clearly (ask for another answer sheet, if needed) and be careful to indent
code properly.
3. If you choose good function names and variable names, you will not need many
comments.
4. If you make good use of Python data structures and operations/methods, each of
these functions can be programmed in 15 lines of code or fewer.
5. Some methods that you might find useful:
a. On strings: theStr.lower(), theStr.split(arg)
b. On dictionaries: theDict.items()
c. On lists: theLst.sort()
d. On files: open(fname, mode), fobj.close(),
fobj.readline()
Input file for the example:
Example interaction: In this example, the user enters everything on a line following the
shell prompt “>>>” or a question mark (‘?’). The program outputs everything else.
Output file (freq.dat) produced by the first call to main:
Output file (freq.dat) produced by the second call to main: