Midterm Prep 2018
D. Thiebaut (talk) 11:06, 20 February 2018 (EST)
Contents
CSC111
midterm exam preparation sheet
Here are sample problems that will give you an idea of the type of questions you can expect for the midterm. No solution sheet will be posted. If you want to figure out the answers, use Python in interactive mode, or write short programs that will help you discover the answers. That too is a good way to prepare for the exam!
Problem #1
What is the output of the following python program?
a = 3
b = 4
a, b = b, a
for a in range( b ):
print( a, a+b )
Problem #2
What is the output of the following program? (tricky)
print( "you win: " )
for i in range( -3, 0, -1 ):
print( "hip,", sep='-' )
print( "hourrah!" )
Problem #3:
What is the output of the program below?
Sum = 0
for i in range( 3 ):
for j in range( i ):
Sum = Sum + j
print( Sum )
Problem #4:
Write the code necessary to print the first, third, fifth, seventh,... words of the following list of words.
L = [ "the", "quick", "red", "fox", 'jumped', 'over', 'the', 'lazy', 'brown', 'sleeping', 'dog' ]
In other words, the output for this list of words should be:
the red jumped the brown dog
Make sure your code works with any list of words, not just the one given here. In particular, you shouldn't use the number 11, which is the number of words in the list.
Problem #5:
When we run the program below, it outputs 18 10. What were the 2 numbers entered by the user?
def f1( a ):
b = 5
return a*2
def f2( b ):
a = 3
return b+a
def main():
a = eval( input( "Enter first number? " ) )
b = eval( input( "Enter second number? " ) )
print( f1( b ), f2( a ) )
main()
Problem #6:
Which of the possible answers below is the actual output of this code section?
name = "hello" for i in range( 1, len( name ), 2 ): print( name[i], end="" )
- hello
- hel
- ello
- el
- lo
Problem #7:
Is the following code valid python code?
print( "print" ) forloop = "for" for i in range( len( "range" ) ): print( "range"[i] )
Problem #8:
Write a python program (you do not need to use functions, but you can if you prefer) that asks the user for her full name (first and last name), and outputs the name on the screen with a box of stars around it, such that the user's name is centered in the box. The box should be exactly 6 characters wider than the name of the user.
Here's an example of how a user might use the program:
python problem8.py
What is your name? Alan Turing
*****************
* *
* Alan Turing *
* *
*****************
Here is another example:
python problem8.py
What is your name? Nabucodonosor, King of Babylon
************************************
* *
* Nabucodonosor, King of Babylon *
* *
************************************
Problem #9:
There will be questions similar to the questions in the weekly quizzes on the reading material.