Midterm material
Recursion and List
OOP
Sort and Searching
Exception and file handling
100

How many else blocks can an if have?

One

100

What is the output?


nums = [10, 20, 30, 40]

print(nums[2])

30

100

What is the difference between an accessor method and a mutator method?

An accessor reads/returns an object’s data without changing it. A mutator changes the object’s data.

100

What is the main requirement for binary search?


The data must already be sorted

100

What is the difference between "w" mode and "a" mode when opening a file?

"w" writes new contents and erases existing contents, while "a" adds new data to the end of the file without removing existing contents.

200

What is the output?

def calculate(x, y):          # start function

    x = x + 2                 # inside function

    y = y * x                 # inside function

    return y - x              # inside function

# end function

a = 3

b = 4

print(calculate(a, b))

15

200

What does this print?


def mystery(n):

    if n == 0:

        print("Go")

    else:

        print(n)

        mystery(n - 1)


#This is out of the function

mystery(3)

3

2

1

Go

200

what is the output?

class Car:                         # start class

    def __init__(self, speed):     # start constructor

        self.__speed = speed

    # end constructor


    def accelerate(self, n):       # start method

        self.__speed += n

    # end method


    def get_speed(self):           # start method

        return self.__speed

    # end method

# end class


a = Car(10)

b = a

b.accelerate(20)

print(a.get_speed())

30

200

What is the main difference between linear search and binary search?

Linear search checks values one by one.
Binary search repeatedly eliminates half of the remaining values.

200

What is an exception, and what happens if an exception is not handled?


An exception is a problem that occurs while a program is running. If it is not handled, the program stops and prints a traceback. 


300

total = 0


for i in range(1, 5):          # start for loop

    for j in range(i):         # start inner for loop

        total += j             # inside inner for loop

    # end inner for loop


    total += i                 # inside outer for loop

# end outer for loop


print(total)

20

300

What is the output?

def mystery(n):

    if n == 0:

        return 1

    return n + mystery(n - 1)


#out of the function

print(mystery(4) * 2)

22

300

Explain the difference between an instance variable and a class variable.


An instance variable belongs to one object; each object has its own value.
A class variable is shared by all objects of the class. 




300

Explain the main difference between selection sort and merge sort.

Selection sort repeatedly finds the smallest remaining value and puts it in its correct position. 

Merge sort repeatedly divides the list into halves, sorts them, and then merges them back together. 

300

What is the final content of scores.txt?

Assume it initially contains 80.


with open("scores.txt", "a") as file:

    file.write("\n90")

# end with


with open("scores.txt", "w") as file:

    file.write("100\n")

    file.write("70")

# end with

with open("scores.txt", "a") as file:

    file.write("\n60")

# end with

100

70

60