Unit 1 - Practice Problems - Functions and Booleans

These problems are for practice. They are optional, but you are encouraged to complete them and discuss them with members of the course staff during lab and office hours.

0 - Function Calls

What is printed by the following program? (If there is an error, simply state that there is an error)

def halve(input_val):
    return input_val/2

a = 3
t = halve(a)
print(a)

1 - Function Calls

What is printed by the following program? (If there is an error, simply state that there is an error)

def triple(input_val):
    return input_val*3

print(triple(4))

2 - Function Calls

What is printed by the following program? (If there is an error, simply state that there is an error)

def quadruple(input_val):
    print(input_val)
    return input_val*4

print(quadruple(3))

3 - Function Calls

What is printed by the following program? (If there is an error, simply state that there is an error)

def in_quotations(in_string):
    return '"' + in_string + '"'

in_quotations('waterfall')

4 - Functions and Scope

What is printed by the following program? (If there is an error, simply state that there is an error)

def is_positive(in_val):
    if in_val > 0:
        return in_val
    return False

in_val = 3
print("is_positive", in_val)

5 - Functions and Scope

What is printed by the following program? (If there is an error, simply state that there is an error)

def sum_list(in_list):
    x = 0
    for i in in_list:
        x += i
    return x

x = 2
E = [3, 4, 5]
y = sum_list(E)
print(x)

6 - Functions and Scope

What is printed by the following program? (If there is an error, simply state that there is an error)

def sum_list(in_list):
    x = 0
    for i in in_list:
        x += i
    return x

x = 2
E = [3, 4, 5]
y = sum_list(E)
print(y)

7 - Functions and Scope

What is printed by the following program? (If there is an error, simply state that there is an error)

def reverse_list(in_list):
    out_list = []
    for i in range(len(in_list), 0, -1):
        out_list.append(i)
    return out_list

E = [2, 2, 7]
F = reverse_list(E)
print(F)

8 - Functions and Scope

What is printed by the following program? (If there is an error, simply state that there is an error)

def reverse_list(in_list):
    out_list = []
    for i in range(len(in_list), 0, -1):
        in_list.append(i)
    return out_list

E = [2, 2, 7]
F = reverse_list(E)
print(E)

9 - Functions and Scope

What is printed by the following program? (If there is an error, simply state that there is an error)

def longer_list(in_list1, in_list2):
    if in_list1 > in_list2:
        return in_list1
    else:
        return in_list2

E = [11, 2, 3, 5]
F = [4, 5, 6]
G = longer_list(F, E)
print(G)

10 - Functions and Scope

What is printed by the following program? (If there is an error, simply state that there is an error)

def first_longest(in_list1, in_list2):
    if len(in_list1) > len(in_list2):
        return in_list1[i]
    else:
        return in_list2[i]

W = [9, 4, 3, 1]
X = [3, 5, 8]
Y = last_longest(W, X)
print(Y)

11 - Boolean Variables

What is printed by the following program? (If there is an error, simply state that there is an error)

x = 6
y = 7
z = x > y
print(z)

12 - Boolean Variables

What is printed by the following program? (If there is an error, simply state that there is an error)

a = 6 > 5
b = not (3 > 2)
z = a or b
if z or False:
    print(z)

13 - Boolean Operations

def sample_function(a, b):
    c = a or (not b)
    if c and a:
        return b
    elif b:
        return a
    return c

Function sample_function(a,b) can be replaced by a more simple expression. What is that expression?

14 - Boolean Operations

def substantial_function(a, b):
    c = not (a or b)
    d = (not a) and (not b)
    if c or d:
        return b
    elif b:
        return a
    return c

Function substantial_function(a,b) can be replaced by a more simple expression. What is that expression?

15 - Boolean Operations

def stoic_function(a, b):
    return a
    d = not (not a or b) and a == b
    if b and not a:
        c = a ^ b
    elif d and not (a or b):
        c = not b
    else:
        return a
    return c == d

Function stoic_function(a,b) can be replaced by a more simple expression. What is that expression?