0% found this document useful (0 votes)
1 views8 pages

Python Questions

Uploaded by

virendrachand89
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)
1 views8 pages

Python Questions

Uploaded by

virendrachand89
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/ 8

Python Programming Exam Questions

String Analysis and Manipulation


1. Given:
a = "This is my first program, I 'm testing it for the first time."

o How many total characters are there in a?


o How many total words are in a?
o How many total sentences are there in a (split by comma)?
o How many characters are in the first sentence (before the comma)?
o How many characters are in the second sentence (after the comma)?

Evaluation and Data Types


2. Input two values using eval(input()), store them as a and b.
o What is the type of a?
o What is the type of b?
o What is the sum of a and b?

String Methods
3. Given a = "This is my first program":
o How many times does "is" occur?
o What is the index of "first"?
o What is the index of "my"?
o Replace "first" with "second" in a.
o What is the value of 10 == 9?

List Operations
4. For the list:

l1 = ["apple","banana","cherry"]

o What is l1 after initialization?


o What does l1[0:2] output?[14]
o What is l1 after l1.append("fruits")?
o What is l1 after l1.insert(0,"orange")?
o If you append another list l2 = ["grapes","pineapple"] to l1, what does l1 output?[15]
5. List modification:

o Remove "apple" from l1.


o Remove the item at index 1 from l1.
o Update l1[0:3] = ["watermelon","orange"]. What is l1 now?
o Assign l1[3:] = ["watermelon"]. What is l1 now?
6. List copying and extending:

o Copy a list with .copy() and show the result.


o Count "apple" in the copied list.
o Extend the list with ["d","e"] and print the result.
o What is the index of "banana" in the updated list?
7. Deep copy:

o Show what happens when you modify an element in a deep copy of a nested list.
8. List unpacking:

l1 = ["A", "B", "C"]


x, y, z = l1

o What will x, y, z equal?

Set and Dictionary Operations


9. Sets:

s1 = {"apple","banana","cherry"}

o Discard "apple" from s1. What is the resultant set?


10. Dictionaries:

d1 = {"apple":5,"banana":7,"cherry":10}

o Update "kiwi" to 50, then to "40" (string), and remove "banana". What is d1?

Data Structure Conversion


11. Convert ("apple","banana","cherry") to tuple, list, and set. Display each result.

Further Python Practice


12. Take a float input as percentage; print its value and its rounded value.
13. What is round(3.14195,2)?
14. How do you format a float variable as a string with 2 decimals using f-string?
15. What is the value of math.pi and its 3-decimal rounded form?
16. Show two ways to print marks=76, with standard and f-string print.
17. Minutes and feet conversions:
o Convert 75 minutes to hours and minutes.
o Convert 60 feet to feet and inches.
18. String and ASCII:
o ord('a')
o chr(65)

19. String escape sequences:


o Print a Windows path with double backslashes.
o Print O'donard with proper escaping.
o Print Height 6'11" , file path programs\users\admin\n\t (using a raw string).
20. String slicing:
o Reverse 'hello world!'.
o Slice beyond the length.
21. In the string "I love python and python programming", how would you:
o Replace all occurrences of 'python' with 'java'?
o Find the starting index of the first occurrence of 'python'?
o Find the starting index of the second occurrence of 'python'?
o Find the starting index of the last occurrence of 'python'?
o Count how many times 'python' appears?
22. Write a program that takes a user's text as input and replaces the slang "TTYL" with "talk to you
later".
23. Write a program that takes a 19-digit card number string (e.g., "1234 5678 9012 3456") and
prints a masked version like "**** **** **** 3456".
24. How do you split the string 'I love python, it is my first programming' into a list based on the
comma delimiter?
25. Given the list house_prices = ['$140,000', '$550,000', '$480,000'], write code to perform the
following sequential operations:
o Update the second element to '$175,000'.
o Add '$175,000' to the end of the list.
o Remove the element at index 1.
o Remove the element with the value '$140,000'.
26. Given two lists of student scores, midterm_scores and final_scores, write a program to find:
o The number of students who didn't take the final exam.
o The minimum, maximum, and average scores for both the midterm and the final.
27. How can you flatten a list of tuples like [(1,2), (3,4), (5,6)] into a single list [1, 2, 3, 4, 5, 6]?

Questions with answers


String Analysis and Manipulation
1. Given:
a = "This is my first program, I 'm testing it for the first time."

• Total characters: len(a) → 56


• Total words: len(a.split()) → 13
• Total sentences (split by comma): len(a.split(',')) → 2
• Characters in first sentence (before comma): len(a.split(',')) → 22
• Characters in second sentence (after comma): len(a.split(',')) → 34[1]
Evaluation and Data Types
2.
# Assume input: 5, 3.2
a = eval(input()) # 5
b = eval(input()) # 3.2
# Type of a: int
# Type of b: float
# Sum: 8.2

• type(a): int
• type(b): float
• a + b: 8.2
String Methods
3. Given: a = "This is my first program"
• "is" occurs: a.count("is") → 2
• Index of "first": a.index("first") → 11
• Index of "my": a.index("my") → 8
• Replace "first" with "second": a.replace("first", "second") → "This is my second program"
• Value of 10 == 9: False
List Operations
4.
l1 = ["apple","banana","cherry"]
# After initialization: ['apple', 'banana', 'cherry']
l1[0:2] # ['apple', 'banana']
l1.append("fruits") # ['apple', 'banana', 'cherry', 'fruits']
l1.insert(0, "orange") # ['orange', 'apple', 'banana', 'cherry', 'fruits']
l2 = ["grapes","pineapple"]
l1.append(l2) # ['orange', 'apple', 'banana', 'cherry', 'fruits', ['grapes','pineapple']]

5. List modification:
# Assume l1 = ['orange', 'apple', 'banana', 'cherry', 'fruits', ['grapes','pineapple']]
l1.remove("apple") # removes 'apple'
l1.pop(1) # removes 'banana' at index 1
l1[0:3] = ["watermelon","orange"] # replaces first three elements
l1[3:] = ["watermelon"] # replaces from index 3 onwards

6. List copying and extending:


lst = ['a','b','apple','c','apple']
clst = lst.copy() # ['a','b','apple','c','apple']
clst.count("apple") # 2
clst.extend(["d","e"]) # ['a','b','apple','c','apple','d','e']
clst.index("b") # 1

7. Deep copy:
import copy
nl = [[1,2],[3,4]]
dcl = copy.deepcopy(nl)
dcl = 9
# nl remains unchanged: [[1,2],[3,4]]

8. List unpacking:
l1 = ["A", "B", "C"]
x, y, z = l1
# x = 'A', y = 'B', z = 'C'

Set and Dictionary Operations


9. Sets:
s1 = {"apple","banana","cherry"}
s1.discard("apple") # {'banana','cherry'}

10. Dictionaries:
d1 = {"apple":5,"banana":7,"cherry":10}
d1["kiwi"] = 50
d1["kiwi"] = "40"
del d1["banana"]
# d1 = {'apple': 5, 'cherry': 10, 'kiwi': '40'}

Data Structure Conversion


11.
t = ("apple","banana","cherry")
list_t = list(t) # ['apple', 'banana', 'cherry']
set_t = set(t) # {'apple', 'banana', 'cherry'}

Further Python Practice


12. Input float percentage:
perc = float(input("Enter percentage: ")) # 89.5
print(perc, round(perc)) # 89.5 90

13. round(3.14195,2) → 3.14


14. f"{x:.2f}" formats float variable x with 2 decimals.
15.
import math
# math.pi -> 3.141592653589793
# round(math.pi, 3) -> 3.142

16.
marks = 76
print("marks =",marks) # Standard print
print(f"marks = {marks}") # f-string print

17. Minutes and feet conversions:


mins = 75
hrs = mins // 60 # 1
mins_left = mins % 60 # 15
# 75 mins = 1 hr 15 mins

feet = 60
ft = feet // 12 # 5
inches = feet % 12 # 0
# 60 feet = 5 feet 0 inches

18. String and ASCII:


ord('a') # 97
chr(65) # 'A'

19. String escape sequences:


print("C:\\Windows\\Program Files\\") # Use double backslashes
print("O\'donard") # O'donard
print(r'Height 6\'11" , file path programs\users\admin\n\t') # raw string

20. String slicing:


'hello world!'[::-1] # '!dlrow olleh'
'hello world!'[0:100] # 'hello world!' (no IndexError)

21. String operations:


s = "I love python and python programming"
s.replace('python','java') # Replace all
s.find('python') # 7 (first occurrence)
s.find('python', s.find('python')+1) # 16 (second)
s.rfind('python') # 16 (last)
s.count('python') #2

22. Replace slang:


msg = input()
print(msg.replace('TTYL','talk to you later'))

23. Mask card number:


card = "1234 5678 9012 3456"
print("**** **** **** " + card.split()[-1])

24. Split string on comma:


s = 'I love python, it is my first programming'
s.split(',') # ['I love python', ' it is my first programming']

25. House prices list operations:


house_prices = ['$140,000', '$550,000', '$480,000']
house_prices[^1_1] = '$175,000'
house_prices.append('$175,000')
house_prices.pop(1)
house_prices.remove('$140,000')

26. Student scores:


midterm_scores = [50, 75, 80]
final_scores = [80, 75]
# Students not in final: len(midterm_scores)-len(final_scores) = 1
min(midterm_scores), max(midterm_scores), sum(midterm_scores)/len(midterm_scores)
min(final_scores), max(final_scores), sum(final_scores)/len(final_scores)

27. Flatten list of tuples:


tuples = [(1,2), (3,4), (5,6)]
flat = [x for t in tuples for x in t] # [1,2,3,4,5,6]

You might also like