ENGR 102 EXAM 2 fully solved &
updated 2025-2026
1. List 5 good coding practices that have been mentioned in this course. - # Q1
# always comment your code
# keep your code simple
# test your code often, work through it in groups
# be consistent with your code
# use good identifying variables
# write as few of lines as possible
2. In the following dictionary, identify the keys and values. Write the command to add the
items 'Orange' and 1 to the dictionary. What does the command print(mydict.items())
output to the console? mydict = {'Apple' : 3, 'Pear' : 5, 'Banana' : 2} - # Q2
mydict = {'Apple' : 3, 'Pear' : 5, 'Banana' : 2}
mydict['Orange'] = 1
print(mydict.items()) # prints items as list
print(mydict) # **** whats diff ??? ****
# cannot do this, a dictionary does not have append or add
# mydict.append("'Orange' : 1")
3. Explain how dictionaries are different from lists, and how lists are different from
tuples. - # Q3
# dictionaries are a table of key values while a list
# is an ordered collection of items
# lists are mutable, tuples are not
ENGR 102
,ENGR 102
4. Identify and explain 3 ways strings are similar to lists - # Q4
# are similar bc both are used to store data and are a collection of items
5. List all of the data types we have seen in this course and provide an example of each.
Identify which ones are mutable and which are immutable. - # Q5
# integers : 9 : immutable
# floating-point : 2.9 : immutable
# tuples/ strings : "" : immutable
# lists/ dictionaries : [] {} : mutable
6. In your own words, explain the difference between the top-down and bottom-up design
methods. Name at least one advantage and one disadvantage for each. - # Q6
# Top down programming, in simple terms, is dividing
# and conquering a problem, Bottum-up programming is
# used when you're not sure where to start your code
7. Describe the various components of a hierarchy. How can we use one in program
design? - # Q7
# A hierarchy is an organized structure where items are
# ranked based on level of importance. As the tree
# descends, the tasks become less important.
8. What type of coding error do you hate the most and why? - # Q8
# Syntax Errors occur when the program contains invalid code that cannot be understood.
# Indentation Errors occur when the lines of a program are not properly indented.
ENGR 102
, ENGR 102
# Value Errors occur when an invalid value is used - can occur if letters are given to int().
# Name Errors occur when the program tries to use a variable that does not exist.
# Type Errors occur when an operation uses incorrect types - can occur if adding an
integer to a string.
9. When you are trying to fix your code, what are some alternatives to using a debugger?
- # Q9
# starting with a visual inspection can be helpful
# the try and excpect method aka guess and check
# method is helpful to check blocks of code
10. When would it be best to use the myfile = open(...) / myfile.close() commands for
opening files as opposed to the with open(...) as myfile: command? - # Q10
### option 1
myfile = open()
x=1
myfile.close()
### option 2
with open() as myfile:
meow = 1
# option 2 is better because it is less lines of code
11. When opening files, what is the difference between the designators r, w, r+, and a? - #
Q11
# r = read
# w = write
ENGR 102