Test 6: Long Questions

Long questions

9 questions · 50 min · 61 marks

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

Sample questions with answers

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

  1. Question 1 · 8 marks

    What are the basic data types in Python? Explain integers, floats, strings, and Booleans with examples.

    Show model answer

    A variable in Python can store different kinds of data, and the common types are:

    1)Integer (int): stores whole numbers.

    Example: age = 17.

    2)Floating-point (float): stores decimal numbers.

    Example: price = 19.99.

    3)String (str): stores text enclosed in quotation marks.

    Example: name = 'Ali'.

    4)Boolean (bool): stores True or False.

    Example: is_student = True. A variable's value can change during execution (e.g. age = 71 then age = 60).

    Variable naming rules: begin with a letter or underscore, can then contain letters, digits or underscores, are case-sensitive (age and Age differ), and cannot be reserved keywords such as for, while, if.

  2. Question 2 · 8 marks

    What are arithmetic operators in Python. Provide examples of each operator and explain when you would use them.

    Show model answer

    Arithmetic operators perform basic mathematical operations. With a = 10 and b = 3: Addition (+): a + b = 13, used to add values (e.g. totals).

    Subtraction (-): a - b = 7, to find differences. Multiplication (*): a * b = 30, to find products. Division (/): a / b = 3.3333333333333335, returns a decimal result. Floor division (//): a // b = 3, gives the whole-number part of the division. Modulus (%): a % b = 1, gives the remainder (e.g. to check divisibility). Exponentiation (): a b = 1000, raises a number to a power.

    Example code: a = 10; b = 3; print(a, '+', b, '=', a + b).

    Precedence: parentheses first, then **, then *, / and %, then + and -.

  3. Question 3 · 8 marks

    Explain the input() and print() functions with examples.

    Show model answer

    Input and output operations allow you to interact with the user. input(): gets user input; it displays a message on the screen, waits for the user to type something and press Enter, and the entered text is stored in a variable as a string.

    Example: name = input('Enter your name: '). To handle numeric input, convert with int() or float(): user_age = int(input('Enter your age: ')); user_height = float(input('Enter your height in meters: ')). print(): displays information on the screen; it takes one or more arguments and displays them.

    Example: print('Hello, ' + name + '!') gives Hello, Maryam Ashraf! if the user entered Maryam Ashraf; print('Your age is:', user_age) displays the age. Together, input() collects data from the user and print() shows results.

  4. Question 4 · 8 marks

    Explain how comparison and logical operators work together in conditional statements. Provide a practical example.

    Show model answer

    Comparison operators (>, <, ==, !=, >=, <=) compare two values and return True or False. Logical operators (and, or, not) combine multiple conditions: and gives True only if both are True, or gives True if at least one is True, and not reverses a Boolean value. In a conditional statement (if), the condition is built using comparison operators and combined with logical operators; the code block runs only when the whole condition is True.

    Practical example: age = 17; marks = 85; if age >= 16 and marks > 80: print('Eligible for the scholarship') else: print('Not eligible'). Here age >= 16 is True and marks > 80 is True, so True and True gives True and the first message is printed. If the condition were age >= 16 or marks > 90, only one True condition would be needed.

    Note: if and else are standard Python conditional statements not covered in this chapter.

  5. Question 5 · 5 marks

    Write a program in Python that will take a name from user and display greeting message to the given name.

    Show model answer

    Program: name = input('Enter your name: '); print('Hello, ' + name + '!').

    Explanation: input() displays the message, waits for the user to type the name and press Enter, and stores it in the variable name. print() then joins the greeting text, the name and an exclamation mark using +.

    Sample output: Enter your name: Maryam Ashraf then Hello, Maryam Ashraf!. (Write each statement on its own line in the exam.)

More tests in Introduction to Python Programming

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