Uploaded by Jerry Ma

CS1026 Midterm Exam multiple choice2.docx

advertisement
CS1026 Midterm Exam – October 2017
Page 4 of 16
Question 2: Multiple Choice – 15 Marks (1 each)
1) Which of the following statements is/are TRUE about the CPU?
a. CPU stands for Central Processing Unit
b. The CPU is what performs computation
c. The CPU processes machine language
d. At least two of the above statements are true
e. None of the above are true
2) What are two of the most important benefits of the Python language?
a. Advanced mathematical equations and fast programs
b. Ease of use and fast programs
c. Ease of use and portability
d. Fast programs and smaller programs
3)
a.
b.
c.
d.
Which statement(s) allows us to initialize the list numbers with 10 elements all set to zero?
numbers = [0]
numbers[10] = 0
numbers = [0] * 10
numbers[10] = [0] * 10
4) Which of the following subtracts a variable x from a variable y, divides their difference by 3
and adds
11 to the result:
a. ((x - y) / 3) + 11
b. x - y / 3 + 11
c. y - x /3 + 11
d. (y - x) / 3 + 11
e. None of the above are true
5) What will be the values of the variables num1 and num2 after the execution of the following
assignments?
num1 = 21
num2 = 18
num1 = num1 + num2 / 2
num2 = num1
a. num1 is 21, num2 is 21
b. num1 is 30, num2 is 30
c. num1 is 30, num2 is 21
d. num1 is 30, num2 is 18
e. None of the above.
Consider the following code for the next two questions:
num1 = int(input("Enter a number: "))
num2 = int(input("Enter a number: "))
num3 = int(input("Enter a number: "))
if num1 > num2 :
if num1 > num3 :
print(num1)
else :
print(num3)
else :
if num2 <= num3 :
print(num2)
else :
print(num3)
6) Assuming that a user enters 30, 20, and 10 as the input values in that order to the code above,
what is
the output?
a. 30
b. 20
c. 10
d. 30 and 20
e. Nothing, there is an error.
7) If the user enters, 10, 20, 30 as the input to the code above in that order, what is the output?
a. 10
b. 20
c. 30
d. 10 and 30
e. Nothing, there is an error.
8) The following code snippet contains an error. What is the error?
cost = int(input("Enter the cost: "))
if cost > 100
cost = cost - 10
print("Discounted cost:", cost)
a. Logical error: use of an uninitialized variable
b. Syntax error: missing colon after if statement
c. Syntax error: missing an else statement
d. Logical error: error in converting input
9) Which function call correctly invokes the partial drawShape function listed below
and prints a star triangle?
def drawShape(type) :
length = len(type)
if length == 0 :
return
if type == "triangle" :
print(" *")
print(" ***")
print("*****")
a. drawShape(triangle)
b. drawShape("triangle")
c. drawShape
d. value = drawShape(triangle)
10) Which of the following for loops will run the loop body 5 times?
a. for i in range(13, 9, -1) :
b. for i in range(14, 10, -1) :
c. for i in range(15, 9, -1) :
d. for i in range(14, 9, -1) :
11) Which of the following checks to see if there is a comma anywhere in the string
variable name?
a. if name.contains(",") :
b. if "," not in name :
c. if name.startswith(",") :
d. if "," in name :
12) Is the code snippet written below legal?
s = "1234"
for i in range (0, 4) :
print(s[i], s[i + 1])
a. Yes.
b. No; there should not be a colon at the end of line 2.
c. No; for i = 3, s[i + 1] will result in an string index out of range error.
d. No; for i = 0, s[i] will result in an string index out of range error
def myReverse(str):
outputStr = ""
i=1i=0
if ( len(str) ) < 5
if (len(str)) <= 5:
outputStr.upper() outputStr = str.upper()
return outputStr
else:
while i < len(str) :
outputStr = outputStr + str[i] outputStr = outputStr + str[(len(str)-1)– i)]
i=i+1
print outputStr
return outputStr
inp = input('Enter the string to be reversed:')
A = myReverse(inp)
print(A)
# Read an integer from the user.
value = input("Enter an integer: ") value = int (input(“Enter an int;”)
# Compute and display the factors.
print("The factors are:")
while value >= 1 : should be >
divisor = 2
found = False
# Search for a factor (divisor) until we find one.
while found == True :
should be false not true
if value % divisor == 0 :
print(divisor)
value = value / divisor
found = True
divisor = divisor – 1. Should be +1
print('Goodbye')
Download