Skip to content
VJ Classes

One place to learn science, commerce, arts, computers and life skills

10 questions. Think of your answer first, then open “Show answer”.

  1. Which symbol starts a comment in Python?

    1. //
    2. #
    3. /*
    4. --
    Show answer

    Answer: B · #

    Python ignores everything from # to the end of the line; // and /* start comments in languages such as C and Java.

  2. Which data type does Python give to the value 3.0?

    1. float
    2. int
    3. str
    4. double
    Show answer

    Answer: A · float

    Any number written with a decimal point is a float, even if the decimal part is zero; Python has no separate double type.

  3. Which keyword defines a function in Python?

    1. function
    2. define
    3. fun
    4. def
    Show answer

    Answer: D · def

    A function definition starts with def, followed by the name, the parameters in brackets and a colon.

  4. What does print(f"{7 / 2:.2f}") display?

    1. 3
    2. 3.5
    3. 3.50
    4. 3.500
    Show answer

    Answer: C · 3.50

    7 / 2 is 3.5, and the format .2f always shows exactly two digits after the decimal point.

  5. After total = 0 and then for n in range(1, 5): total += n, what is the value of total?

    1. 15
    2. 10
    3. 4
    4. 5
    Show answer

    Answer: B · 10

    range(1, 5) gives 1, 2, 3 and 4, and 1 + 2 + 3 + 4 = 10; the loop stops before 5.

  6. Which statement about tuples is true?

    1. Tuples can grow with append()
    2. Tuples cannot contain repeated values
    3. Tuples are written in square brackets
    4. The items of a tuple cannot be changed after it is created
    Show answer

    Answer: D · The items of a tuple cannot be changed after it is created

    Tuples are immutable and written in round brackets, and they may contain duplicates; lists are the changeable version.

  7. What does len(set([3, 1, 3, 2, 1])) return?

    1. 5
    2. 2
    3. 3
    4. 1
    Show answer

    Answer: C · 3

    A set keeps only unique values, so the set is {1, 2, 3}, which has 3 items.

  8. In a method of a class, what does self refer to?

    1. The particular object on which the method was called
    2. The class itself, shared by all objects
    3. The file in which the class is saved
    4. The value that the method returns
    Show answer

    Answer: A · The particular object on which the method was called

    When you call s1.introduce(), Python passes s1 as self, so self gives the method access to that object's own attributes.

  9. count = 0, then while count < 5: print(count), with nothing else inside the loop. What happens when this runs?

    1. It prints 0 to 4 and stops
    2. It prints nothing
    3. It prints 0 again and again without stopping
    4. It raises an error at once
    Show answer

    Answer: C · It prints 0 again and again without stopping

    count never changes inside the loop, so count < 5 stays True forever; adding count += 1 inside the loop fixes it.

  10. If d = {'a': 1}, which expression gives 0 instead of raising an error for the missing key 'b'?

    1. d['b']
    2. d.get('b', 0)
    3. d.find('b')
    4. d['b'] or 0
    Show answer

    Answer: B · d.get('b', 0)

    d.get('b', 0) returns the default 0 when the key is missing, while d['b'] raises a KeyError and dictionaries have no find() method.

Want to test yourself? Take the same questions as a timed practice test and see your score. Start the practice test