CSCI 131 - Programming with Python

Final Exam Review Questions

Question 1 :

What will be the output of the following program if the input is 7, 5, 7, 4, 3, 7, 8, 0?

What this program is doing? What the function func is doing?

def func ( ):

num = input ( “Please enter the number “)

counter = 0

while ( num != 0):

if (num % 7 ==0 ):

counter = counter + 1

num = input (“Please enter the number “)

return counter

#main program

print func( )

Question 2:

What will be the output of the following program if the input is 0, -5, 5, 6, 7, -4, 3, -1?

What this program is doing?

counter = 0

product = 1

sum = 0

while ( counter < 8):

num = input (“Please enter the number “)

if (num < 0 ):

sum = sum + num

if ( num > 0):

product = product * num

counter = counter +1

print sum, product

Question 3:

What will be the output of the following program if the input is 0, -5, 5, 6, 7, -4, 3, -1?

What this program is doing? What function mystery is doing?

def mystery(num1, num2)

if num1 < num2:

return num*num2

else:

return num1+num2

#main program

counter = 0

while ( counter < 4):

a = input (“Please enter the number “)

b = input ( “Please enter the number “)

print mystery ( a, b )

Question 4:

Write a function mess (num1, num2, num3) that has three parameters. The function returns the product of three integers if the sum of two first numbers greater than the third number. The function returns the sum of three numbers if the sum of two first numbers less or equal than the third number.

Write a main program that reads 6 numbers. For each 3 numbers print the result of the function mess.

Question 5:

Write a function printOdd ( num ) that reads num numbers. The function prints odd numbers among num input numbers.

Write a main program that reads one number that indicates the value of the parameter that will be used for the function printOdd. Use function prinOdd to read input numbers and print all odd numbers among the inputs.

Question 6:

Write a program that reads one integer number and determines and prints the sum of all divisors that are less then the number itself. For example, if the input is 8, the program prints 7 ( 1 + 2 + 4), if the input is 12, the program prints 16 (1+2+3+4+6).