Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

CSC 161: Midterm Exam

Fall 2015

Part I:    True/False

Circle True or False for each statement.

1.  (3 points)  True       False        An algorithm can be written without using a programming language.

2.  (3 points)  True       False        In Python, x  =  x  +  1 is a legal statement.

3.  (3 points)  True       False        Since floating points numbers are extremely accurate, they should generally be used instead of integers.

4.  (3 points)  True       False        In Python, the result of 4  +  5 produces the same data type as 4.0  +  5.0.

5.  (3 points)  True       False        Python strings are mutable.


Part II:    Multiple Choice (Concept Review)

Select the correct answer to each question below.

6.  (3 points)  Fragments of code that produce or calculate new data values are called. . . 口 Identifiers

口 Expressions

口 Productive Clauses

口 Assignment Statements

7.  (3 points) Which of the following is not a Python data type: 口  int

口  float

口  rational

口  str

8.  (3 points) In order to use functions in the math library, a program must include. . .  a comment

口 a loop

口 an operator

口 an import statement

9.  (3 points)  Before reading or writing to a file, a Python file object must be created by calling. . . 口 open ()

  create()

口 File

 Folder


10.  (3 points)  A function can send data back to its caller function using a(n): 口 return keyword

 print () function

口 assignment

口 export statement


Part III:    Short Answer

Answer the questions, below. Don’t over-think the answers, keep it succinct! Partial credit is possible.

11.  Functions can be thought of as miniature sub-programs inside other programs. Like a program, we can think of functions has having input and output to communicate with the main program.

(a)  (2 points) What are the purpose of parameters?

 

(b)  (2 points)  Explain the difference between a formal and actual parameter, and give an example.

 

(c)  (2 points)  Describe a key difference between using a return statement versus print ().

 

12.  (4 points)  Given these Python statements:

    s1  =   [2,1,4,3]

    s2  =   ['c','a','b']

Show the result of evaluating each of the following sequence expressions:

s1  +  s2

 

s1[1]

 

s1[1:3]

 

s1  +  s2[-1]



13.  Consider the for loop in Python:

(a)  (2 points)  Simply explain what a for loop does:

 

(b)  (2 points) Write a for loop that uses the accumulator pattern to count the number of characters in the string “ More  Cowbell!” and the prints the total character count.

 

14.  Explain the following patterns in your own words: (a)  (2 points)  A simple decision

(b)  (2 points)  Two-way decision

 

(c)  (2 points)  Multi-way decision

 

Part IV:    Multiple Choice (Programming)

Select the correct output to the code provided.

15.  (5 points)

    list1  =   [1,  2,  3,  4]

    list2  =   [5,  6,  7,  8]

   print (len (list1  +  list2))

 2

4

口 5

8

口 The program returns an error

 

16.  (5 points)

def  list2():

s1  =   [2,  1,  3,  4]

s2  =   ['c',   'a',   'b']

print (s1+s2)

s1[1:3]  =  s2

print (s1+list (s2[-1]))

list2()

口  [2,  1,  3,  4,  ’c’,  ’a’,  ’b’]

[2,  ’c’,  ’a’,  ’b’,  4,  ’b’]

口  [2,  1,  3,  4,  ’c’,  ’a’,  ’b’]

[2,  ’c’,  ’a’,  ’b’,  ’b’]

口  [2,  1,  3,  4,  ’c’,  ’a’,  ’b’]

[’c’,  ’a’,  ’b’,  4,  ’b’]

口  [2,  1,  3,  4]   [’c’,  ’a’,  ’b’]

[2,  ’c’,  ’a’,  ’b’,  4,  ’b’]

口 The program returns an error.

17.  (5 points)

   def  bar3(x):

            p  =  -1234567890

             for  i  in  x:

                      if  p  <  i:

                               p  =  i

             return  p


   param  =   [27,  42,  -123,  12,  13,  -987654321]

   print (bar3(param))

  -987654321

口 None

  -1234567890

口  42

  27

18.  (5 points)

   def  square(x):

             area  =  x  **  2

             return  area

   def  cube(x):

             area  =  6  *  square(x)

             volume  =  square(x)  *  x

             return  area,  volume

   param  =  3

    a,  v  =  cube(param)

   print ("Area:",  a,  "and  Volume:   {0:.1f}".format (v))

口 Area:    54  and  Volume:    27

 Area:    54.0  and  Volume:    27.0

口 Area:    54  and  Volume:    27.0

口 Area:    27  and  Volume:    54

口  The  program  returns  an  error

19.  (5 points)    You have a text file called quotes.txt that contains four movie quotes, one per line:

"Forty-two,"  said  Deep  Thought

Don't  Panic.

Get  the  cheese  to  sickbay.

Open  the  pod  bay  doors,  HAL.

Then, you have a short Python program that reads in this quote file. What is its output?

f  =  open ("quotes.txt",  "r")

lines  =  f.readlines()

#  strip()  removes  the  newline  character:   "\n"

a_line  =  lines[1].strip()

print (a_line[:-1]  +  "!")

f.close()

  "Forty-two,"  said  Deep  Thought!

口 Don't  Panic!

口 Don't  Panic.

 Get  the  cheese  to  sickbay!

口 The program returns an error


Part V:    Programming Assignment

20.  (25 points) Write a Python function that takes a date as input and calculates the day number. The day number is the number of days from the beginning of the year. So January 1 is day number 1, July 22 is day number 196, and December 31 is day number 365 for non-leap years, and 366 for leap years. Your function must use loops. Using simplified formulas to calculate the day number is not acceptable.        The function you will write for your solution is required to use the following function prototype:

def  daynum(mm,  dd,  yy):

#  Parameters  mm,  dd  and  yy  are  all  integers.

#  Your  code  goes  here.

Your program should use return with a value for the day number calculation.

Also, you will need to call the function isleap(y) in your code, where y is an integer number for the year (i.e.  2015).  It returns True for leap years, False otherwise.  You dont have to write the body of this function, you just need to call it in your code.

def  is_leap(year):

if  year  %  4   !=  0:

return  False

elif  year  %  100  ==  0:

if  year  %  400  ==  0:

return  True

else :

return  False

else :

return  True

Lastly, a main() function is not required.

Hint: The number of days in each month is: January, 31; February, 28 in non leap year, 29 in leap year; March, 31; April, 30; May, 31; June, 30; July, 31; August, 31; September, 30; October, 31; November, 30; December, 31.

 

Question

Points

Bonus Points

Score

1

3

0

 

2

3

0

 

3

3

0

 

4

3

0

 

5

3

0

 

6

3

0

 

7

3

0

 

8

3

0

 

9

3

0

 

10

3

0

 

11

6

0

 

12

4

0

 

13

4

0

 

14

6

0

 

15

5

0

 

16

5

0

 

17

5

0

 

18

5

0

 

19

5

0

 

20

25

0

 

Total:

100

0