CSE 1310 - Exams - Midterm 2 Practice Questions


Problem 1

What does this program print?

def fx(x):
    d = x[:]
    size = len(d)
    for i in range(1, size):
        d[i-1] = d[i-1] + d[i]
        i += 1
    return d

## main ##
def main():
    d = [1, 2, 3, 4, 5]
    a = fx(d)
    for num in a:
        print(num)

main()

Problem 2

What does this program print?

def function1(num):
    my_list = [2]
    count = 1
    for i in range(0, num-1):
        my_list.append(count*my_list[count-1])
        count += 1
    return [my_list, max(my_list)]

## main ##
def main():
    n = 4
    result = function1(n)
    print(result)

main()

Problem 3

What does this program print?

def eye(n):
    result = []
    for r in range(0, n):
        row = []
        for c in range(0, n):
            if r==c:
                row.append(1)
            else:
                row.append(0)
        result.append(row)
    return result

## Main ##
def main():
    num = 3
    matrix = eye(num)
    for row in matrix:
        print(row)

main()

Problem 4

What does this program print?

def f(z):
    l = []
    for i in range(1, z+1):
        if z % i == 0:
            l.append(i)
    return l
    
## Main ##
def main():
    i = 16
    z = f(i)
    for item in z:
        print(item)

main()

Problem 5

Write a function foo that satisfies the following specifications:

A palindrome is a word or phrase that reads the same forward as backward. For example, the string 'hannah' is a palindrome. You can assume all characters are lower case letters, upper case letters, or spaces, and no other characters. Note that you will need to remove the spaces from the phrase and make all the letters the same case in order for this to work properly. For example, if the argument is the string 'go hang a salami im a lasagna hog', the function should return True.


Problem 6

Write a function 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 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

Python provides a string method, string.title(), that takes in a string as an argument and returns a new string in which the first letter of each word in the input string is capitalized. For example, if you pass the string 'this is the title of my book', it returns the string 'This Is The Title Of My Book'. Write your own function title(string) that does the same, obviously without using the built-in string.title() method.


Problem 9

What does this program print?

x = []
count = 0
while count < 10:
    t = count + 2
    if t > 5:
        x.append(count)
    else:
        x.append(t)
    count = count + 1

def main():
    for number in x[3:]:
        print(number)

main()

Problem 10

String processing: can you loop through strings and make decisions based on characters?

Write a function foo that satisfies the following specifications:


Problem 11

(Note: This problem really defines three possible midterm questions, we would not be including this entire problem as a single question. However, a midterm question at the level of difficulty of writing just foo, or just foo2, or just foo3, would be fair game).

Can you perform string slicing (also applicable to lists and tuples)?

Write a funtion foo that satisfies the following specifications:

Also, write a function foo2, that is like foo, except that it returns the slice in reverse order.

In addition, write a function foo3, that is like foo, except that it returns a string of all characters EXCEPT for the slice returned by foo.


Problem 12

(Note: the solution to this problem would be too long to be a good fit for an exam. However, it would be fair game to include an exam question that only requires support of only three commands, e.g., just supporting insert, delete, and print).

Modifying lists

Write a program that starts with an empty list and presents the user a menu that allows them to append elements, insert elements, delete elements, print the list, pop an element and print it, and find an element and print its position

This is how the program should work:


Problem 13

Nested lists - do you know how to access nested lists? What does the following program print?

L = [[1, 3, [5]], [44, 5], [8]]

print(L[0])
print(L[0][2])
print(L[0][2][0])
print(L[2])
print(L[1][1])

Problem 14

Write a function foo 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 15

Write python code that creates a list L that satisfies the following specifications: Note: there are multiple correct answers for this problem.

Problem 16

Write a function foo that satisfies the following specifications:

For example, foo(3) returns the list ['a', 'aa', 'aaa'].

Problem 17

What does this program print?

"""
assumptions:
 - L is a list of numbers.
"""
def foo(L):
    # L must have an odd number of elements, else return None.
    if (len(L) % 2) == 0:
        return None

    half_length = len(L) / 2 #note: if foo has 5 elements, half_length is 2.

    for i in L:
        smaller = 0
        larger = 0

        for j in L:
            if j <= i:
                smaller = smaller + 1
            if j >= i:
                larger = larger + 1

        if (smaller > half_length) and (larger > half_length):
            return i


print(foo([5, 10, 1, 20, 30]))

Problem 18

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 19

NOTE: Although this problem refers to "dictionaries", the "dictionaries" referred to in this problem are just lists. No knowledge of actual Python dictionaries is needed to solve this problem correctly. Actually, using actual Python dictionaries would violate the specifications of this problem.

Write a function foo that translates word-by-word from language A to language B. In particular, foo should satisfy the following specifications:

For example, if we execute the following code:
dictionary = [['house', 'casa'], ['is', 'es'], ['sky', 'cielo'], ['today', 'hoy'], ['Monday', 'lunes']]
sentence = ['today', 'is', 'Monday']
print(foo(dictionary, sentence))
we should get the output:
['hoy', 'es', 'lunes']