Computer Programming Final Exam With Keys
Computer Programming Final Exam With Keys
Runtime error). Finally write the correct code in the given space. (8 points)
Note: The first line of code is line number 1.
1.
Def 1PrintChars(myString):
stringLength = len(mystring)
for i in range(stringlength + 1)
my_char = myString[i]
print(my_char)
PrintChars("hi")
PrintChars(1234)
PrintChars("this is fun)
PrintChars("hi")
PrintChars("1234")
PrintChars(1234)
1
Part II: Write the output of the following programs in the provided space (7 points each)
1.
sum = 0
for i in range(2,12,4):
for j in range( i, 0 , -2):
sum+= i + j
print("i = ",i," j = ",j," sum = ", sum)
if(sum > 50):
break
print("Finally sum = ", sum)
i = 2 j = 2 sum = 4
i = 6 j = 6 sum = 16
i = 6 j = 4 sum = 26
i = 6 j = 2 sum = 34
i = 10 j = 10 sum = 54
Finally sum = 54
2.
count=10
def myFun(count):
for i in range(10):
if(i%3 == 0 or i%2 == 0):
continue
else:
while(count >= i ):
print(count , "-" , i , "=" , count-i)
count=int(count/2)
print(i , "+" , count , "=" , count+i)
myFun(6)
6-1=5
3-1=2
1-1=0
1+0=1
5+0=5
7+0=7
2
3.
def check_number(n):
cond=False
assert n>1
if n==2:
cond=True
for num in range(2, n):
if n%num==0:
cond=True
break
return cond
n=6
print( check_number(n))
True
1. (7 points) Write a function the number of times a sub string occurs inside a string. The function
has two parameters the first is string which is actual string and the second is the substring.
NOTE: String letters are case-sensitive.
3
2. (8 points) Write a function to convert a decimal representation of a positive integer to octave
representation using while loop. (Hint: Octave representation is the number representation to
the base eight).
Note: The output should also be type int.
Example:
The output of the function call: convert_to_octave(10) is 12.
def convert_to_octave(n):
octave = 0
i = 1
while(n != 0):
rem = n%8
octave = (rem * i) + octave
n = n//8
i = i * 10
return octave
print(convert_to_octave(1))