CSE 1310 - Exams - Midterm 1 Practice Questions


Problem 1

Write a function factorial(number) that receives an integer as an argument and computes the factorial of the integer. The factorial of an integer is the product of all the positive integers less than or equal to that integer. For example, the factorial of 5 (denoted by 5!) is 5*4*3*2*1 = 120. The function should verify the input is a positive integer and return None if it is not.


Problem 2

Write a function keep_vowels(text) that takes a string as an argument and returns a new string that only keeps the vowels and spaces of the input string and omits the consonants. You can assume that the argument only contains letters and spaces, you do not need to write code to check for that. For example, keep_vowels('hello there') should return 'eo ee'.


Problem 3

Write a function capitalize_vowels(text) that satisfies the following specifications:


Problem 4

Write a function foo(S1) that takes as argument a string S1 that contains only letters and spaces, and returns a string S2 that contains first the consonants of S1 (in the order in which they appear in S1), and then the vowels of S1 (again, in the order in which they appear in S1). For example: foo('Hello world') should return the string 'Hllwrldeoo'.


Problem 5

We define that a string S is a well-formed name of a person if it follows the conventions:

For example, "Smith, John" is a well-formed name. "Smith, John Edward" is NOT a well-formed name. "Smith-Johnson, Mary" is NOT a well-formed name. Write a function check_name that takes in as argument a string S and returns True if S is a well-formed name, and False otherwise. Don't handle the case where the argument S is not a string, but do handle correctly all cases where S is indeed a string.

Problem 6

Write a function multiple_options(N, option) that:


Problem 7

Write a program that takes a string as an argument and returns a new string that only keeps the vowels and spaces of the input string and omits the consonants. You can assume that the argument only contains letters and spaces, you do not need to write code to check for that. For example, if you passed the function 'hello there', it would return 'eo ee'.


Problem 8

Write a function named compute_differences(n1, n2, n3) that:

For example:


Problem 9

Write a function named foo(num) that:

For example:


Problem 10

Describe the execution of this program: what lines are executed; what namespaces come into existence and when; what variables are in each namespace and what are their values? Your answer must explicitly show the state of each namespace after every execution of a line.
def function1(z):       # line 1
    values = [1, 2, 3]  # line 2
    return values[z]    # line 3
                        # line 4
def function2(z, y):    # line 5
    if y < 0:           # line 6
        val = 0         # line 7
    elif y == 10:       # line 8
        val = 1         # line 9
    else:               # line 10
        val = 2         # line 11
    x = function1(val)  # line 12
    return x*z          # line 13
                        # line 14
x = 10                  # line 15
y = 2                   # line 16
print(function2(y, x))   # line 17


Problem 11

Write a function named repeat_number(s) that:

For example:

IMPORTANT: make sure that, when the input argument is an integer, the output is also an integer. In other words, make sure that, if the input argument is of type int, the return value is also of type int.


Problem 12

What does this function do? What does it return if x = 5?

def Func(x):
    total = 0
    for i in range(1, x+1):
        total += i * (i-1)
        
    return total
Your answer should be understandable by someone who does not know programming.


Problem 13

Describe the execution of this program: what lines are executed; what namespaces come into existence and when; what variables are in each namespace and what are their values? Your answer must explicitly show the state of each namespace after every execution of a line.

def counter(num, limit):          #line 1
    num += 1                      #line 2
    for x in range(1, num):       #line 3 
        print(x, end = ','),      #line 4
    print('\n')                   #line 5
    if (num ==  limit):           #line 6
        return                    #line 7
    else:                         #line 8
        counter(num, limit)       #line 9
                                  #line 10
num = 1                           #line 11
limit = 5                         #line 12
counter(num, limit)               #line 13


Problem 14

The Fibonacci sequence is 1,1,2,3,5,8, 13, ... You can see that the first and second numbers are both 1. Thereafter, each number is the sum of the previous two numbers. Write a function Fibonacci(N) that takes as input an integer N and prints the first N numbers of Fibonacci sequence.

For example, Fibonacci(15) prints the following:

1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610