10th Class Computer Science Notes 2026 — Chapter 4
Class 10 Computer Science — Chapter 4: Control Structures in Python
Board: Punjab Board
Exercise Solutions
Multiple Choice Questions (MCQs)
1. What does the range() function do in Python?
(a) Generates a list of numbers
(b) Creates a sequence of numbers
(c) Calculates the sum of numbers
(d) Prints a range of numbers
Correct Answer: (b) Creates a sequence of numbers
2. What is the output of the following code?
count = 0for i in range(2): for j in range(3): count += 1print(count)(a) 4
(b) 5
(c) 6
(d) 7
Correct Answer: (c) 6
3. Which method is used to add an item at the end of a list in Python?
(a) insert()
(b) append()
(c) add()
(d) extend()
Correct Answer: (b) append()
4. What is the purpose of control structures in programming?
(a) Only to make code longer
(b) To manage decision-making and repetition of tasks
(c) To create variables
(d) To import libraries
Correct Answer: (b) To manage decision-making and repetition of tasks
5. Which statement is used to execute code when a condition is false?
(a) if
(b) elif
(c) else
(d) while
Correct Answer: (c) else
6. How do you prevent print() from adding a newline?
(a) Use end=""
(b) Use newline=False
(c) Use break
(d) Use continue
Correct Answer: (a) Use end=""
7. Which library is used to generate random numbers?
(a) datetime
(b) statistics
(c) random
(d) math
Correct Answer: (c) random
8. What does list.append() do?
(a) Removes the first item
(b) Adds an item to the end
(c) Sorts the list
(d) Reverses the list
Correct Answer: (b) Adds an item to the end
9. What is the purpose of the range() function?
(a) To create a list of numbers
(b) To generate a sequence of numbers
(c) To calculate the range of values
(d) To sort numbers
Correct Answer: (b) To generate a sequence of numbers
10. Which debugging technique involves adding output statements?
(a) Unit testing
(b) Print statements
(c) Error messages
(d) Integration testing
Correct Answer: (b) Print statements
Short Answer Questions
1. Explain the use of the range() function in a for loop.
Answer:
The range() function generates an immutable sequence of numbers. It is commonly used in for loops to control the number of iterations. It accepts up to three arguments: range(start, stop, step).
Example:
for i in range(1, 6): print(i)This loop runs five times, with i taking values from 1 to 5.
2. What is the difference between the append() and remove() methods in Python lists?
Answer:
append(item): Adds a single element to the end of a list, increasing its length by one.remove(value): Searches for and deletes the first occurrence of a specified value from the list, reducing its length by one.
3. Define debugging.
Answer:
Debugging is the process of detecting, diagnosing, and resolving errors, such as syntax, runtime, or logical errors, in computer software so that it operates correctly.
4. What are the three parameters of the range() function?
Answer:
Start: The starting integer of the sequence. It is inclusive and defaults to
0if omitted.Stop: The ending value of the sequence. It is exclusive, meaning the sequence stops before reaching this value.
Step: The increment or decrement between numbers. It defaults to
1.
5. Name three list methods and explain their purposes.
Answer:
append(item): Adds an item to the end of a list.sort(): Sorts the list items in ascending or alphabetical order.pop(index): Removes and returns the item at the specified index.
6. How do you import a library in Python?
Answer:
A library is imported using the import keyword followed by the library name.
Examples:
import randomimport statistics7. What is unit testing, and why is it important?
Answer:
Unit testing involves testing individual components, methods, or functions of a program independently to ensure that each part works correctly. It is important because it helps detect bugs early and prevents regression when code is updated.
8. How do you access the third element in a list?
Answer:
Python lists use zero-based indexing, so the third element is accessed using index 2.
Example:
my_list[2]9. What is the purpose of the end parameter in print()?
Answer:
The end parameter specifies what is printed at the end of a print() statement instead of the default newline character (\n). Setting end="" keeps subsequent output on the same line.
Example:
print("Hello", end="")print(" World")Output:
Hello World10. How can we access an element from a list?
Answer:
An element in a list is accessed by placing its index inside square brackets after the list variable name.
Example:
my_list[index]11. How do you modify an existing item in a list?
Answer:
An item is modified by referring to its index and assigning a new value using the assignment operator (=).
Example:
my_list[1] = "Updated Value"This updates the second item in the list.
Long Answer Questions
1. Explain the different types of decision-making statements in Python with examples.
Answer:
Decision-making statements control the flow of program execution according to specified conditions.
1. if Statement
The if statement executes a block of code only when the given condition evaluates to True.
score = 85if score >= 50: print("Pass")2. if-else Statement
The if-else statement provides an alternative block of code that executes when the condition evaluates to False.
age = 16if age >= 18: print("Eligible to vote")else: print("Not eligible to vote")3. if-elif-else Statement
The if-elif-else statement checks multiple conditions in sequence until one evaluates to True. If none is true, the else block executes.
marks = 75if marks >= 80: print("Grade A")elif marks >= 60: print("Grade B")else: print("Grade C")2. Compare while loops and for loops.
Answer:
Feature |
|
|
|---|---|---|
Primary use | Used when iterating over a sequence or range. | Used when execution depends on a condition remaining |
Iterations | Generally determined by the sequence or range. | Continues until the condition becomes |
Variable management | Automatically manages the iteration variable. | Often requires manual initialization and updating of a variable. |
3. Describe how to work with Python lists, including creation and modification. Provide code examples for adding, removing, and accessing elements.
Answer:
Lists are ordered, mutable collections of items enclosed in square brackets ([]). They allow programmers to store, access, add, and remove elements.
Creation of a List
fruits = ["Apple", "Banana", "Mango"]Accessing Elements
List elements are accessed using their index numbers. Indexing starts from 0.
first_item = fruits[0]print(first_item)Output:
AppleAdding Items
The append() method adds an item to the end of the list.
fruits.append("Orange")Removing Items
The remove() method deletes the first occurrence of the specified value.
fruits.remove("Banana")Modifying Items
An existing item can be changed by assigning a new value to its index.
fruits[1] = "Grapes"4. Explain the importance of testing and debugging in programming. Discuss different debugging techniques.
Answer:
Importance of Testing and Debugging
Testing ensures that a program works correctly, prevents unexpected crashes, and verifies that it meets the required specifications. Debugging identifies and removes errors and logical flaws, improving software reliability.
Debugging Techniques
Print Statement Debugging: Inserting
print()statements at important points in the program to track variable values and execution.Code Inspection (Dry Run): Manually tracing the program step by step, often on paper, to understand its logic.
Interactive Debuggers (
pdb): Using debugging tools to set breakpoints, pause execution, and inspect program variables and the call stack.
5. How do Python libraries enhance programming efficiency? Provide an example.
Answer:
Python libraries improve programming efficiency by providing pre-written and tested functions and modules for performing complex operations. They save time and reduce the need to write repetitive code.
Example:
The math library provides a function for calculating square roots.
import mathprint(math.sqrt(49))Output:
7.06. Write a Python program using a loop that prints all the odd numbers between 1 and 20.
Answer:
# Program to print odd numbers between 1 and 20for num in range(1, 21, 2): print(num, end=" ")Output:
1 3 5 7 9 11 13 15 17 197. Write a Python program that generates a list of five random numbers between 1 and 20 and calculates their mean using the statistics library.
Answer:
import randomimport statistics# Generate five random numbers between 1 and 20random_nums = [random.randint(1, 20) for _ in range(5)]print("Generated Numbers:", random_nums)# Calculate and print the meanmean_value = statistics.mean(random_nums)print("Mean of Numbers:", mean_value)8. Write a Python program to perform the following operations:
(a) Create a list of popular Pakistani dishes: ["Biryani", "Nihari", "Karahi", "Seekh Kebabs"].
(b) Add "Quorma" to the list.
(c) Change "Karahi" to "Chicken Karahi".
(d) Remove "Nihari" from the list.
Answer:
# (a) Create the listdishes = ["Biryani", "Nihari", "Karahi", "Seekh Kebabs"]# (b) Add "Quorma"dishes.append("Quorma")# (c) Change "Karahi" to "Chicken Karahi"karahi_index = dishes.index("Karahi")dishes[karahi_index] = "Chicken Karahi"# (d) Remove "Nihari"dishes.remove("Nihari")# Display the updated listprint("Final Dishes List:", dishes)Output:
Final Dishes List: ['Biryani', 'Chicken Karahi', 'Seekh Kebabs', 'Quorma']9. Write code to print stars (*) in a rectangular pattern with three rows and five columns using nested loops.
Answer:
# Program to print a 3 × 5 star rectanglefor i in range(3): for j in range(5): print("*", end="") print()Output:
*****
*****
*****10. Write the output of the following code.
Code:
print("=== Multiplication Table (1-5) ===")for i in range(2, 4): for j in range(1, 11): print(f"{i} x {j} = {i*j:2d}", end=" ") print()Answer:
=== Multiplication Table (1-5) ===
2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 2 x 4 = 8 2 x 5 = 10 2 x 6 = 12 2 x 7 = 14 2 x 8 = 16 2 x 9 = 18 2 x 10 = 20
3 x 1 = 3 3 x 2 = 6 3 x 3 = 9 3 x 4 = 12 3 x 5 = 15 3 x 6 = 18 3 x 7 = 21 3 x 8 = 24 3 x 9 = 27 3 x 10 = 30Notes by Sir Dilawar
For more notes, visit QuranEducationPoint.com
