Test 6: Long Questions

Long questions

10 questions · 50 min · 59 marks

Want to try first? Take the free Computer Science test: 94hbp-7gz9m-ufent-tsj4r

Sample questions with answers

The first 5 of 10, each with a model answer. Try writing yours first.

  1. Question 1 · 8 marks

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

    Show model answer

    Decision making allows a program to choose different actions based on conditions. Python provides three forms.

    1.if Statement

    Runs a block of code only if the condition is true. Indentation is mandatory (otherwise IndentationError).

    Syntax:

    if condition:

    code to run if the condition is true

    Example:

    temperature = 35

    if temperature > 30:

    print("It's a hot day")

    Output: It's a hot day

    2.if-else Statement

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

    Syntax:

    if condition:

    code to run if the condition is true

    else:

    code to run if the condition is false

    Example:

    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

    Checking multiple conditions inside another condition is called nesting.

    Syntax:

    if condition1:

    if condition2:

    code if both are true

    else:

    code if condition1 is true but condition2 is false

    else:

    code if condition1 is false

    Example:

    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

  2. Question 2 · 6 marks

    Compare while loops and for loops.

    Show model answer

    Both are looping constructs that repeat actions, making code more efficient and easier to read.

    while loop:

    - Runs as long as a condition is true; the condition is checked before each iteration and the loop stops when it is no longer true.

    - Syntax: while condition: code

    - Needs the programmer to update the variable (e.g. number += 1), otherwise it may run forever.

    Example:

    number = 1

    while number < 10:

    print(number)

    number += 1

    Output: 1 to 9

    for loop:

    - Repeats a block of code a specific number of times and is commonly used to iterate over a sequence (list, tuple, string).

    - Syntax: for variable in sequence: code

    - Often used with range() to generate numbers.

    Example:

    friends = ["Sami", "Raza", "Moosa"]

    for friend in friends:

    print("Welcome to ", friend)

    Key difference: a while loop is condition-based, while a for loop is sequence-based. Both can be nested.

  3. Question 3 · 8 marks

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

    Show model answer

    A list is a versatile data structure that can hold a collection of items of different types (numbers, strings or even other lists).

    1.

    Creating a list: place items inside square brackets [ ] separated by commas.

    fruits = ["Mango", "Apple", "Banana"]

    print(fruits)

    2.

    Accessing elements: use the index, which starts from 0.

    print(fruits[1])

    Output: Apple

    3.

    Modifying an item: access it by index and assign a new value.

    fruits[0] = "Orange"

    4.

    Adding items: append(item) adds an item to the end.

    fruits.append("Pineapple")

    print(fruits)

    Output: ['Orange', 'Apple', 'Banana', 'Pineapple']

    5.

    Removing items: remove(item) removes the first occurrence of the item.

    students = ["Ahmed", "Sara", "Ali", "Hina", "Sara"]

    students.remove("Sara")

    print(students)

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

    6.

    Other operations: sort() sorts in ascending order and reverse() reverses the order.

    numbers = [34, 12, 89, 5, 23]

    numbers.sort()

    print(numbers)

    Output: [5, 12, 23, 34, 89]

  4. Question 4 · 8 marks

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

    Show model answer

    Importance: Testing and debugging are essential practices to ensure that code works correctly and efficiently.

    Testing is the process of running code with various inputs to check if it behaves as expected. Its goal is to find and fix issues before the code is used in real-world applications.

    Types of testing:

    - Unit Testing: tests individual parts (functions or classes) in isolation; the unit test module is commonly used.

    - Integration Testing: checks how different parts work together.

    - Functional Testing: validates that the software behaves as expected from the user's perspective.

    - Regression Testing: ensures new changes don't affect the existing functionality.

    Debugging is the process of finding and fixing errors (bugs). It involves identifying the root cause of problems and making the necessary changes.

    Debugging techniques:

    1.

    Print Statements: add print statements to check the values of variables at different stages of the code.

    2.

    Debugging Tools: use tools like pdb (Python Debugger) to step through the code, inspect variables and understand the flow of execution.

    3.

    Error Messages: read and interpret error messages to locate the source of the problem.

    Together, testing finds problems and debugging fixes them, making programs reliable.

  5. Question 5 · 5 marks

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

    Show model answer

    Python offers an extensive standard library with built-in modules and data structures. Libraries are like toolboxes or pre-built toolkits, so you can solve problems without writing all the code yourself. They save time and effort and let you focus on the specific problem rather than reinventing the wheel. Only import what you need.

    Example 1: random library

    import random

    number = random.randint(1, 10)

    print("The random number is:", number)

    Example 2: statistics library

    import statistics

    data = [23, 45, 67, 89, 12, 44, 56]

    print("The mean value is:", statistics.mean(data))

    Output: The mean value is: 48

    The statistics library performs calculations such as mean, median and mode, which is useful in data analysis.

More tests in Control Structures in Python

Practise with a free account

Save your scores, see which chapters need work, and keep a notebook of every question you got wrong.

Create free account