SE 100 Midterm 2 PDF
SE 100 Midterm 2 PDF
Student Name:
Student Number:
Student Signature:
A. ABC
B. ABCD
C. C
D. ABD
2) _________ loop uses a true/false condition to control the number of times that it repeats.
A. Condition-controlled
B. Counted-controlled
C. Both condition-controlled and counted-controlled
D. None of the mentioned
3) What is the output of the following code (Consider the comma as a new line)?
START_SPEED = 60
END_SPEED = 101
INCREMENT = 10
Page 2 of 12
4) What is the output of the following code if the user enters (Andrew) as the first name and (Ng) as
the last name?
x = 'i'
while x == 'i':
fname = input('Enter your first name: ')
lname = input('Enter your last name: ')
merge_names = fname + lname
print(merge_names)
print('End program')
A. AndrewNg
End program
B. AndrewNg, and then the program goes into an infinite loop.
C. Andrew Ng
D. Andrew Ng End program
Page 3 of 12
8) What will be displayed after the following code is executed?
for num in range(0, 20, 5):
num += num
print(num)
A. 25
B. 30
C. 0 5 10 15
D. The code will not work because of a syntax error
A. 2
B. 10
C. 12
D. 20
10) What is the output of the following code (Consider the comma as a new line)?
x = 1
def f1():
x = 3
print(x)
f1()
print(x)
A. 1, 1
B. 2, 3
C. 3, 3
D. 3, 1
E. Error
A. 3
B. 5
C. 6
D. 7
E. The program has a runtime error because a3 and a4 are not defined
Page 4 of 12
12) What is the output of the following code?
x = "hard"
def myfunc():
global x
x = "easy"
myfunc()
print("Python is " + x)
A. Python is hard
B. Python is easy
C. Python is hard easy
D. Python is easy hard
E. The program has a runtime error because x is not defined.
A. [1, 3, 2, 4, 5, 2]
B. [1, 3, 2, 4, 5, 2, 1]
C. 0
D. [1, 3, 2, 4, 5, 2, 1, 0]
Page 5 of 12
16) What is the output of the following code?
def f(i, values):
values.append(i)
return values
myList = []
v = f(1, myList)
v = f(2, myList)
v = f(3, myList)
print(v)
myValue = 1
myList = [10, 20, 30]
f(myValue, myList)
print(myValue,',' ,myList)
18) What is the output of the following code (Consider the comma as a new line)?
def c(x):
x = 52
print('x is', x)
def main():
x = 42
print('x is', x)
c(x)
print('x is', x)
main()
A. x is 42, x is 52, x is 52
B. x is 42, x is 42, x is 42
C. x is 42, x is 52, x is 42
D. x is 52, x is 52, x is 52
Page 6 of 12
19) What is the output of the following code?
list1 = ['a', 'b', 'c']
list2 = ['A', 'B', 'C']
list1 += list2
print(list1)
A. The number is 10
The number is 0
B. The number is 10
The number is 10
C. The number is 0
The number is 10
D. The number is 0
The number is 0
A. [1, 2, 3, 4, 5]
B. [2, 3, 4, 5]
C. [3, 4, 5]
D. [1]
Page 7 of 12
22) What is the possible output after executing the following code?
import random
HEADS = 1
TAILS = 2
TOSSES = 10
def main():
for toss in range(TOSSES):
if random.randint(HEADS, TAILS) == HEADS:
print('Heads')
else:
print('Tails')
main()
A. [1, 2, 3]
[1, 2, 3, 10, 20, 30]
B. [1, 2, 3]
[10, 20, 30, 1, 2, 3]
C. [1, 2, 3]
[10, 20, 30]
D. [10, 20, 30]
[1, 2, 3]
Page 8 of 12
25) What is the output of the following code?
lis = [2, 1, 3, 5, 3, 8]
lis.sort()
print ("List elements are : ", end="")
for i in range(0, len(lis)):
print(lis[i], end=" ")
lis.reverse()
lis.remove(3)
print ("\nList elements are : ", end="")
for i in range(0, len(lis)):
print(lis[i], end=" ")
26) What is the correct answer after executing the following code? (Consider the comma as a new
line)
s=0
while(s<10):
if(s%2==1):
print(s)
s+=1
27) What is the output after executing the following code? (Consider the comma as a new line).
for x in range(2,11,2):
if(x%2==0):
print(x)
Page 9 of 12
28) Given the following code, which of the following statement is a correct answer?
s=10
sum=0
while(s!=0):
s=int(input("Input: " ))
sum+=s
print("Sum is ", sum)
A. It will keep on showing the message “Input: ”,as the user keeps entering non-zero numbers. If
the input is zero, it will print the summary of the numbers.
B. It will show the message “Input: ”once, and after the user inputs a number will go into an
endless loop.
C. It will keep on showing the message “Input: ”, as the user keeps entering non- zero numbers.
If the input is zero, it will print the message “Sum is 0”.
D. It will keep on showing the message “Input: ”,as the user keeps entering non- zero numbers. If
the input is zero, it will go into an infinite loop.
E. It will show the message “Input: ” once, and after the user inputs a number it will print the
message “Sum is <Num>”, where <Num> is the user’s input.
30) What is the output of the following code? (Consider the comma as a new line).
s=0
for x in range(5):
s+=x
print(s)
A. Just number 10
B. 0,1,3,6
C. 0,5
D. 0,1,2,3,4
E. 0,1,3,6,10
Page 10 of 12
31) What is the output of the following code:
def aFunction(arg1, arg2, arg3):
for x in range(arg1,arg2):
arg3+=x
print(arg3)
aFunction(2,3,5)
A. 8
B. 10
C. There is no output, but the code will exit successfully
D. The code will go into an infinite loop
E. None of those answers is correct
32) What is the output of the following code? (Consider the comma as a new line)?
def changeValue(arg1, arg2, arg3):
print (arg2)
arg3 = 7
return arg1
arg3 = changeValue(2,4,5)
print(arg3)
A. 4, 7
B. 4, 2
C. Sytax error
D. 2, 7
E. 5
A. -8
B. 6
C. 4
D. d
E. -6
Page 11 of 12
34) Using the math module, what is the correct statement to find the square root of a number, x?
Assume that the math module is imported.
F. squareRoot(x)
G. math.sqrt(x)
H. M.SR(x)
I. MATH.sqrt(x)
def change(list):
aChar = 'B'
list[2] = 99
A. Syntax error since the function cannot return more than one value
B. 77 , 55
C. 55 , 77
D. 77 , 77
E. 55 , 55
Page 12 of 12