Python Test
Part 1: Multiple Choice
1. For Loop What will be the output of the following code?
for i in range(3): print(i)
a) 0 1 2
b) 1 2 3
c) 0 1 2 3
d) 1 2 3 4
2. For Loop How many times will the loop execute?
for i in range(1, 6): print(i)
a) 4 times
b) 5 times
c) 6 times
d) 7 times
3. For Loop What will be the output of the following code?
for i in range(2, 8, 2): print(i)
a) 2 3 4 5 6 7
b) 2 4 6
c) 2 4 6 8
d) 2 3 4 5
4. While Loop What will be the output of the following code?
x = 0 while x < 3: print(x) x += 1
a) 0 1 2
b) 1 2 3
c) 0 1 2 3
d) 1 2 3 4
5. While Loop What is the condition to terminate the loop in the following code?
x = 10 while x > 0: print(x) x -= 2
a) x < 10
b) x > 10
c) x <= 0
d) x >= 0
6. While Loop How many times will the following loop execute?
x = 5 while x < 15: x += 2
a) 4 times
b) 5 times
c) 6 times
d) 7 times
7. Escape Characters Which of the following is the escape character for a newline?
a) \t
b) \n
c) \\
d) \r
1. Escape Characters What will be the output of the following code?
print("Hello\\nWorld")
a) Hello World
b) Hello\nWorld
c) Hello
World
d) Hello World (on a new line)
2. Escape Characters What will be the output of the following code?
print("Hello\\tWorld")
a) Hello World
b) Hello\tWorld
c) Hello World
d) Hello World (with a tab space)
3. Data Types What is the data type of the following expression?
3.14
a) int
b) float
c) str
d) bool
4. Data Types What is the data type of the following value?
True
a) int
b) float
c) str
d) bool
5. Data Types What is the type of the following variable?
my_var = [1, 2, 3]
a) list
b) tuple
c) set
d) dict
6. String Manipulation What will be the output of the following code?
my_string = "hello".upper() print(my_string)
a) HELLO
b) hello
c) Hello
d) HELLO (in lowercase)
7. String Manipulation What will be the output of the following code?
my_string = "HELLO".lower() print(my_string)
a) hello
b) HELLO
c) Hello
d) hello (in uppercase)
8. String Manipulation What will be the output of the following code?
my_string = "Hello" print(my_string[1:4])
a) He
b) Hell
c) ell
d) llo
Part 2: Fill in the Blank
1. List Fill in the blank to access the first element of the list.
my_list = [10, 20, 30] first_element = my_list[___]
2. List Fill in the blank to append an element to the list.
my_list = [10, 20, 30] my_list.___(___)
3. List Fill in the blank to access the last element of the list.
my_list = [10, 20, 30] last_element = my_list[___]
4. List Fill in the blank to remove the second element from the list.
my_list = [10, 20, 30] my_list.___(___)
5. String Manipulation Fill in the blank to get the length of the string.
my_string = "Hello, World!" length = len(___)
6. String Manipulation Fill in the blank to convert the string to uppercase.
my_string = "hello" upper_string = my_string.___()
7. String Manipulation Fill in the blank to concatenate two strings.
string1 = "Hello" string2 = "World" result = string1 + ___
8. If, Elif, Else Fill in the blank to complete the if-else statement.
x = 10 if x > 5: print("x is greater than 5") ___: print("x is not greater than 5")
9. If, Elif, Else Fill in the blank to check if a number is even.
x = 4 if x ___ 2 == 0: print("x is even") else: print("x is odd")
10. Nested If Fill in the blank to complete the nested if statement.
x = 10 if x > 5: if x < 15: print("x is between 5 and 15")
Part 3: Coding Exercises
1. For Loop Write a Python function print_even_numbers that prints all even numbers from 1 to 10.
def print_even_numbers(): # Your code here
2. For Loop Write a Python function sum_range that takes two integers, start and end, and returns the sum of all integers in that range
(inclusive).
def sum_range(start, end): # Your code here
3. While Loop Write a Python function countdown that takes an integer n as input and prints a countdown from n to 0.
def countdown(n): # Your code here
4. While Loop Write a Python function sum_numbers that takes an integer n and returns the sum of all integers from 1 to n.
def sum_numbers(n): # Your code here
5. Escape Characters Write a Python function print_escaped that prints the string "Hello\tWorld\nGoodbye".
def print_escaped(): # Your code here
6. Escape Characters Write a Python function format_string that takes a string and formats it to include a tab and a newline.
def format_string(s): # Your code here
7. List Write a Python function sum_list that takes a list of numbers as input and returns the sum of the numbers.
def sum_list(numbers): # Your code here
8. List Write a Python function remove_element that takes a list and an element, and removes the element from the list if it exists.
def remove_element(my_list, element): # Your code here
9. String Manipulation Write a Python function reverse_string that takes a string as input and returns the reversed string.
def reverse_string(s): # Your code here
10. String Manipulation Write a Python function capitalize_string that takes a string as input and returns the string with the first letter
capitalized.
def capitalize_string(s): # Your code here
11. If, Elif, Else Write a Python function check_number that takes an integer as input and prints whether the number is positive, negative, or
zero.
def check_number(num): # Your code here
12. If, Elif, Else Write a Python function max_of_three that takes three integers and returns the maximum of the three.
def max_of_three(a, b, c): # Your code here
13. Nested If Write a Python function grade_student that takes a score (0-100) and prints the grade based on the following criteria:
A: 90-100
B: 80-89
C: 70-79
D: 60-69
F: below 60
def grade_student(score): # Your code here
14. Nested If Write a Python function number_category that takes an integer and prints whether the number is positive, negative, or zero,
and if it is even or odd.
def number_category(num): # Your code here
15. Nested If Write a Python function age_category that takes an age as input and prints whether the person is a child (0-12), a teenager
(13-19), an adult (20-64), or a senior (65+).
def age_category(age): # Your code here
Part 4: Comprehensive Program
1. Comprehensive Program Write a Python program that processes student records stored in separate lists and performs the following
tasks:
For Loop: Iterate through the list of student names.
While Loop: Calculate the average grade of each student until all grades are processed.
Escape Characters: Format the output to display student details with proper spacing and new lines.
List: Store the grades of each student and perform operations on them.
Data Types: Handle different data types including strings, integers, and floats.
String Manipulation: Format and print student names in uppercase.
If, Elif, Else: Determine the grade category of each student based on their average grade.
Nested If: Further categorize the students based on their grades.
The program should output the following information for each student:
Name (in uppercase)
List of grades
Average grade
Grade category (A, B, C, D, F)
Additional comment based on the grade category (e.g., "Excellent" for A, "Needs Improvement" for F)
Sample student records:
names = ["Alice", "Bob", "Charlie", "Daisy"] alice_grades = [85, 90, 88] bob_grades = [70, 78, 65] charlie_grades
= [95, 100, 92] daisy_grades = [55, 60, 58]
Summary of Answers
Part 1: Multiple Choice
1. a) 0 1 2
2. b) 5 times
3. b) 2 4 6
4. a) 0 1 2
5. c) x <= 0
6. c) 6 times
7. b) \n
8. b) Hello\nWorld
9. c) Hello\tWorld
10. b) float
11. d) bool
12. a) list
13. a) HELLO
14. a) hello
15. c) ell
Part 2: Fill in the Blank
1. 0
2. append, 40
3. -1
4. pop, 1
5. my_string
6. upper
7. string2
8. else
9. %
10.
Part 3: Coding Exercises
1. For Loop
def print_even_numbers(): for i in range(1, 11): if i % 2 == 0: print(i)
2. For Loop
def sum_range(start, end): total = 0 for i in range(start, end + 1): total += i return total
3. While Loop
def countdown(n): while n >= 0: print(n) n -= 1
4. While Loop
def sum_numbers(n): total = 0 while n > 0: total += n n -= 1 return total
5. Escape Characters
def print_escaped(): print("Hello\tWorld\nGoodbye")
6. Escape Characters
def format_string(s): return f"{s}\t\n"
7. List
def sum_list(numbers): return sum(numbers)
8. List
def remove_element(my_list, element): if element in my_list: my_list.remove(element) return my_list
9. String Manipulation
def reverse_string(s): return s[::-1]
10. String Manipulation
def capitalize_string(s): return [Link]()
11. If, Elif, Else
def check_number(num): if num > 0: print("Positive") elif num < 0: print("Negative") else: print("Zero")
12. If, Elif, Else
def max_of_three(a, b, c): if a > b and a > c: return a elif b > c: return b else: return c
13. Nested If
def grade_student(score): if score >= 90: grade = "A" elif score >= 80: grade = "B" elif score >= 70: grade = "C"
elif score >= 60: grade = "D" else: grade = "F" print(f"Grade: {grade}")
14. Nested If
def number_category(num): if num > 0: category = "Positive" elif num < 0: category = "Negative" else: category =
"Zero" if num % 2 == 0: even_odd = "Even" else: even_odd = "Odd" print(f"{num} is {category} and {even_odd}")
15. Nested If
def age_category(age): if age >= 0: if age <= 12: category = "Child" elif age <= 19: category = "Teenager" elif
age <= 64: category = "Adult" else: category = "Senior" else: category = "Invalid Age" print(f"Age {age} is
categorized as: {category}")
names = ["Alice", "Bob", "Charlie", "Daisy"]
alice_grades = [85, 90, 88]
bob_grades = [70, 78, 65]
charlie_grades = [95, 100, 92]
daisy_grades = [55, 60, 58]
grades_list = [alice_grades, bob_grades, charlie_grades, daisy_grades]
def process_students(names, grades_list):
for i in range(len(names)):
name = names[i].upper()
student_grades = grades_list[i]
total = 0
count = 0
1 while count < len(student_grades):
2 total += student_grades[count]
3 count += 1
4
5 average = total / len(student_grades)
6
7 if average >= 90:
8 grade = "A"
9 comment = "Excellent"
10 elif average >= 80:
11 grade = "B"
12 comment = "Good Job"
13 elif average >= 70:
14 grade = "C"
15 comment = "Average"
16 elif average >= 60:
17 grade = "D"
18 comment = "Below Average"
19 else:
20 grade = "F"
21 comment = "Needs Improvement"
22
23 print(f"Name: {name}\\nGrades: {student_grades}\\nAverage: {average:.2f}\\nGrade: {grade}\\nComment: {commen
process_students(names, grades_list)