random
exam 1
exam 2
exam 3
exam 4
100

What will be printed?

def main():

    value = [99]

    show_value(value)

    print(value[0])

   

def show_value(num):

    num[0] = 0

   

main() 

0

100

The following code runs without error and prints "B".

x = 7

if x > 5:

    if x < 10:

        if x % 2 == 0:

            print("A")

        else:

            print("B")

    else:

        print("C")

else:

    print("D") 

B

100

will this run forever

try:
    n = 5 / 0
except Exception:
    n = 10
finally:
    print(n)

no it ends

100

What will the following code print?

s1 = {1, 2, 3}
s2 = {3, 4, 5}
s3 = s1 & s2
s1.add(6)
s3.add(7)
print(len(s1), len(s3))

4 2

100

free points

100

200

import sqlite3
conn = sqlite3.connect('class.db')
cur = conn.cursor()
cur.execute("CREATE TABLE IF NOT EXISTS students (id INTEGER PRIMARY KEY, name TEXT)")
cur.execute("INSERT INTO students (name) VALUES (?)", ("Lily",))
cur.execute("SELECT * FROM students WHERE name = ?", ("Lily",))
print(cur.fetchone()) 

will this run every time good

ye

200

What will be the output of the following code?

for i in range(1, 6):
    if i % 2 == 0:
        continue
    for j in range(i):
        if j == i - 1:
            print(f"Inner: {j}")

Inner: 0
Inner: 2
Inner: 4

200

What will be the output of the following code?

y = 20

def update():
    y = y + 5
    print(y)

update()
print(y)

error teehee

200

WHAT WILL THIS OUTPUT

numbers=[1,2,3,4,5,6]

result = [x * 2 for x in numbers if x % 2 != 0]
print(result)

[2, 6, 10]

200

what is the symbols for 

lists

sets 

tuples

dictionaires

[]

{}

[]

{}

300

def add_numbers(a, b=5):
    return a + b

result = add_numbers(3)
print(result)

will this work?

yes

300

What will this nested loop structure produce?

def pattern(n):
    result = []
    for i in range(n):
        if i % 2 == 0:
            inner = []
            for j in range(i+1):
                if j == 0 or j == i:
                    inner.append(1)
                else:
                    inner.append(0)
            result.append(inner)
    return result

print(pattern(4))

[[1], [1, 0, 1]]

300

text = "banana"
count = 0

for i in range(1, len(text), 2):
    if text[i] == "a":
        count += 1

print(count)

what is the output

3

300

What will be the output of this dictionary comprehension?

dictA = {'x': 4, 'y': 1, 'z': 5}
dictB = {val: key for key, val in dictA.items() if val >= 4}
print(dictB)

{4: 'x', 5: 'z'}

300

does this raise an error?

class Book:
    def __init__(self, title):
        self.title = title

    def update_title(self, title):
        title = "New Title"

b1 = Book("Introduction to programming")
b1.update_title("Data science")
print(b1.title)

NO

400

What is SQL create smth about it rn

random

400

What will be the output of the following code?

x = 5
y = 0
while x > 0 and y < 10:
    if x % 2 == 0:
        y += x
    else:
        y += 1
    x -= 1
print(x, y-1)

0 8

400

What will be the output of the following code?

list1 = [1, 2, 3]
list2 = list1
list1.extend([4, 5])
list2.append(6)

print(list1[-2])

5

400

What will the following code print?

students_dict = {'Frank': [70, 80], 'Lily': [88, 92]}
for scores in students_dict.values():
    scores[1] -= 10

print(students_dict['Frank'][0])

70

400

text = "banana"
count = 0

for i in range(1, len(text), 2):
    if text[i] == "a":
        count += 1

print(count)

40 40

500

create a SQL query that grabs employees making less then 5000, and then orders by salary descending order.

off paper

500

Consider this function using string methods. What will be printed on the screen?

def validate(text):
    for c in text:
        if not (c.isalpha() or c.isspace()):
            return False
    return True

def format_text(template, text):
    if not validate(text):
        return None
    words = text.split()
    if len(words) == 0:
        return template.strip()
       
    count = 0
    for i in range(len(template)):
        if template[i:i+2] == "{}":
            count += 1
           
    result = template
    for i in range(min(count, len(words))):
        result = result.replace("{}", words[i], 1)
    return result.title()
   
temp = "Hello {}, welcome to {}"
print(format_text(temp, "john doe python"))
print(format_text(temp, "123 test"))
print(format_text(temp, ""))

def show_value(num):

    num[0] = 0

main() 

Hello John, Welcome To Doe
None
Hello {}, welcome to {}

500

Write a Python program to manage the following inventory list for a small business:

products = ["laptop", "mouse", "keyboard", "monitor", "printer"]

Your program should:

  1. Print all the items in the products list using a for loop.
  2. Ask the user to enter a product name.
  3. Use the in operator to check whether the product exists in the list exactly as entered.
  4. If the product is found, print "Product available" and print the index of the product.
  5. If the product is not found, print "Product not available" and add the product to the list.
  6. Print the updated products list.
  7. Ask the user to enter a list index (as an integer).
  8. Use a try block to access and print the product at that index.
  9. Use an except block to catch IndexError and print "Invalid product index".

check

500

Write a class CourseManager that:

  1. Has methods to:

    • add_course(name, credits)
    • get_total_credits()
  2. Stores courses in a dictionary where:

    • key is the course name

    • value is the number of credits

  3. Includes basic error checking:
    • Do not allow courses with zero or negative credits

Example usage (You do not need to include this in your code. This section is not part of the points):

manager = CourseManager()
manager.add_course("Introduction to Programming", 3)
manager.add_course("AI for Business", 4)
manager.add_course("Data Science", 2)

print(manager.get_total_credits())   # Should print 9

work through

500

Using pickle, what is wrong with this code?

import pickle
class MyClass:
    def __init__(self):
        self.data = [1,2,3]
       
obj = MyClass()
with open('data.pkl', 'w') as f:
    pickle.dump(obj, f)

WB

M
e
n
u