Python Basics
Loops and Recursion
Structures and Logic
Searching and Sorting
Hashing, Files, Dataclass
100

Python is a _____ typed language

Statically

100

range(1, 10, 3) includes what numbers?

1, 4, 7

100

Given the string s="JARED", what do the following equate to:

 - s[0:2]

- s[0:-1]

- s[0::2]

- s[::]

- s[::-1]

- JA

- JARE

- JRD

- JARED

- DERAJ

100

Can binary search be done on any list?

No, the list must be sorted first
100

What would a data class for your SI Leader look like?

(potential answer)

@dataclass

class SI_Leader:

    name: str

    age: int

    major: str

200

What is the result of 10/3? What is the result of 10//3?

- 3.33

- 3

200

What does a base case do?

It stops the recursion from continuing the work

200
Define what a set is and what a dictionary is in Python

- Set: a collection of unordered, unindexed, unique values

- Dictionary: a collection of unordered, unique (key, value) pairs

Bonus Points: What are the access time complexities of both?

200

What are the time complexities of binary search and linear search?

Binary: O(logn)

Linear: O(n)

200

What method removes all leading and trailing whitespace of a string? What method can split a string into a list of strings?

- strip()

- split()

300

What is the result of 5*2? What is the result of 5**2?

- 10

- 25

300

Write a function, using a loop, to sum all the numbers between 1-n

def sum(n :int) -> int:

    total = 0

    for num in range (1,n):

        total+= num

    return total

300

Define a tuple and list in Python

Tuple: a collection of ordered and immutable values

List: a collection of ordered and mutable values

Both are heterogeneous (able to store varied types) 

300

What are the time complexities of the following in regards to a list:

- creating a new list

- appending 

- pop() and insert()

- O(n)

- O(1)

- O(1)

300

What are the 2 different ways of handling collisions in a hash table? What's the difference between the 2?

- Open Addressing: if there's a collision, the value is stored in the next available space in the hash table


- Chaining: if there is a collision, the value is stored in a list stored at the same index where the collision occurred

400

What denotes the blocking of code in Python?

White space

400

Write a recursive function to sum all the numbers between 1-n

def sum_rec(n:int)->int:

    if n == 1:

        return 1

    return n + sum_rec(n-1)    # will include n; to exclude n, do n-1

400
Given s="JARED", what do the following equal:


- x = len(s)

- y = s[-len(s)]

- s[2] = 'P'

- x = 5

- y = D

- ERROR/ILLEGAL, strings are immutable!

400

What are the best, avg, and worst time complexities of

- selection sort

- insertion sort

Selection Sort: Best, Avg, Worst - O(n^2)

Insertion Sort: Best - O(n); Avg, Worst - O(n^2)

400

How would you read from a file called "exam_review.txt"? How would you write to the same file?

with open("exam_review.txt", "r") as file:

    content = file.read()

    file.write("Hello there")


(if we didn't use 'with open', it would look like)

file = open("exam_review.txt")

content = file.read()

file.write("Hello there")

file.close()

500

What symbols can be used to create valid variable names?

Numbers, characters, _

500

Write the substitution trace for the following recursive function:


def x2 (n:int)->int:

    if n == 0:

        return 0

    return 2 * x2(n-1)


print( x2(3) )

x2 (3) = 2 * x2(2)

          = 2 * (2 * x2(1) )

          = 2 * (2 * (2 * x2(0) ) )

          = 2 * (2 * (2 * 0) )

          = 2 * (2 * 0)

          = 2 * 0

          = 0

500

Given the dictionary, bag = {'apple': 6, 'banana': 7, 'lime': 2}, what do the following equate to:


- bag['apple']

- bag.keys()

- 'lime' in bag

- 3 in bag

- bag.values()

- 6

- apple, banana, lime

- True

- False

- 6, 7, 3

500

What are the best, avg, and worst time complexities of

- mergesort

- quicksort

Mergesort: O(nlogn)

Quicksort: Best and Avg - O(nlogn); Worst - O(n^2) 

500

When do we rehash? What does rehashing mean?

When the load factor exceeds a certain threshold. We expand the size of the table and then hash the values already in the hash table again (rehash) and put them in the new table. We rehash to reduce the number of collisions