0% found this document useful (0 votes)
10 views7 pages

PYTHON

The document is a Python programming exam consisting of theory questions, code snippet completions, code dry runs, error corrections, problem-solving tasks, and advanced topics. It covers fundamental concepts such as Python characteristics, data structures, recursion, OOP principles, threading vs multiprocessing, and practical coding exercises. An answer key is provided for each section to guide the evaluation of responses.

Uploaded by

nisssrine
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views7 pages

PYTHON

The document is a Python programming exam consisting of theory questions, code snippet completions, code dry runs, error corrections, problem-solving tasks, and advanced topics. It covers fundamental concepts such as Python characteristics, data structures, recursion, OOP principles, threading vs multiprocessing, and practical coding exercises. An answer key is provided for each section to guide the evaluation of responses.

Uploaded by

nisssrine
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Python Programming Exam

Section 1: Theory Questions

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.

Section 2: Code Snippet Completion

1. Complete the following code to calculate the factorial of a number using


recursion:

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

Section 3: Code Dry Runs

1. What is the output of the following code?

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

def func(a, b=5):


return a + b

print(func(3))
print(func(3, 7))

Section 4: Code Error Correction

1. Identify and fix the errors in the following code:

python

Copy

def add_numbers(a, b)
return a + b

result = add_numbers(5, 10)


print(result)
2. Identify and fix the errors in the following code:
python

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}")

my_car = Car("Toyota", "Corolla")


my_car.display()

Section 5: Problem-Solving

1. Write a Python function to check if a string is a palindrome.


o A palindrome reads the same backward as forward (e.g., "madam").
2. Write a Python program to find the largest number in a list.
o Do not use the built-in max() function.
3. Write a Python program to count the frequency of each element in a list.
o Example:
Input: [1, 2, 2, 3, 3, 3]
Output: {1: 1, 2: 2, 3: 3}
4. Write a Python program to implement a binary search algorithm.
o Assume the input list is already sorted.

Section 6: Advanced Topics

1. Explain the difference between threading and multiprocessing in Python.


o Provide an example use case for each.
2. Write a Python program to create two threads that print numbers from 1
to 10 concurrently.
o Use the threading module.
3. Write a Python program to demonstrate inheritance.
o Create a base class Animal and a child class Dog that inherits from Animal.

Answer Key

Section 1: Theory Questions

1. Python is an interpreted, dynamically-typed scripting language used for web


development, data analysis, AI, and more.
2. Lists are ordered collections, while dictionaries are key-value pairs.
3. Indentation defines code blocks in Python. Incorrect indentation leads
to IndentationError.
4. Local variables are defined inside a function, while global variables are defined
outside.
5. A docstring describes a function’s purpose. Example:
python

Copy

def add(a, b):


"""Returns the sum of a and b."""
return a + b
6. GIL prevents multiple threads from executing Python bytecode simultaneously,
limiting threading performance.
7. Recursion is a function calling itself. The base case stops recursion, while the
recursive case calls the function.
8. OOP principles: Encapsulation, Inheritance, Polymorphism, Abstraction.

Section 2: Code Snippet Completion

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

Section 3: Code Dry Runs

1. Output: 24 (Factorial of 4).


2. Output: [4, 8].
3. Output: 8 and 10.

Section 4: Code Error Correction

1. Missing colon:

python
Copy

def add_numbers(a, b):


return a + b
2. Index out of range:
python

Copy

for i in range(len(numbers) - 1):


3. No errors.

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

from collections import Counter


def count_frequency(arr):
return dict(Counter(arr))
4. python

Copy

def binary_search(arr, target):


low, high = 0, len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == target:
return mid
elif arr[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1

Section 6: Advanced Topics

1. Threading shares memory and is lightweight but limited by GIL.


Multiprocessing runs independently and allows true parallelism.
2. python

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!"

You might also like