0% found this document useful (0 votes)
37 views3 pages

Python Exam Solutions

Uploaded by

Sonali Nagar
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)
37 views3 pages

Python Exam Solutions

Uploaded by

Sonali Nagar
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/ 3

Q1: What is variable in Python?

A variable is a name that refers to a value stored in memory. It can hold different data types.
x = 10
name = 'Emma'

Q2: Classify age into Child, Teenager, Adult


age = int(input("Enter age: "))
if age <= 12 and age >= 0:
print("Child")
elif 13 <= age <= 19:
print("Teenager")
elif age >= 20:
print("Adult")
else:
print("Invalid age")

Q3: Convert dictionary to list of tuples and find length


my_dict = {'name':'Emma', 'age':28, 'city':'London'}
tuples_list = list(my_dict.items())
print("List of tuples:", tuples_list)
print("Length:", len(my_dict))

Q4: Dot product of two lists


def dot_product(a, b):
return sum(x*y for x, y in zip(a, b))

print(dot_product([1,2,3], [4,5,6]))

Q5: Box plot of dataset using Matplotlib


import matplotlib.pyplot as plt
data = [7, 25, 20, 80, 31, 44, 58]
plt.boxplot(data)
plt.show()

Q6: Merge two lists and remove duplicates


a = list(map(int, input("List1: ").split()))
b = list(map(int, input("List2: ").split()))
merged = a + b
unique = []
for x in merged:
if x not in unique:
unique.append(x)
print("Merged:", unique)

Q7: Correlation matrix of student scores


import pandas as pd
data = {
'Math':[65,78,90,55,88],
'Physics':[60,80,85,50,86],
'Chemistry':[70,75,92,58,90]
}
df = pd.DataFrame(data)
print(df.corr())

Q8: Prime number check (Corrected Code)


num = int(input("Enter number: "))
if num > 1:
for i in range(2, int(num**0.5)+1):
if num % i == 0:
print(num, "is not prime")
break
else:
print(num, "is prime")
else:
print(num, "is not prime")

Q9: Find factors of a number in descending order


n = int(input("Enter number: "))
factors = []
for i in range(1, int(n**0.5)+1):
if n % i == 0:
factors.append(i)
if i != n//i:
factors.append(n//i)
factors.sort(reverse=True)
print("Factors:", factors)
print("Count:", len(factors))

Q10: Load CSV and display statistics


import pandas as pd
df = pd.read_csv("data.csv")
print(df.describe())

Q11 (a): Matrix multiplication


A = [[1,2],[3,4]]
B = [[5,6],[7,8]]
C = [[0,0],[0,0]]
for i in range(len(A)):
for j in range(len(B[0])):
for k in range(len(B)):
C[i][j] += A[i][k]*B[k][j]
print(C)

Q11 (b): Student grade system


marks = int(input("Enter marks: "))
if marks >= 90:
grade = "A"
elif 80 <= marks <= 89:
grade = "B"
elif 70 <= marks <= 79:
grade = "C"
elif 60 <= marks <= 69:
grade = "D"
else:
grade = "F"
print("Grade:", grade)
Q12 (a): Leap year check
year = int(input("Enter year: "))
if (year % 4 == 0 and year % 100 != 0) or (year % 400 == 0):
print("Leap year")
else:
print("Not a leap year")

Q12 (b): Count vowels and consonants in a string


s = input("Enter string: ").lower()
vowels = "aeiou"
v_count = c_count = 0
for ch in s:
if ch.isalpha():
if ch in vowels:
v_count += 1
else:
c_count += 1
print("Vowels:", v_count)
print("Consonants:", c_count)

You might also like