Questions
Below are optional practice problems for Quiz 2. These problems are intended to help you become more familiar with loops and lists. Solutions for each problem can be found at the bottom of this page.
Lists T/F
- The values in a list cannot be changed while your program is running. (T/F)
- You can add, change, and remove items in a list. (T/F)
- For the purposes of this class, all items in a list are of the same type. (T/F)
- The first index of a list is 1. (T/F)
- The last index of a list is the length of the list. (T/F)
- You can initialize an empty list with no items. (T/F)
- To remove an item, you input the value of the item that you want to remove into the pop() function. (T/F)
- While loops inside a function may continue looping after a return statement is reached. (T/F)
- A while loop statement can you used anywhere you can write a statement. (T/F)
- You can put any kind of statement in the body of a while loop. (T/F)
List Diagramming 1
Given the code listing below, draw an environment diagram then answer the questions that follow.
- What is the final value of
x
in themain
function frame? - What is the length of
a_list
after the code has finished running? - What are the final items in
a_list
?
List Diagramming 2
Given the code listing below, draw an environment diagram then answer the questions that follow.
- From the
main
frame isb[2]
defined? - From the
main
frame, what are the final items ina
?
List Diagramming 3
Given the code listing below, draw an environment diagram then answer the question that follows.
- What is the final value of
i
in the make_word frame?
Loops Diagramming 1
Given the code listing below, draw an environment diagram then answer the questions that follow.
- What is the printed output?
- Write a function call to
listFun
that would return the value “saanhaoaawha”.
Function Writing
- Write a definition for a function called
reverse
, which has one parameter of type str namedstring
and returns a str that has all the characters ofstring
in reverse order. Use a while loop. For example, a call toreverse
with “hello” would return “olleh”.
Global Variables
Base your answers to the following questions on the code listing below.
- What is the printed output of this code listing?
- On line 8, the value of
x
is changed. On what line isx
declared? - Two different variables named
y
are declared. What is the final value of the global variabley
? - On line 11, a variable
y
is changed. On what line is thisy
declared?
Solutions
Lists T/F
- F
- T
- T
- F
- F
- T
- F
- F
- T
- T
List Diagramming 1
- 4
- 7
- [0, 1, 10, 2, 12, 5, 8]
List Diagramming 2
- No
- [2, 2, 3]
List Diagramming 3
- 3
Loops Diagramming 1
- “yaaohadaaaha”
- listFun(“snow”)
Function Writing
One possible solution is shown below, but there are multiple solutions!
Global Variables
- Output:
5
5
- Line 3
- 1
- Line 10