0% found this document useful (0 votes)
3 views9 pages

Basic Python Test

The document is a 3-hour practice exam for Python programming, consisting of various sections that cover basics, lists, strings, functions, and mixed problems. Each section contains specific programming tasks with input/output requirements, constraints, and evaluation criteria. The exam emphasizes writing complete, runnable Python programs while managing time effectively and handling edge cases.

Uploaded by

akshajgoel297
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)
3 views9 pages

Basic Python Test

The document is a 3-hour practice exam for Python programming, consisting of various sections that cover basics, lists, strings, functions, and mixed problems. Each section contains specific programming tasks with input/output requirements, constraints, and evaluation criteria. The exam emphasizes writing complete, runnable Python programs while managing time effectively and handling edge cases.

Uploaded by

akshajgoel297
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

Python Programming - 3 Hour Practice Exam

Total Time: 180 Minutes | Total Marks: 100

Instructions
Write complete, runnable Python programs for each question

Use proper variable names and indentation

All input/output should use input() and print()

Handle edge cases where mentioned

Time management: Allocate roughly 30 minutes per section

Do NOT use external libraries unless explicitly mentioned

Section A: Basics & I/O (20 marks)


Q1. Temperature Conversion (5 marks)
Write a program that reads a temperature in Celsius from the user and converts it to both Fahrenheit and
Kelvin.

Formula:

Fahrenheit = (C × 9/5) + 32

Kelvin = C + 273.15

Input: 25

Output:

Temperature in Celsius: 25
Temperature in Fahrenheit: 77
Temperature in Kelvin: 298.15

Constraints:

Accept decimal values

Display Fahrenheit as an integer (round down)

Display Kelvin with 2 decimal places

Q2. Simple Calculator with Division Check (8 marks)


Write a program that takes two numbers and an operator (+, -, *, /) from the user and performs the operation.

Input:
20
/
5

Output:

20 / 5 = 4

Special Requirements:

Check for division by zero and print "Error: Division by zero" if detected

For division, display the result as an integer (whole number only)

For other operations, display exact results

Use if-elif-else statements, not a dictionary

Q3. Input Validation Loop (7 marks)


Write a program that repeatedly asks the user to enter a number between 1 and 100. Keep asking until they
enter a valid number, then print "Valid number entered: X" and exit.

Sample Run:

Enter a number between 1 and 100: 150


Invalid! Try again.
Enter a number between 1 and 100: 50
Valid number entered: 50
Number of invalid attempts: 1

Constraints:

Use a while loop

Count how many invalid attempts were made and print it at the end

Handle non-integer inputs gracefully

Section B: Lists & Basic Loops (25 marks)


Q4. List Sum and Average (6 marks)
Write a program that:

1. Takes the size of a list from the user (max 100)

2. Takes that many integers as input

3. Calculates and prints the sum and average

Input:
Enter list size: 5
Enter 5 numbers: 10 20 30 40 50

Output:

Sum: 150
Average: 30.0

Constraints:

Use a single loop for both input and calculation (efficiency counts)

Handle case where size is 0 (print "List is empty")

Average should be a float

Q5. Find Largest and Smallest (6 marks)


Write a program that finds the largest and smallest elements in a list, along with their positions (indices).

Input:

Enter list size: 6


Enter elements: 45 12 78 12 99 34

Output:

Largest element: 99 at index 4


Smallest element: 12 at index 1

Constraints:

If there are multiple occurrences, print the first index

Size will be at least 1

Do NOT use built-in max() or min() with index

Q6. Reverse List (6 marks)


Write a program that reverses a list and prints both original and reversed versions.

Input:

Enter list size: 5


Enter elements: 1 2 3 4 5

Output:

Original list: [1, 2, 3, 4, 5]


Reversed list: [5, 4, 3, 2, 1]
Constraints:

Create a new reversed list (don't modify in-place)

Do NOT use the .reverse() method

Use slicing or manual loops

Q7. Count and Replace (7 marks)


Write a program that:

1. Takes a list of integers

2. Asks the user for a number to search for

3. Counts occurrences and replaces all with 0

4. Prints the modified list and count

Input:

Enter list size: 7


Enter elements: 5 3 5 7 5 2 5
Enter number to replace: 5

Output:

Occurrences of 5: 4
Modified list: [0, 3, 0, 7, 0, 2, 0]

Constraints:

Handle case where number is not found

Modify the original list

Use a loop, not .replace() or list comprehension (yet)

Section C: Strings & String Manipulation (25 marks)


Q8. String Length & Reverse (6 marks)
Write a program that:

1. Reads a string from the user

2. Prints its length

3. Prints the string in reverse without using slicing

Input: Hello

Output:
String: Hello
Length: 5
Reversed: olleH

Constraints:

Do NOT use slicing [::-1]

Use manual loops and character access

Handle spaces if present

Q9. Vowel Counter (6 marks)


Write a program that:

1. Reads a string

2. Counts vowels (both uppercase and lowercase)

3. Counts consonants

4. Counts digits

5. Counts other characters

Input: Hello123World!

Output:

Vowels: 3
Consonants: 7
Digits: 3
Others: 1

Constraints:

Vowels = a, e, i, o, u (both cases)

Consonants = alphabetic but not vowels

Use .isdigit() , .isalpha() methods

Q10. String Comparison & Palindrome (7 marks)


Write a program that:

1. Takes a string from the user

2. Checks if it's a palindrome (ignoring spaces and case)

3. Prints "Palindrome" or "Not a palindrome"

Input: A man a plan a canal Panama

Output:
Is palindrome (ignoring spaces and case): Yes

Input: Hello

Output:

Is palindrome (ignoring spaces and case): No

Constraints:

Ignore spaces in comparison

Ignore uppercase/lowercase differences

Use loops, not slicing for reversal

Q11. Character Frequency (6 marks)


Write a program that takes a string and finds the most frequently occurring character (excluding spaces).

Input: programming is fun

Output:

Most frequent character: 'r' (appears 2 times)

Constraints:

Ignore spaces

Case-sensitive (treat 'A' and 'a' as different)

If there's a tie, print the first one found

Do NOT use .count() method

Section D: Functions & Logic (20 marks)


Q12. Prime Number Checker (5 marks)
Write a function that checks if a number is prime. Use it in a program that:

1. Takes a number from the user

2. Calls the function

3. Prints whether it's prime or not

Input: 17

Output: 17 is a prime number

Input: 20

Output: 20 is not a prime number


Constraints:

Function should be: def is_prime(n):

Handle edge cases (n ≤ 1, n = 2)

Optimize: check divisibility only up to √n

Q13. GCD using Recursion (6 marks)


Write a recursive function to find the GCD (Greatest Common Divisor) of two numbers using Euclid's algorithm.

Input:

48
18

Output: GCD of 48 and 18 is 6

Constraints:

Function: def gcd(a, b):

Use recursion (base case: when b = 0, return a)

Euclid's formula: gcd(a, b) = gcd(b, a % b)

Do NOT use [Link]()

Q14. Power Function (4 marks)


Write a function that calculates x raised to the power of n (x^n). Handle negative exponents.

Input:

Base: 2
Exponent: -3

Output: 2^-3 = 0.125

Constraints:

Function: def power(x, n):

For negative exponents, use 1 / (x^|n|)

Display with 3 decimal places

Do NOT use ** operator (implement manually with loops)


Q15. Fibonacci Series (5 marks)
Write a program that prints the first n Fibonacci numbers using a function.

Input: 10

Output:

First 10 Fibonacci numbers:


0 1 1 2 3 5 8 13 21 34

Constraints:

Function: def fibonacci(n):

Return a list of first n Fibonacci numbers

Handle n = 0 and n = 1 edge cases

Use iterative approach (not recursion—too slow)

Section E: Mixed Problems (10 marks)


Q16. Matrix Transpose (5 marks)
Write a program that takes a 3×3 matrix and prints its transpose.

Input:

Enter 3x3 matrix:


1 2 3
4 5 6
7 8 9

Output:

Transposed matrix:
1 4 7
2 5 8
3 6 9

Constraints:

Use lists of lists (2D lists)

Print matrices in proper row-column format

Do NOT use NumPy


Q17. Bubble Sort (5 marks)
Write a program that sorts a list in ascending order using bubble sort.

Input:

List size: 6
Enter elements: 64 34 25 12 22 11

Output:

Sorted list: [11, 12, 22, 25, 34, 64]


Number of swaps performed: 15

Constraints:

Implement bubble sort manually (nested loops)

Count the total number of swaps

Do NOT use .sort() or sorted()

Answer Format
For each question, write your complete Python code. Example:

# Your code here


name = input("Enter your name: ")
print(f"Hello, {name}!")

Evaluation Criteria
Correctness: Does the program produce correct output?

Efficiency: Is the algorithm reasonably efficient?

Code Quality: Proper indentation, variable naming, comments

Edge Case Handling: Does it handle special/boundary cases?

Completeness: Is the entire solution provided?

Good luck! Remember: write clean code, test with different inputs, and manage your time wisely.
You've got this!

You might also like