Uploaded by Alex Nina

Final Exam 2

advertisement
Final- Exam 2
Grade ( /80)
Name_________________________________________
1. After the statement data.insert(1, 15), the original data evaluates to (4pt)
a. [15, 10, 20, 30]
b. [10, 15, 30]
c. [10, 15, 20, 30]
2. The main window class in a GUI-based program is a subclass of: (4pt)
a. TextArea
b. EasyFrame
c. Window
3. The attribute used to attach an event-handling method to a button is named(4pt)
a. pressevent
b. onclick
c. command
4. The interface of a class is the set of all its(4pt)
a. objects
b. attributes
c. methods
5. The print function (4pt)
a. creates a new object
b. copies an existing object
c. prints a string representation of an object
For questions 6–9, assume that the variable info refers to the dictionary (20pt)
{"name":"Sandy", "age":17}.
6. The expression list(info.keys()) evaluates to
a. ("name", "age")
b. ["name", "age"]
7. The expression info.get("hobbies", None) evaluates to
a. "knitting"
b. None
c. 1000
8. The method to remove an entry from a dictionary is named
a. delete
b. pop
c. remove
9. Explain what happens when the following recursive function is called with the value (10pt)
4 as an argument:
def example(n):
if n > 0:
print(n)
example(n - 1)
10. (15pt)
A list is sorted in ascending order if it is empty or each item except the last one is less than or equal to its
successor. Define a predicate isSorted that expects a list as an argument and returns True if the list is sorted, or
returns False otherwise.(Hint: For a list of length 2 or greater, loop through the list and compare pairs of items,
from left to right, and return False if the first item in a pair is greater.)
11. (15pt)
Write a recursive function that expects a pathname as an argument. The pathname can be either the name of a
file or the name of a directory. If the pathname refers to a file, its name is displayed, followed by its contents.
Otherwise, if the pathname refers to a directory, the function is applied to each name in the directory. Test this
function in a new program.
# Past screenshot code and output here
Download