- (10%) Explain the meaning of Moore's Law.
- (10%) Determine whether the following code has
syntax errors or not. If it is correct, predict its output.
If it is incorrect, point out the mistake(s).
# for-loop (P.75)
x = 2
y = 10
for j in range(0, y, x):
print(j, end="")
print(x + y)
print("done")
- (10%) Determine whether the following code has
syntax errors or not. If it is correct, predict its output.
If it is incorrect, point out the mistake(s).
# String Formatting (P.147)
def star(n):
return '*'*n
height = 5
for i in range(height):
template = "{0:^" + str( 2 * height - 1 ) + "}"
print( template.format( star( 9 - 2 * i ) ) )
- (10%) Consider
the following decision structure:
# Decision
Structures (P.229)
a,
b, c,
= eval(input('Enter three numbers: '))
if a >
b:
if b > c:
print("Spam Please!")
else:
print("It's a late parrot!")
elif b
> c:
print("Cheese Shoppe")
if a >= c:
print("Cheddar")
elif a < b:
print("Gouda")
elif c == b:
print("Swiss")
else:
print("Trees")
if a == b:
print("Chestnut")
else:
print("Larch")
print("Done")
What
will be the output that would result from the input 5,4,7?
- (10%) Determine whether the following code has
syntax errors or not. If it is correct, predict its output.
If it is incorrect, point out the mistake(s).
# Recursive Function
def d(c):
if 'A' <= c <= 'F':
value = ord(c) - 55
else:
value = ord(c) - 48
return value
def f(s):
if len(s) == 1:
return d(s)
else:
return f(s[:-1])*16 + d(
s[-1] )
def main():
print( f("A1B2") )
main()
- (10%) Determine whether the following code has
syntax errors or not. If it is correct, predict its output.
If it is incorrect, point out the mistake(s).
# List-specific methods (P.345)
idList
= [4, 3, 1, 2]
nameList = ["Alice", "Bob", "Charlie", "Dennis"]
print( nameList[ 3 ] )
print( nameList[ idList.index(3) ] )
- Suppose that
you created a file "student.txt" using "nano"
with the following contents:
When you run the "Word Count" program (P.164, Ex.14) on this data file,
it shows
3 3 18
which implies that this file contains 3 lines, 3 words, and 18
characters (including the newline character \n).
Now, answer the following questions based on this data file:
- (10%) What will
be the output of the following
program?
# read() (P.154)
infile = open("student.txt", "r")
students = infile.read()
print( len(students) )
infile.close()
- (10%) What will be the output
of the following
program?
# readline()
infile = open("student.txt", "r")
students = infile.readline()
print( len(students) )
infile.close()
- (10%) What will be the output
of the following
program?
# readlines()
infile = open("student.txt", "r")
students = infile.readlines()
print( len(students) )
infile.close()
- (10%) What will be the output
of the following
program?
# read() and readlines()
infile = open("student.txt", "r")
students = infile.read()
print( len(students) )
students = infile.readlines()
print( len(students) )
infile.close()
- (10%) Determine
whether the following code has
syntax errors or not. If it is correct, predict its output.
If it is incorrect, point out the mistake(s).
# Find the earliest available
NTbus
def
earliest(aList, t):
i = 0
while t > aList[i]:
if i < len(aList) - 1:
i =
i + 1
else:
return "Not Available"
return aList[i]
def main():
arrival = ["08:50", "09:50", "10:50", "11:50",
"12:50", "13:50", "14:50",
"15:50", "16:50"]
print( earliest(arrival, "08:47"),
earliest(arrival, "16:46"),
earliest(arrival, "10:50"),
earliest(arrival, "17:00") )
main()
-
(10%) Determine whether the following code has syntax errors or not. If it is correct, predict its output. If it is incorrect, point out the mistake(s).
# It takes some time to walk to the platform of NTbus
def earliest(aList, t, buffer):
hour_str, minute_str = t.split(':')
hour, minute = int(hour_str), int(minute_str) + buffer
if minute >= 60:
hour = hour + 1
minute = minute - 60
t = "{0:02}:{1:02}".format(hour, minute)
i = 0
while t > aList[i]:
if i < len(aList) - 1:
i = i + 1
else:
return "Not Available"
return aList[i]
def main():
arrival = ["08:50", "09:50", "10:50", "11:50",
"12:50", "13:50", "14:50", "15:50", "16:50"]
print( earliest(arrival, "08:47", 5),
earliest(arrival, "16:46", 5),
earliest(arrival, "10:50", 5),
earliest(arrival, "17:00", 5) )
main()
-
(10%) Determine whether the following code has syntax errors or not. If it is correct, predict its output. If it is incorrect, point out the mistake(s).
# Indexing (P.343)
class Student:
def __init__(self, i, n, s):
self.id, self.name, self.score = i, n, s
def __str__(self):
return str(self.id) + ' ' + self.name + ' ' + str(self.score)
students = [Student(580, "Alice", 35),
Student(191, "Bob", 75),
Student(641, "Charlie", 76),
Student(813, "Dennis", 27),
Student(809, "Emily", 79),
Student(568, "Fiona", 65) ]
students.reverse()
print( students[2] )
- (10%) Determine whether the following code has syntax errors or
not. If it is correct, predict its output. If it is
incorrect, point out the mistake(s).
# Sorting a List (P.354)
class Student:
def __init__(self, i, n, s):
self.id, self.name, self.score = i, n, s
def __str__(self):
return str(self.id) + ' ' + self.name + ' ' + str(self.score)
def getScore(self):
return self.score
students = [Student(580, "Alice", 35),
Student(191, "Bob", 75),
Student(641, "Charlie", 76),
Student(813, "Dennis", 27),
Student(809, "Emily", 79),
Student(568, "Fiona", 65) ]
students.sort(key=Student.getScore)
- (10%) Determine whether the following code has syntax errors or
not. If it is correct, predict its output. If it is
incorrect, point out the mistake(s).
# Key Function for Sorting (P.353)
class Student:
def __init__(self, i, n, s):
self.id, self.name, self.score = i, n, s
def __str__(self):
return str(self.id) + ' ' + self.name + ' ' + str(self.score)
def score(aStudent):
return 100 - aStudent.score
students = [Student(580, "Alice", 35),
Student(191, "Bob", 75),
Student(641, "Charlie", 76),
Student(813, "Dennis", 27),
Student(809, "Emily", 79),
Student(568, "Fiona", 65) ]
students.sort(key=score)
print( students[2] )