You are on page 1of 7

Python Programming sheet-4 – 2019-2020

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.

How large a value can a 32 bit unsigned integer hold?


a) about 16 billion
b) about 8 billion
c) about 4 billion
d) no limit
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 represents the Boolean “not” operator?


a) not
b) ~
c) !
d) %
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

Which of the following describes the advantages of modules?


a) share knowledge with others about how to solve a particular problem
b) saving effort by allowing reuse of code by others
c) verification of the code by use in many other systems
d) All of the above
e) None of the above
Figure 2

Given the input ‘aabbcc’ what output is produced by Line2 in Figure 2?


a) ‘aa’
b) ‘bb’
c) ‘cc’
d) ‘bbcc’
e) None of the above
Given the input ‘aaccbb’ what output is produced by Line2 in Figure 2?
a) ‘aa’
b) ‘bb’
c) ‘cc’
d) ‘bbcc’
e) None of the above
Given the input ‘abcc’ what output is produced by Line3 in Figure 2?
a) 1
b) 2
c) 3
d) 4
e) None of the above
What is the purpose of the break statement in Line 1 of Figure 2?
a) exit the loop
b) exit the present iteration of the loop, continue from the top of the loop
c) skip the next statement and then continue
d) halt the entire program
e) None of the above
Which of the following is a true statement(s) about Python slicing?
a) a range, indicated with a ‘:’ , is a half-open range
b) a negative number starts counting from the end of the string
c) the first element in a string begins at index 0
d) All of the above
e) None of the above
Figure 3
Given the input 10 what output is produced by Line 2 of Figure 3?
a) 0
b) 3
c) 14
d) 18
e) None of the above
Given the input 11 what output is produced by Line 3 of Figure 3?
a) 4
b) 5
c) 6
d) 7
e) None of the above
Which of the following is a reasonable replacement for Line 1 in Figure 3?
a) extra++
b) ++extra
c) extra =+ 1
d) extra += 1
e) None of the above
If Line 1 were removed from Figure 3, which of the following statements would be
true?
a) no runtime errors
b) extra would always print 1
c) error, would run past the end of the range
d) All of the above
e) None of the above
Figure 4

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.

28) What is printed by the Python code?


s = ”abcdefg”
print s[2]
print s[3:5]

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

35) What is printed by the Python code?


x = 2.5679
y = 9.0
print ”Answers %.3f and %.3f” % (x, y)

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.

38) Write a small program question

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

The beginning of the sequence looks like:


0,1,1,2,3,5,8,13,21,34,… for example
 element 6 has the value 8, the sum of element 4(3) and element 5(5)
 element 7 has the value 13, the sum of element 5(5) and element 6(8)

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!

You might also like