Difference between revisions of "CSC111 Midterm Preparation 2014"

From dftwiki3
Jump to: navigation, search
Line 23: Line 23:
 
What is the output of the following python program?
 
What is the output of the following python program?
  
 
+
</pre></code>
 +
<source lang="python">
 
a = 3
 
a = 3
 
b = 4
 
b = 4
 
for a in range( b ):
 
for a in range( b ):
 
     print a, a+b
 
     print a, a+b
 
+
</source>
 +
<code><pre>
  
 
Problem #2:
 
Problem #2:
Line 36: Line 38:
 
(tricky)
 
(tricky)
  
 +
</pre></code>
 +
<source lang="python">
 
print( "you win: " )
 
print( "you win: " )
 
for i in range( -3, 0, -1 ):
 
for i in range( -3, 0, -1 ):
 
     print( "hip,", sep='-' )
 
     print( "hip,", sep='-' )
 
print( "hourrah!" )
 
print( "hourrah!" )
 +
</source>
 +
<code><pre>
  
 
Problem #3:
 
Problem #3:
Line 46: Line 52:
 
What is the output of the program below?
 
What is the output of the program below?
  
 +
</pre></code>
 +
<source lang="python">
 
Sum = 0
 
Sum = 0
 
for i in range( 3 ):
 
for i in range( 3 ):
Line 52: Line 60:
  
 
print( Sum )
 
print( Sum )
 
+
</source>
 +
<code><pre>
  
 
Problem #4:
 
Problem #4:
Line 60: Line 69:
 
of the following list of words.
 
of the following list of words.
  
 +
</pre></code>
 +
 +
<source lang="python">
 
L = [ "the", "quick", "red", "fox", 'jumped', 'over',\
 
L = [ "the", "quick", "red", "fox", 'jumped', 'over',\
 
       'the', 'lazy', 'brown', 'sleeping', 'dog' ]
 
       'the', 'lazy', 'brown', 'sleeping', 'dog' ]
 
+
</source>
 +
<code><pre>
  
 
In other words, the output for this list of words should be:
 
In other words, the output for this list of words should be:
Line 79: Line 92:
 
What were the 2 numbers entered by the user?
 
What were the 2 numbers entered by the user?
  
 +
</pre></code>
 +
<source lang="python">
 
def f1( a ):
 
def f1( a ):
 
     b = 5
 
     b = 5
Line 95: Line 110:
  
 
main()
 
main()
 
+
</source>
 +
<code><pre>
  
 
Problem #6:
 
Problem #6:

Revision as of 10:35, 17 February 2014

The midterm is closed-notes, closed-books, closed-computers.

Note that depending on which textbook is used for CSC111, different topics are covered in different orders, and sometimes topics covered one semester before the midterm are covered after the midterm another semester. The purpose of this page is to give you an idea of the format that will be used and the type of questions to expect. Do not pay close attention to the actual Python constructs being tested.


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!

NOTE: The python programs were originally Version 2, and may not work
when you try them out.   The idea here is to give you an idea of the type
of question to expect.

Problem #1:
==========

What is the output of the following python program?

a = 3
b = 4
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 = input( "Enter first number? " )
    b = input( "Enter second number? " )
    print  f1( b ), f2( a )


main()

Problem #6:
==========
You are writing a python program with emacs, and discover when you
exit emacs that you had actually named your program "test123.pt", and
not "test123.py", as you had originally planned.

What commands do you use to make sure all the code you have just typed
ends up in "test123.py"?   In other words, how do you rename the name
of your file so that it has the right extension?


Problem #7:
==========
In the list below, indicate which of the names represent an operating system,
a computer brand, an interpreter, a program, or a programming language.  

Linux
Emacs
Windows
Python
Shell
Macbook
Fedora
Unix
C
C++
basic

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  *
*                                  *
************************************