6.100A: Practice questions for the final exam
#

Concepts
#

Questions 1 and 2
#

def check1(x):
    return x is None or len(x) == 0:
  
def check2(x):
    return len(x) == 0 or x is None
  1. What does
    check1(None)
    return?
  2. What does
    check2(None)
    return?

Question 3
#

x = 6
y = 3
print(x / y)
  1. What is printed to the screen?

Question 4
#

l = [9, 3, 2, 6, 4, 1, 5, 8, 7]
sorted(l, reverse=True)

Questions 5 to 9
#

  1. True or False: Checking if an element is in a list takes Θ(n)\Theta(n) time.
  2. True or False: Checking if a key is in a dictionary mapping
    str
    to
    int
    takes Θ(n)\Theta(n) time.
  3. True or False: Checking if a value is in a dictionary mapping
    str
    to
    int
    takes Θ(n)\Theta(n) time.
  4. True or False: If
    s
    is a string, then
    s += "more text"
    allocates memory for a new string.
  5. True or False: If
    s = "Hello world"
    , then
    s[0] = 'h'
    results in
    s
    having the value
    "hello world"
    .

Question 10
#

def f(l):
    f.append(1)

def g(l):
    l += [1]
  1. True or False: The following functions
    f
    and
    g
    are equivalent:

Programming
#

Recursion
#

def is_BST(tree: Node)
#

Given the following class definition for a

Node
, check whether or not a tree (represented by a
Node
) is a binary search tree.

A binary search tree

# tree.py
from typing import Optional

class Node:
    def __init__(self, val: int, l: Optional[Node], r: Optional[Node]):
        self.val = val
        self.left = l
        self.right = r
    
    def get_value(self):
        return self.val

    def get_left(self):
        return self.left
    
    def get_right(self):
        return self.right

def find_level_max_num(tree)
#

def cartesian_product(L)
#

def permutations(L)
#

Answers to concept questions
#

Solutions (click to hide/show)