Department of Computer Science
CSC 2301
Name: ___________________________
MIDTERM EXAM, Fall 2014 Date: Sep. 25, 2014 Time: 01:00pm – 02:15 p.m Total: 100 points + 20 bonus points
Problem
Points
1 2 3 4 5 6 (Bonus Question) Total
1/7
Department of Computer Science
CSC 2301
Name: ___________________________
1. ( 10 x 2 pts. = 20pts. )
(Questions: a,b,c,d,e) Multiple choice questions:
a). Which of the following is not a file-reading method in python? a. read c. readall
[
c ]
[
b
[
c ]
[
b
[
a ]
b. readline d. readlines
b). What is the output of print (3// 4)? a. 0.0 c. 0.75
]
b. 0 d. 1
c). In Python, getting user input is done with a special expression called a. for c. eval
b. input d. read
d). Which of the following are not used in expressions? a. variables c. operators
]
b. statements d. literals
e). Which of the following is a Python reserved word a. for c. Print
b. Input d. rawinput
2. (3 x 2 pts = 6 pts.)
(Questions: f,g,h) Fill in the blanks:
f. print(int(-3.54),round(-3.54)) output is
-3 -4
.
g. Example of “second memory” is .
CD, DVD, USB memory
h. The index of a string starts from?
0
.
3. (3 x 3 pts = 9 pts.)
2/7
Department of Computer Science
CSC 2301
Name: ___________________________
(Questions: i, j,k) The following programs don't run because they have a syntax error. Please help me fix them.
i. a = 30 b = 40 print("a=", a, "b=" b)
#print("a=", a, "b=", b)
j. def func1( ): print("line 1’) func1( ) #print("line 1”) Or print(‘line 1’)
k: a = “2” pi = 3.14 print(a+pi) #print(float(a)+pi) Or print(int(a)+pi) Or print(a + str(pi))
4. ( 5 x 5 pts. = 25 pts. ) What is the output of the following code:
3/7
Department of Computer Science
CSC 2301
Name: ___________________________
a) x=6/2 print (x)
# 3.0
b) What is the output of the following code: a = "Hello " b = "world!" print (a+b)
#Hello world!
c) string = “hello world!” newstr = string[0:-1] print(newstr)
# hello world
d) x = 4.0 + 4 print(x)
#8.0
e) x = 2.0 x=3+x x=x+3 x=x-2
4/7
Department of Computer Science
CSC 2301
Name: ___________________________
print (x) #6.0
5. ( 2 x 8 pts + 2 x 12 pts. = 40 pts. ) Write programs to solve the following problems
a) (8 pts )Write a program to prompt user to input two numbers, then calculate their average and then print the result.
first = eval(input("please input the first number: ")) second = eval(input("please input the second number: ")) average = (first + second)/2 print("the average of the two numbers is: ",average)
b) (8 pts )Write a program to prompt user to input two numbers, then calculate their distance and then print the result.
first = eval(input("please input the first number: ")) second = eval(input("please input the second number: ")) distance = abs(abs(first) – abs(second)) print("the distance of the two numbers is: ",distance)
c) (8 pts ) Write a program to prompt user for his/her first name, middle name and last name, and then print his/her name in the following format: ", : ". first = input("please input your first name: " ) middle = input("please input your middle name: " ) last = input("please input your last name: " ) print(“,:”) d) (12 pts ) Rectangle information Input: two mouse clicks for the opposite corners of a rectangle
5/7
Department of Computer Science
CSC 2301
Name: ___________________________
Output: Draw the rectangle. Print the area of the rectangle. Formulas: area = length*width Solution: #rectangle.pyw from graphics import * def main(): #Introduction print("this program will draw a rectangle and calculate its area and perimeter") win = GraphWin("rectangle",400,400) win.setCoords(0.0,0.0,4.0,4.0) #get the lower-left and upper-right points p1 = win.getMouse() p1.draw(win) p2 = win.getMouse() p2.draw(win) rect = Rectangle(p1,p2) rect.draw(win) # the width of this rectangle width = abs(p2.getX()-p1.getX()) length = abs(p2.getY()-p1.getY()) area= round(length*width,2) #perimeter output2 = Text(Point(3.5,0.5),"") output2.draw(win) #output output2.setText(perimeter) message = Text(Point(2,0.2),"click anywhere to quit.") message.draw(win) win.getMouse() win.close() main() e) (12 pts) Write a program to calculate the numeric value of a name which may contain first name, middle name and last name and separate by space. Input: name Output: numeric value of the name
6/7
Department of Computer Science
CSC 2301
Name: ___________________________
Solution: def main(): # convert all upper case to lower case name = input("please input a name: ").lower() # refer the ASCII, a=97, we can use the 'ord' function to convert character to number sum = 0 names = name.split() for subname in names: for ch in subname: num = ord(ch) # summing up the values of each letter sum = sum + num print("The numeric value of " + name + " is: ", sum) main()
f) Bonus (20 pts) Write a program that calculates the number of words in a sentence entered by the user. #Solution: # count.py # this program return the number of words in a input sentence, which is separated by space def main(): #input sentence sentence = input("please a sentence separated by space:") #first separating by',', then by space, like: hello world, this is a test sent_part = sentence.split(","); #after this, the sentence become two parts: "hello world", and "this is a test" # they serve as elements of sent_part # then we split it by space for each element count = 0 for sen in sent_part: sub_sent = sen.split() # sen will be splited by space and store them in the list of sub_sent sub_count = len(sub_sent) count = count + sub_count # print the count print("The number of words of this sentence is: ", count) main()
7/7