ST.
JOSEPH COLLEGE OF ENGINEERING AND TECHNOLOGY
CS 2209 - PYTHON PROGRAMMING
DEGREE CONTINUOUS ASSESSMENT TEST - I, JUNE-2024
PART A (7 x 2 = 14)
1. What do you mean by iteration?
Iteration means repeating a block of code multiple times. It is achieved using loops such as `for` and `while`
in Python.
2. Mention at least two disadvantages of flowchart:
- Complex logic becomes hard to represent.
- Not suitable for large programs due to complexity and size.
3. Write a pseudo-code for finding greatest of three numbers:
Start
Input a, b, c
If a > b and a > c:
Print a is greatest
Else if b > a and b > c:
Print b is greatest
Else:
Print c is greatest
End
4. What do you mean by list?
A list is a collection data type in Python used to store multiple items in a single variable. It is ordered,
mutable, and allows duplicates.
Example: `my_list = [1, 2, 3, 4]`
5. Mention at least two features of interactive mode:
- Executes code line by line.
- Immediate feedback of code output.
6. Write a break statement in Python with suitable example:
```python
for i in range(1, 6):
if i == 3:
break
print(i)
```
7. Write a pass statement in Python with suitable example:
```python
for i in range(5):
if i == 3:
pass
print(i)
```
PART B (3 x 4 = 12)
8(a). Algorithm to guess an integer number in a range:
Start
Set secret_number to some fixed value (e.g., 7)
Ask user to guess a number between 1 and 10
If guess == secret_number:
Print "Correct!"
Else:
Print "Wrong, try again."
End
[Flowchart will be added here]
8(b). Algorithm for factorial using recursion:
Start
Function factorial(n):
If n == 0 or n == 1:
return 1
Else:
return n * factorial(n - 1)
Input number
Call factorial(number)
End
9(a). Python program to swap two numbers:
a=5
b = 10
a, b = b, a
print("a =", a)
print("b =", b)
9(b). Difference between compiler and interpreter:
- Compiler translates the whole program before execution (e.g., C/C++).
- Interpreter translates and runs the code line-by-line (e.g., Python).
10(a). Python program to find distance between two points:
import math
x1, y1 = 1, 2
x2, y2 = 4, 6
distance = [Link]((x2 - x1)**2 + (y2 - y1)**2)
print("Distance:", distance)
10(b). Python program to check if a number is positive or negative:
num = int(input("Enter a number: "))
if num > 0:
print("Positive")
elif num < 0:
print("Negative")
else:
print("Zero")
PART C (2 x 12 = 24)
11(a). Six features in algorithmic problem solving:
1. Input - Data to the problem
2. Output - Result after processing
3. Definiteness - Clear instructions
4. Finiteness - Must terminate
5. Effectiveness - Basic, executable steps
6. Correctness - Accurate results for all inputs
11(b). Four types of operators in Python:
1. Arithmetic Operators: +, -, *, /
-a+b
2. Comparison Operators: ==, !=, <, >
- a == b
3. Logical Operators: and, or, not
- a > 3 and b < 5
4. Assignment Operators: =, +=, -=
- a += 1
12(a). Built-in and User-defined Functions
- Built-in: Predefined like len(), print()
- User-defined: Created by user using def
Example:
def greet(name):
print("Hello", name)
greet("John")
Declaration: def function_name()
Calling: function_name()
Flow: Code jumps to function, executes, returns
12(b)(i). Sum of digits using loop:
num = 123
total = 0
while num > 0:
digit = num % 10
total += digit
num = num // 10
print("Sum of digits:", total)
12(b)(ii). Find biggest of three numbers:
a, b, c = 10, 20, 5
if a > b and a > c:
print("a is greatest")
elif b > c:
print("b is greatest")
else:
print("c is greatest")