Skip to content
πŸ“š Free notes, MCQ tests & past papers β€” FBISE & BISE Rawalpindi
πŸ“’ Latest Updates

10th Class Computer Science Unit 4 Control Structures in Python: Exercise Solutions 2027 (Punjab Board / BISE Rawalpindi)

Reviewed by Sadia Shahid, Senior Science teacher (Maths, Physics, Chemistry, Biology, Computer Science)

These are the solved end-of-unit exercises for Unit 4, Control Structures in Python, from the new PECTAA Computer Science and Entrepreneurship-10 textbook used by BISE Rawalpindi, Lahore and the other Punjab boards. Every answer is written from the text of the unit, for the 10th Class (SSC Part II) Annual Examination 2027.

This unit covers decision making (if, if-else and nested conditions), the while and for loops, the range() function, the end parameter of print(), nested loops, libraries such as random and statistics, lists and their methods, and testing and debugging. Every program below was run and the output shown is the real output.

Unit 4: Control Structures in Python
Class 10th Class (SSC Part II), Punjab boards
MCQs answered 10
Other questions solved 21

Exercise questions deleted in the ALP Smart Syllabus 2027

Nothing in Unit 4 is deleted in the ALP Smart Syllabus 2027. The whole unit and all of its exercise questions are in the exam.

Multiple Choice Questions

MCQ 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

Ans: (b) Creates a sequence of numbers β€” the book says the range() function generates a sequence of numbers, which is often used in for loops. (In Python 3 it makes a range object, not a ready-made list.)

MCQ 2. What is the output of this code? count = 0; for i in range(2): for j in range(3): count += 1; print(count)
(a) 4 (b) 5 (c) 6 (d) 7

Ans: (c) 6 β€” the outer loop runs 2 times and for each of them the inner loop runs 3 times, so count is increased 2 Γ— 3 = 6 times.

count = 0
for i in range(2):
    for j in range(3):
        count += 1
print(count)

Output:

6

MCQ 3. Which method is used to add an item at the end of the list in Python?
(a) insert() (b) append() (c) add() (d) extend()

Ans: (b) append() β€” append(item) adds an item to the end of the list.

MCQ 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

Ans: (b) To manage decision-making and repetition of tasks β€” control structures manage decision making and the repetition of tasks (loops).

MCQ 5. Which statement is used to execute code when a condition is false?
(a) if (b) elif (c) else (d) while

Ans: (c) else β€” in an if-else statement the else block runs when the condition is false.

MCQ 6. How do you prevent print() from adding a newline?
(a) Use end=”” (b) Use newline=False (c) Use break (d) Use continue

Ans: (a) Use end=”” β€” end is an optional parameter of print(). Setting it, for example end=” “, replaces the default newline.

MCQ 7. Which library is used to generate random numbers?
(a) Datetime (b) statistics (c) random (d) math

Ans: (c) random β€” import random gives functions such as random.randint(1, 10).

MCQ 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

Ans: (b) Adds an item to the end β€” append() adds one item at the end of the list.

MCQ 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

Ans: (b) To generate a sequence of numbers β€” range() generates a sequence of numbers (the same idea as MCQ 1).

MCQ 10. Which debugging technique involves adding output statements?
(a) Unit testing (b) Print statements (c) Error messages (d) Integration testing

Ans: (b) Print statements β€” adding print statements to check the values of variables at different stages of the code is a debugging technique.

Short Questions

Short 1. Explain the use of the range() function in a for loop.

Ans: The for loop repeats a block of code for each item of a sequence. range() produces that sequence of numbers, so the loop can run a fixed number of times. For example, for i in range(5) repeats the loop for the first five whole numbers, 0 to 4.

for i in range(5):
    print(i)

Output:

0
1
2
3
4

Short 2. What is the difference between the append and remove methods in Python lists?

append(item) remove(item)
Adds an item at the end of the list. Removes the first occurrence of the item from the list.
The list becomes longer by one item. The list becomes shorter. Only the first matching item is removed.
students = ["Ahmed", "Sara", "Ali"]
students.append("Hina")
print(students)
students.remove("Sara")
print(students)

Output:

['Ahmed', 'Sara', 'Ali', 'Hina']
['Ahmed', 'Ali', 'Hina']

Short 3. Define debugging.

Ans: Debugging is the process of finding and fixing errors (bugs) in a program. It means finding the root cause of the problem and making the changes needed to correct it.

Short 4. What are the three parameters of the range() function?

Ans: range(start, stop, step): start is the first number, stop is the number where the sequence ends (it is not included) and step is the gap between two numbers. The forms range(stop) and range(start, stop) are also allowed.

for i in range(2, 11, 2):
    print(i, end=" ")

Output:

2 4 6 8 10

Short 5. Name three list methods and their purposes.

Method Purpose
append(item) Adds an item to the end of the list.
remove(item) Removes the first occurrence of an item.
sort() Sorts the list in ascending order.
reverse() Reverses the order of the list.

Any three of these four methods can be written.

Short 6. How do you import a library in Python?

Ans: With the import statement written at the top of the program, for example import random. After that its functions are used with the library name, such as random.randint(1, 10).

import random
number = random.randint(1, 10)
print("The random number is:", number)

Output:

The random number is: 4

The number is chosen by chance, so a different value can appear each time the program runs.

Short 7. What is unit testing and why is it important?

Ans: Unit testing tests the individual parts of a program, such as functions or classes, separately from the rest. Python’s unittest module is commonly used. It is important because testing a program with different inputs shows whether it behaves as expected, so problems are found and fixed before the code is used in real situations.

Short 8. How do you access the third element in a list?

Ans: List positions (indexes) start at 0, so the third element has index 2: list_name[2].

fruits = ["Mango", "Apple", "Banana"]
print(fruits[2])

Output:

Banana

Short 9. What is the purpose of the end parameter in print()?

Ans: By default print() ends with a newline, so the next output starts on a new line. The end parameter changes what is printed at the end, so several outputs can appear on the same line.

for i in range(1, 6):
    print(i, end=" ")

Output:

1 2 3 4 5

Short 10. How can we access an element from the list?

Ans: By writing its index in square brackets after the list name. Indexing starts at 0, so fruits[1] gives the second item. (The printed question says “assess”, which means “access”.)

Short 11. How do you modify an existing item in a list?

Ans: Access the item by its index and assign a new value to it.

fruits = ["Mango", "Apple", "Banana"]
fruits[0] = "Orange"
print(fruits)

Output:

['Orange', 'Apple', 'Banana']

Long Questions

Long 1. Explain the different types of decision-making statements in Python with examples for each.

Decision making lets a program choose different actions depending on a condition, like deciding to take an umbrella if the weather looks rainy. In Python the code inside each statement must be indented.

1. if statement

Runs a block of code only if the condition is true.

temperature = 35
if temperature > 30:
    print("It's a hot day")

Output:

It's a hot day

2. if-else statement

Runs one block if the condition is true and another block if it is false.

temperature = 15
if temperature > 30:
    print("It's a hot day")
else:
    print("It's not a hot day")

Output:

It's not a hot day

3. Nested conditions

An if statement placed inside another if or else block, used to check more than one condition.

weather = "rainy"
temperature = 10
if weather == "rainy":
    if temperature < 15:
        print("Wear a raincoat")
    else:
        print("Take an umbrella")
else:
    print("Enjoy your day!")

Output:

Wear a raincoat

The program first checks whether the weather is rainy. If it is, it checks whether the temperature is below 15. Both are true, so “Wear a raincoat” is printed. If it were rainy but not below 15, it would print “Take an umbrella”, and if it were not rainy it would print “Enjoy your day!”.

Long 2. Compare while loops and for loops.

while loop for loop
Repeats as long as a condition is true. The condition is checked before every repetition. Repeats a block of code a specific number of times, once for each item of a sequence (list, tuple, string or range()).
Syntax: while condition: Syntax: for variable in sequence:
The loop variable must be changed inside the loop (for example number += 1), or the loop never stops. The variable automatically takes the next item of the sequence each time.
Used when the number of repetitions is not known in advance. Used when the number of repetitions is known, often with range().
# while loop
number = 1
while number < 10:
    print(number, end=" ")
    number += 1
print()

# for loop
friends = ["Sami", "Raza", "Moosa"]
for friend in friends:
    print("Welcome to", friend)

Output:

1 2 3 4 5 6 7 8 9 
Welcome to Sami
Welcome to Raza
Welcome to Moosa

Long 3. Describe how to work with Python lists, including creation and modification. Provide code examples for adding items, removing items and accessing elements.

A list is a data structure that holds a collection of items. It is created by placing items inside square brackets, separated by commas. Items can be numbers, strings or even other lists.

# creating a list
fruits = ["Mango", "Apple", "Banana"]
print(fruits)

# accessing items by index (starts at 0)
print(fruits[1])

# modifying an item
fruits[0] = "Orange"
print(fruits)

# adding an item at the end
fruits.append("Pineapple")
print(fruits)

# removing an item
fruits.remove("Apple")
print(fruits)

# sorting and reversing
numbers = [34, 12, 89, 5, 23]
numbers.sort()
print(numbers)
numbers.reverse()
print(numbers)

Output:

['Mango', 'Apple', 'Banana']
Apple
['Orange', 'Apple', 'Banana']
['Orange', 'Apple', 'Banana', 'Pineapple']
['Orange', 'Banana', 'Pineapple']
[5, 12, 23, 34, 89]
[89, 34, 23, 12, 5]

Summary: creating uses [ ]; accessing uses the index; modifying assigns a new value to an index; append() adds at the end; remove() deletes the first matching item; sort() arranges in ascending order; reverse() reverses the order.

Long 4. Explain the importance of testing and debugging in programming. Discuss different debugging techniques.

Importance. Testing means running the code with various inputs to check that it behaves as expected. Its goal is to find and fix issues before the code is used in real-world applications. Debugging means finding and fixing the errors (bugs) found, by identifying the root cause of the problem. Together they make sure the code works correctly and efficiently.

Types of testing:

  • Unit testing: tests individual parts such as functions or classes in isolation (Python’s unittest module).
  • Integration testing: checks how different parts of the code work together.
  • Functional testing: checks that the software behaves as expected from the user’s point of view.
  • Regression testing: makes sure new changes do not affect existing functions.

Debugging techniques:

  • Print statements: add print() lines to check the values of variables at different stages.
  • Debugging tools: use tools such as pdb (Python Debugger) to step through the code, inspect variables and follow the flow of execution.
  • Error messages: read and understand the error message to locate the source of the problem.

Long 5. How do Python libraries enhance programming efficiency? Provide an example.

Libraries are like toolboxes of ready-made tools. Instead of writing everything from scratch, a programmer imports a library and uses its tools. This saves time and effort and lets the programmer concentrate on the actual problem. The book advises importing only what you need.

Example: the statistics library calculates the mean, median and mode, which is useful in data analysis.

import statistics

data = [23, 45, 67, 89, 12, 44, 56]
mean_value = statistics.mean(data)
print("The mean value is:", mean_value)

Output:

The mean value is: 48

The seven numbers add up to 336 and 336 Γ· 7 = 48, so no loop or formula had to be written.

Long 6. Write a Python program using a loop that prints all the odd numbers between 1 and 20.

for number in range(1, 20):
    if number % 2 != 0:
        print(number, end=" ")

Output:

1 3 5 7 9 11 13 15 17 19

number % 2 != 0 is true when the remainder after dividing by 2 is not zero, which means the number is odd. Another way is for number in range(1, 20, 2), which steps by 2.

Long 7. Write a Python program that (a) generates a list of 5 random numbers between 1 and 20 and (b) uses the statistics library to calculate and print their mean.

import random
import statistics

numbers = []
for i in range(5):
    numbers.append(random.randint(1, 20))

print("Random numbers:", numbers)
print("Mean:", statistics.mean(numbers))

Output:

Random numbers: [15, 18, 15, 15, 17]
Mean: 16

The output above is one possible run. The numbers change every time because they are chosen at random.

Long 8. Write a Python program that: (a) creates the list [“Biryani”, “Nihari”, “Karahi”, “Seekh Kebabs”], (b) adds “Quorma”, (c) changes “Karahi” to “Chicken Karahi” and (d) removes “Nihari”.

dishes = ["Biryani", "Nihari", "Karahi", "Seekh Kebabs"]   # (a)
dishes.append("Quorma")                                     # (b)
dishes[2] = "Chicken Karahi"                                # (c)
dishes.remove("Nihari")                                     # (d)
print(dishes)

Output:

['Biryani', 'Chicken Karahi', 'Seekh Kebabs', 'Quorma']

“Karahi” is at index 2 (Biryani 0, Nihari 1, Karahi 2). The final list is printed after all four changes.

Long 9. Write code to print stars (asterisks) in a rectangular pattern using nested loops with three rows and five columns.

for row in range(3):          # outer loop: 3 rows
    for col in range(5):      # inner loop: 5 stars in each row
        print("*", end=" ")
    print()                   # new line after each row

Output:

* * * * * 
* * * * * 
* * * * *

For each of the 3 passes of the outer loop, the inner loop runs completely and prints 5 stars on one line (because of end=” “). The empty print() then moves to the next line.

Long 10. Write the output of the following 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()

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()

Output:

=== 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 = 30

Explanation: range(2, 4) gives i = 2 and i = 3, so only the tables of 2 and 3 are printed, although the heading says 1-5. The inner loop prints j from 1 to 10 on one line because end=” ” is used, and the formatting :2d reserves two places for the product so the numbers line up. The empty print() starts a new line after each table.

More for 10th Class Computer Science

The exercise questions are from the PECTAA Computer Science and Entrepreneurship-10 textbook. The deleted list follows the PECTAA ALP Smart Syllabus 2027. Please confirm all dates and figures on the official website: pectaa.edu.pk

Need help with 10th Class Computer Science? Join our live online classes for Matric (Punjab and Federal Board). Book a free demo class.

All chapters of this subject

πŸ“Œ Struggling with this chapter? Join our live online tuition β€” one-to-one or small group classes with experienced teachers. Book a free demo class β†’

Ask a question about this topic

Your email address will not be published. Required fields are marked *