PYTHON
PYTHON
1. What is Python?
oExplain Python’s key characteristics (e.g., interpreted, dynamically-typed,
scripting language).
o Provide examples of Python’s applications.
2. What are the differences between lists and dictionaries in Python?
o Explain their structures and use cases.
3. What is the purpose of indentation in Python?
o Why is it important, and what errors can occur if it’s used incorrectly?
4. Explain the difference between local and global variables in Python.
o Provide an example of each.
5. What is the purpose of a docstring in Python?
o Write an example of a function with a docstring.
6. What is the Global Interpreter Lock (GIL) in Python?
o How does it affect threading and parallel execution?
7. What is recursion?
oExplain the difference between a base case and a recursive case.
8. What are the four principles of Object-Oriented Programming (OOP)?
o Briefly explain each principle.
python
Copy
def factorial(n):
# Base case
if _______:
return _______
# Recursive case
else:
return _______
2. Complete the following code to create a class Rectangle with
attributes length and width, and a method area():
python
Copy
class Rectangle:
def __init__(self, length, width):
self.length = _______
self.width = _______
def area(self):
return _______
3. Complete the following code to sort a list of numbers in ascending order
using the bubble sort algorithm:
python
Copy
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n-i-1):
if _______:
arr[j], arr[j+1] = _______
return arr
python
Copy
def mystery(x):
if x == 0:
return 1
else:
return x * mystery(x - 1)
print(mystery(4))
2. What is the output of the following code?
python
Copy
data = [1, 2, 3, 4, 5]
result = [x * 2 for x in data if x % 2 == 0]
print(result)
3. What is the output of the following code?
python
Copy
print(func(3))
print(func(3, 7))
python
Copy
def add_numbers(a, b)
return a + b
Copy
numbers = [1, 2, 3, 4, 5]
for i in range(len(numbers)):
print(numbers[i + 1])
3. Identify and fix the errors in the following code:
python
Copy
class Car:
def __init__(self, make, model):
self.make = make
self.model = model
def display(self):
print(f"Car: {self.make} {self.model}")
Section 5: Problem-Solving
Answer Key
Copy
1. python
Copy
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n - 1)
2. python
Copy
class Rectangle:
def __init__(self, length, width):
self.length = length
self.width = width
def area(self):
return self.length * self.width
3. python
Copy
def bubble_sort(arr):
n = len(arr)
for i in range(n):
for j in range(0, n-i-1):
if arr[j] > arr[j+1]:
arr[j], arr[j+1] = arr[j+1], arr[j]
return arr
1. Missing colon:
python
Copy
Copy
Section 5: Problem-Solving
1. python
Copy
def is_palindrome(s):
return s == s[::-1]
2. python
Copy
def find_largest(arr):
largest = arr[0]
for num in arr:
if num > largest:
largest = num
return largest
3. python
Copy
Copy
Copy
import threading
def print_numbers():
for i in range(1, 11):
print(i)
t1 = threading.Thread(target=print_numbers)
t2 = threading.Thread(target=print_numbers)
t1.start()
t2.start()
3. python
Copy
class Animal:
def __init__(self, name):
self.name = name
def speak(self):
pass
class Dog(Animal):
def speak(self):
return "Woof!"