0% found this document useful (0 votes)
2 views

Computer Programming Final Exam With Keys

The document contains a code fragment with multiple syntax and runtime errors, along with their identification and corrections. It also includes outputs for three separate code snippets and provides solutions for two coding tasks: counting substring occurrences and converting a decimal number to octave representation. The corrected code and outputs are clearly presented for each section.

Uploaded by

fenotabera1233
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Computer Programming Final Exam With Keys

The document contains a code fragment with multiple syntax and runtime errors, along with their identification and corrections. It also includes outputs for three separate code snippets and provides solutions for two coding tasks: counting substring occurrences and converting a decimal number to octave representation. The corrected code and outputs are clearly presented for each section.

Uploaded by

fenotabera1233
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Part I: Find the errors in the following code fragment and identify the error as (Syntax or

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)

Error Identification (4 points)

Line number Error type Description


1 Syntax The def key work is not used properly. It
should be def rather than Def.
1 Syntax Function identifiers names shouldn’t start with
number.
2 Syntax There is a use of undefined variable. The
function parameter is myString not mystring
3 Syntax There is a use of undefined variable. The string
length identifier should be replaced with
stringLength.
3 Runtime Index error. The argument to range function
should be replaced with stringLength.
7 Runtime Type error.
Two answers:
- Put the argument to the function
inside a quote.
- Put a condition to check whether the
passed condition is a string.
8 Syntax The argument should be put inside a quote.

Write the correct code in the given box (4 points)


def PrintChars(myString):
assert type(myString) == str, "The argument should be string"
stringLength = len(myString)
for i in range(stringLength):
my_char = myString[i]
print(my_char)

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)

Write the output here

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)

Write the output here

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))

Write the output here

True

Part III: Write code

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.

Example: count_substring(“ABCDCCDC”,”CDC”) outputs 2.

def count_substring(string, sub_string):


count = 0
for i in range(len(string)):
current = string[i:i+len(sub_string)]
if(current == sub_string):
count += 1
return count

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))

You might also like