Python Sheet-4 2020 Programming
Python Sheet-4 2020 Programming
Figure 1
Given user input of 11, what value is output by Line 3 in Figure 1?
a) 0
b) 1
c) 2
d) 3
e) None of the above.
Given user input of 12, what value is output by Line 4 in Figure 1?
a) 0
b) 1
c) 2
d) 3
e) None of the above
What type is referenced by (associated with) userVal in Line 1 of Figure 1?
a) integer
b) float
c) list
d) tuple
e) None of the above.
What is the purpose of the = (equal sign) in Line 2 of Figure 1?
a) take the value on the left side and associate it with the variable on the right side
b) take the value on the right side and associate it with the variable on the left side
c) generate a Boolean indicating if the left side has the same value as the right side
d) all of the above, just depends on the type
e) None of the above.
What is the purpose of the : (colon) at the end of the while statement in Figure 1?
a) beginning of a compound statement (a suite)
b) end of a compound statement (a suite)
c) indication of a change of control in the program (a modifier)
d) only there to make the program more readable (a kind of comment)
e) None of the above.
What is an iterator?
a) a variable that can take on multiple values
b) a method that makes a sequence (like a list or a string)
c) sets the output width for all subsequent output
d) a control statement goes through the elements of a sequence, one at a time
e) None of the above
Which of the following statements are true about the differences between lists and tuples?
a) they differ in the kinds of objects they can hold
b) lists are mutable, tuples are not
c) tuples are mutable, lists are not
d) you cannot concatenate lists with the + operator
e) None of the above
Which of the following is true about the conversion statement as found below
myFloat = 3.14
int(myFloat)
a) permanently change the value of myFloat
b) permanently change the type of myFloat to an integer
c) take the value from myFloat and turn it into an integer, even if some parts of
myFloat’s value is lost. Return the integer. myFloat is not changed
d) take the value from myDouble and turn it into an integer only if myDouble is
not modified by the conversion. Return the integer. myDouble is not changed
e) None of the above
21) Given the input ‘a*b*1*.*T’ what output is produced by Line 3 of Figure 4?
a) [‘a’,’X’,’b’,’X’,’1’,’X’]
b) [‘a’,’X’,’b’,’X’,1,’X’]
c) [‘a’,’*’,’b’,’*’,1,’*’]
d) [‘a’,’X’,’b’,’X’,1,’X’,’.’,’T’]
e) None of the above
22) Given the input ‘3.14159’ what output is produced by Line 2 of Figure 4?
a) 4
b) 5
c) 6
d) 7
e) None of the above
23) Given the input ‘$100’ what output is produced by Line 4 of Figure 4?
a) False
b) True
c) 0
d) 1
e) None of the above
24) If Line 1 were changed to indx = indx + 2 in Figure 4, the effect would be?
a) No effect
b) error, infinite loop
c) the program would only process every other character
d) error, index beyond the length of the string
e) None of the above
25) What kind of type can the variable indx in Figure 4 hold?
a) no restriction, can contain a value of any type
b) restricted to holding integer types
c) restricted to holding string types
d) restricted to holding any sequence type (string, list, tuple, etc)
e) None of the above
26) Which of the following are true statements regarding for loops and while loops?
a) only a for loop can iterate through a sequence
b) a while loop is the more general “looping statement” of the two
c) the for loop requires a boolean in its statement header
d) All of the above
e) None of the above
27) Write a Python program that prompts the user for two numbers, reads them in, and
prints out the product, labeled.
29) Given a string s, write an expression for a string that includes s repeated five times.
30) Given an odd positive integer n, write a Python expression that creates a list of all the
odd positive numbers up through n. If n were 7, the list produced would be [1, 3, 5,
7]
31) Write a Python expression for the first half of a string s. If s has an odd number of
characters, exclude the middle character. For example if s were “abcd”, the result
would be “ab”. If s were “12345”, the result would be “12”.
32) Given a positive integer n, write Python code that prints out n rows, each with one
more ‘x’ on it than the previous row. For instance, if n were 4, the following lines
would be printed:
x
xx
xxx
xxxx
33) Suppose you are given a list of words, wordList. Write Python code that will
write one line for each word. The line for a word contains the word in lower case and
in upper case. For example if wordList is ['Jose', 'Sue', 'Ivan'], then
your code would print
jose JOSE
sue SUE
ivan IVAN
34) What is printed by the Python code?
for z in [2, 5, 6, 8]:
print 2*z
36) The data crunching needs of the 1890 US census were addressed by what
development significant in the history of computers?
37) In modern computers, numbers are stored electronically. Earlier mechanical devices
used for numerical calculation had other ways of representing numbers. List two
ways.
Backround
The Fibonacci sequence works as follows:
element 0 has the value 0
element 1 has the value 1
every element after that has the value of the sum of the two preceding elements
Requirements
Your program prompts for an element, and prints out the value of that element of the
Fibonacci sequence. Thus:
input 7, produces 13
input 9, produces 34
Hints
Don’t try to just type out the entire list. It gets big very fast. Element 25 is
75205. Element 100 is 354224848179261915075. Write a program!