TCSS142 Review Questions

Verified

Added on  2019/09/19

|8
|2141
|359
Homework Assignment
AI Summary
This document presents a comprehensive set of review questions for the TCSS142 course, covering various fundamental programming concepts. The questions are divided into six main sections: String Manipulation, File Reading and Writing, Tracing, Scope, and Pre/Postconditions, Function Object Matching, Lists, and Loops, Turtles, and Classes. Each section includes multiple problems designed to test the student's understanding of the material. The String Manipulation section focuses on modifying and manipulating strings using various techniques. The File Reading and Writing section involves reading from and writing to files, including handling different file formats and data types. The Tracing, Scope, and Pre/Postconditions section tests the student's ability to trace code execution, understand variable scope, and define pre- and post-conditions for functions. The Function Object Matching section requires students to match objects with their respective functions and understand their parameters and return types. The Lists section involves completing coding exercises on lists. Finally, the Loops, Turtles, and Classes section includes problems on creating and manipulating lists, using different types of loops, drawing shapes with turtles, and creating classes with specific attributes and methods. The document also includes a section on matching objects with their respective functions, along with their argument types and return types.
tabler-icon-diamond-filled.svg

Contribute Materials

Your contribution can guide someone’s learning journey. Share your documents today.
Document Page
TCSS142 REVIEW QUESTIONS 1
REVIEW QUESTIONS
1) String Manipulation
Correct the following:
print(“”This is a test,\” said the teacher\’s assistant, ”of +
“what you \”know about backslash \ characters.’)
Write code to change the line, “Bilbo Baggins and Gandalf the Grey
were old friends.”, so that
All the consonants are capitalized
All the vowels are removed
All the capital letters replaced by their ASCII values
There are no duplicate letters (case ignored)
The string is reversed character-by-character
The string is sorted by word in alphabetical order
Change your above code so that it is robust (any string could be input
and the output will be correct); you should probably use for loops.
Write a program that takes user input as a string and then prints out
that string with every word backwards (a word as defined by any
sequence of characters separated by a space).
2) File Reading and Writing
Write a program that reads a file filled with one month and year per
line (e.g., “March 2004”, “February 2000”) and prints to the console
the number of days in that month (e.g., “31 days in March 2004”). You
should use nested conditionals and have your program correctly report
leap years’ differing February days using the rules according to
Wikipedia.
tabler-icon-diamond-filled.svg

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
TCSS142 REVIEW QUESTIONS 2
Write a program that can write to a file from another file, each
specified as entry from the console. (Use any of the text files
provided you from this course or make your own.) From the file being
read, you should iterate over each line and character, passing only
punctuation, numbers, and the letters ‘p’, ‘y’, ‘t’, ‘h’, ‘o’, and ‘n’
to the file being written to.
Write a program that reads a file and counts up how many of each type
of ASCII value appears throughout the file. Print this out as a table
using tabs to format it.
3) Tracing, Scope, and Pre/Postconditions
Given the following code, expand the table below recording all of the
variable values every time a variable changes:
i = 100
j = 0
h = 4000
while i >= j:
j = i
h = i * 50
while j > 5:
j -= 20
while h > i:
h /= 10
i += 1.5
i -= 32.5
Variable Change i j h
i 100
j 100 0
h 100 0 4000

For the following code, record the value of a every time it changes:
a = 1
while (a < 100):
a *= 4
a -= a // 2
Document Page
TCSS142 REVIEW QUESTIONS 3
Given the following code, trace the code line by line for each
function, using the table below as a model:
def method1(d, c, b, a):
print(a, b, c, d)
d *= len(b)
c += '5'
b += [d, a]
a = d < sum(b)
print(a, b, c, d)
def method2(w, x, y, z):
print(w, x, y, z)
w = w != w
x = y
y = z
z = x * 3
print(w, x, y, z)
method3(z, z, y, z)
def method3(h, i, j, k):
print(h, i, j, k)
h = 10
i += ' word'
j = [h, h, i, k]
k *= 2
print(h, i, j, k)
def main():
a = 15
b = 'the'
c = [2.0, 3.1, 4.2]
d = True
print(a, b, c, d)
method1(a, b, c, d)
method2(d, c, b, a)
method3(a, b, b, a)
print(a, b, c, d)
main()
Function Line Number Variable Names and Value list contents
main 1 1, 2, 3, 4
2 a = 15 5, 6, 7, 8
3 a = 15, b = ‘the’
4 a = 15, b = ‘the’, c =
5 a = 15, b = ‘the’, c = , d = True
method1 1 d = 15, c = ‘the’, b = , a = True
Document Page
TCSS142 REVIEW QUESTIONS 4
Write down a definition of variable scope:
Write down definitions of the following: local variable, global
variable, immutable object, mutable object.
Then, for the following code, point out where scope errors occur:
def scope1(number, word):
number += len(word)
condition = True
amount = number * number 10
scope2(amount, condition)
def scope2(total, bool):
total += number
if bool:
scope3(amount)
def scope3(amount):
if amount > number:
scope1(number * 2, “WORD”)
else:
condition = False
Analyze the following code and figure out what it is doing. Then,
write pre- and post conditions for the function:
def function method(variable, unknown):
object = 0
for thing in variable:
for smallthing in thing:
if smallthing.isalnum():
unknown.append(smallthing.lower())
elif ord(smallthing) == 35:
object += 1
if object > 10:
break
variable.close()
return object
tabler-icon-diamond-filled.svg

Secure Best Marks with AI Grader

Need help grading? Try our AI Grader for instant feedback on your assignments.
Document Page
TCSS142 REVIEW QUESTIONS 5
4) Function Object Matching
For each of the following objects on the left, match them to their
respective function(s) on the right (a function name may be shared by
more than one object). Additionally, if the function has parameters
(x) or (x,y), write what type those can be; additionally, if the
function returns a value, write what type of value it returns and what
it represents; if it returns nothing, write “None”.
Object Function Arg Type Return Type
write(x)
sort()
lower()
strip()
find(x)
String close()
len(x)
range(x)
list()
print(x)
List ord(x)
upper()
split(x)
float()
isalpha()
File chr()
index(x)
open(x,y)
list()
isdigit()
System* replace(x,y)
max()
remove(x)
sum(x)
read()
abs(x)
*(can be called without an object reference, e.g. print())
Document Page
TCSS142 REVIEW QUESTIONS 6
5) Lists
Complete as many codingbat.com exercises on lists as possible. If
time, also attempt to complete the string exercises.
6) Loops, Turtles, and Classes
You are tasked with keeping records of how much change there is in a
community change jar. To do this, you need to make two parallel lists,
one that has the dollar type amount by unit as stored in floating
point values (i.e., 0.01, 0.05, ..., 100.0), changeList, and the other
with corresponding counts of these type of change, amountList. Write
at least these two methods in your code:
def addChange(listOfChangeToBeAdded, changeList, amountList):
#this method will accept a list of coin/bill amounts
#(e.g., [1.00, 0.05, 0.25, 0.05, 10.00])
#and appropriately update the pthearallel lists with counts
def randomChange(numberOfChangeToMake, changeList):
#given a number, this method creates and returns a list
#containing that many values, each to be randomized
#between a penny, a nickel, a dime, a quarter, a half-bill,
#a $1 bill, a $2 bill, a $5 bill, a $10 bill, a $20 bill,
#a $50 bill, and a $100 bill
Once you have coded the former problem, test your code by calling
randomChange(int, list) several times and sending their lists to the
addChange(list, list, list) to see if works correctly. Use print
statements after every function call to print the current values and
check whether they were properly added.
Add a method to the code you wrote for the previous problem that gets
the total of the change jar and returns it. Print this value in main
Add a method to the code you wrote that analyzes your parallel lists
for which coin and which bill (exclusively) has the most
representation in the change jar. Print the coin and bill type and how
many of them are in the jar.
Document Page
TCSS142 REVIEW QUESTIONS 7
Write code that produces the following outputs. You must use loops and
cannot have any string literals longer than 1 character.
Output 1:
|------O---------------------------|
|------O------O--------------------|
|------O------O------O-------------|
|------O------O------O------O------|
Output 2:
+---//\\---+
++--//..\\--++
+++-//....\\-+++
++++//......\\++++
Output 3:
[1]
[1.[2]]
[1.[2.,[3]]]
[1.[2.,[3.,;[4]]]]
[1.[2.,[3.,;[4.,;:[5]]]]]
You have learned three types of loop in this course: for each, for in
range, and while. Solve the following problem three different ways—one
for each loop: Write a function that receives a string and goes
through each character in that string and appends it as its ASCII
value to a list. Use whatever other variables and logic is appropriate
for solving it with each looping method.
tabler-icon-diamond-filled.svg

Paraphrase This Document

Need a fresh take? Get an instant paraphrase of this document with our AI Paraphraser
Document Page
TCSS142 REVIEW QUESTIONS 8
Write code that draws the following shape using turtles:
Imagine that you are in a group that is writing grading software for a
school. You are assigned to write two classes for this project--a
student class and an assignment class, with the following
specifications:
class Student:
#should have attributes: name (String), student id (int),
#a GPA (double), and a class standing (String, i.e.,
#freshman, sophomore, junior, senior)
#when initialized, should accept a name, id, GPA, and
#class standing; additionally, should have functions to
#change each of the attributes (i.e., setName(name),
#setID(id), setGPA(gpa), and setClassStanding(level))
class Assignment:
#should have attributes: type (String, i.e., homework,
#quiz, test, project, lab) and points (int)
#when initialized, should accept a type and points;
#additionally, should have functions to change each
#of the attributes (i.e., setType(type), setPoints(points))
chevron_up_icon
1 out of 8
circle_padding
hide_on_mobile
zoom_out_icon
logo.png

Your All-in-One AI-Powered Toolkit for Academic Success.

Available 24*7 on WhatsApp / Email

[object Object]