CP II Midterm 2022 Answers
CP II Midterm 2022 Answers
Student Id:
1) str = "pynative"
print (str[2:4])
What is the output of the above code? 7) x=5
A) py B) yn C) na D) yna y="5"
if x=y:
2) var1 = "1" print("Equal")
var2 = "2" else:
var3 = "3" print("Not Equal")
print(var1+ var2 + var3) What is the output of the above code?
What is the output of the above code? A) Equal B) Not Equal C) Error
A) 6 B) 33 C)123 D) Error
3) def calculate (num1, num2=4): 8) To retrieve the first character “c” from string,
res = num1 * num2 mystr="codescracker" What command do we execute?
print(res) A) mystr[1] B) mystr.getItem(1) C) mystr[0]
calculate(5)
What is the output of the above code? 9) x=[1,2,3,4,5]
A) 20 B) 30 C) Error y=x
x.append(6)
4) def rev_func(x,length): print(y[5])
if length>0: What is the output of the above code?
print(x[length-1],end=" ") A) 5 B) 6 C) Error D)-1
rev_func(x,length-1)
x=[11, 12, 13, 14, 15] 10) tinylist = [123, 'john']
rev_func(x,5) print(tinylist *
What is the output of the above code? What is the output of the above code?
A)15 14 13 12 11 B) 11 12 13 14 A) [123, 'john', 123, 'john'] B) [123, 'john'] * 2
C) Error D) 11121314 C) [246, 'johnjohn'] * 2 D) Error
14) x= [3,5,6,12,44,33,22,55,4,67,112,68]
Write a code that is going to sort the numbers in list x
and display them. Do not use the built in sort function
of Python. (Hint: You can us for loops) (15 Points)
x= [3,5,6,12,44,33,22,55,4,67,112,68]
for i in range(len(x)):
index=i
current_min=x[index]
for k in range(i+1, len(x)):
if x[k]<x[index]:
index=k
temp=x[i]
x[i]=x[index]
x[index]=temp
print(x)