1336-FA23-Practice Final
Question Label the values of the following Boolean expressions as True, False,
-01 Unknown, or Error which have the following specific meanings:
True: The expression always evaluates to Boolean True
False: The expression always evaluates to Boolean False
Unknown: The expression may evaluate to Boolean True or Boolean False
Error: It is not a Boolean Expression and/or evaluation will cause an error
You are given this information about the values of the following variables:
variable low has an integer value between -10 and +10
variable med has an integer value between 15 and 30
variable high has an integer value between 20 and 200
Answer Match Question Items Answer Items
med > 20 A.
True
low < med or med < high B.
False
(med + high) > low C.
Unknown
med > low and "tent" in "tennis" D.
Error
high + low == 'tennis'
"ten" in "tennis" or "tennis" in "ten"
Question Assume aDict is a dictionary, aList is a list, and aString is a string. Assume
-02 each of them has 5 members. Mark each of the following Python statements as:
Error: The execution of the statement will definitely lead to an error
No Error: The execution of the statement will NOT lead to an error
Unknown: The execution may or may not lead to an error depending on the values
of the variables
Answer Match Question Items Answer Items
[Link](aString) A.
Error
[Link](aString) B.
No Error
aList[3] = 0 C.
Unknown
aString[3] = 0
aDict[3] = 0
aList[aList[3]] = 0
Question There exists a file [Link] with the following fields:
-03 Name Midterm1Score Midterm2Score
The content of the file is as follows:
Henry 60 92
Alex 36 88
David 88 80
Linda 52 40
Marco 48 66
Consider the following code
fh = open('[Link]','r')
linelist = [Link]()
for line in linelist:
record = [Link]()
if (int(record[2]) > int(record[1])):
print(record[0])
[Link]()
What does this code print?
In your own words, what is the code doing?
Answer
Question What is the output of the following code?
-04
def partstring(astr, num):
newstr = ''
i = 0
while i < num:
newstr = newstr + astr[i+2]
i = i + 1
return (newstr)
print(partstring('01:Brazil', 5))
print(partstring('02:France', 3))
Answer
Question A business maintains a dictionary that contains pseudonames and hourly wages of
-05 employees. An example:
Payroll = {"HonoriaJ": 16.50, "JackieManager" :
48.00, "NormaK" : 19.00, "DarwinL": 22.50, "LindaManager":
28.00}
Students were asked to write a function to find and return a string that is the name
of the top earning employee in such a dictionary. Label each function below with
one of the following:
Correct Employee: The function will always return the name of the top earning
employee
Wrong Employee: The function will return the name of an employee, but it may not be
the top earning employee
Something Else: The function will return something other than the name of an
employee, or the program will fail.
Answer Match Question Items Answer Items
def TopEmp(payroll): A.
topemp = '' Correct Employee
maxsal = 0.0
for employee in [Link]():
if payroll[employee] > maxsal:
topemp = employee
maxsal = payroll[employee]
return(topemp)
def TopEmp(payroll): B.
topemp = '' Wrong Employee
maxsal = 0.0
for employee in [Link]():
if payroll[employee] > maxsal:
topemp = employee
maxsal = payroll[employee]
return(topemp)
def TopEmp(payroll): C.
topemp = '' Something Else
maxsal = 0.0
for salary in [Link]():
if salary > maxsal:
maxsal = salary
return(topemp)
def TopEmp(payroll):
topemp = ''
maxsal = 0.0
for employee in [Link]():
salary = [Link](employee)
if salary > maxsal:
topemp = employee
maxsal = salary
return(topemp)
Question Label the following statements as
-06 True: The statement is always True
False: The statement is always False
Unknown: It cannot be determined if the statement is True or False
Answer Match Question Items Answer
Items
Information in a file cannot be accessed until the file is A.
opened True
If a user tries to open a file named "[Link]" to write, but B.
there is no file called "[Link]" in the system, then a file False
with that name will be created.
A list can have a mix of integers and floats as its members
The size of a file is limited to 1000 characters
1336-FA22-Final-Part-B
Question Write a function WhenDouble(rate) that computes and returns the number of years
-01 for your bank savings balance to double. The only parameter is the percentage interest
rate. The interest is compounded annually.
Notes:
The question does not specify a starting balance. You can use 100 or any other
amount as your starting balance and compute the number of years to reach double
that amount.
As an example, WhenDouble(30) should return 3. Explanation: Suppose you have
a balance of 100 dollars in the bank. In Year 1, it will increase by 30% and reach 130.00.
In Year 2, it will increase by 30% of the new balance of 130.00 and reach 169.00. In
year 3, it will increase by 30% of 169.0 and exceed 200, which is double of your original
balance. Hence the function should return 3.
Answer
Question Develop a function AnonMe(story) that takes a string story as a parameter, replaces
-02 every instance of your first name in the story with PersonX, and returns the modified
story as a list.
Notes:
For example, if your name is Jane, and the parameter story is the string “Jane will do
what Jane wants to do”, then a call to AnonMe(story) should return the string
“PersonX will do what PersonX wants to do”.
You may use split and join functions as illustrated in the following statements:
listname = [Link]( ) to convert a string to a list of words
strname = " ".join(listname) to convert a list of words to a string with items
separated by blanks.
Answer
Question A business maintains a dictionary that contains pseudonames and hourly wages of
-03 employees. An example:
{"HonoriaJ": 16.50, "JackieManager" : 48.00, "NormaK" :
19.00, "DarwinL": 22.50, "LindaManager": 28.00}
The owner has issued a directive that
1. All employees making less than $30 an hour get a wage increase of $3.00 per
hour
2. Every employee must make a minimum wage of $20 an hour. That is, if any
employee makes less than $20 an hour after the $3 increase, their salary
should be increased to $20 an hour.
Your task is to write a function UpWages(payroll) that takes a dictionary payroll in
the above format and implements the new directive. The dictionary should be updated
to reflect the new wages based on the directive. The function UpWages should not
return anything.
Answer
Question A business maintains a dictionary that contains the pseudonames and hourly wages of
-04 employees. An example:
{"HonoriaJ": 16.50, "JackieManager" : 48.00, "NormaK" :
19.00, "DarwinL": 22.50, "LindaManager": 28.00}
The business has created a list of employees that have resigned, that looks like:
["JackieManager" , "DarwinL"]
Your task is to write a function UpDate(payroll, resigned) that takes a
dictionary payroll in the above format, and a list resigned of pseudonames of
employees that have resigned, as parameters. The function returns an updated payroll
dictionary without resigned employees. For the above example, the
function UpDate should return:
{"HonoriaJ": 16.50, "NormaK" : 19.00, "LindaManager": 28.00}
Notes:
Approaches to this question include creating a new dictionary with the appropriate
content or removing the resigned list names from the dictionary.
You may use the statement del dictionaryname[key] to remove an entry from a
dictionary.
Answer