1AKIFT POS Test (Group Extra)

2021-12-09

Max. 22 points

Name:

Task Max. Achieved
1 2
2 2
3 8
4 5
5 5
Sum 22
Grading: <11: 5, 11-13: 4, 14-16: 3, 17-19: 2, 20-22: 1
  1. Answer the following statements indicating whether they are True or False.
    0-2 correct: 0 points, 3 correct: 1 point, 4 correct: 2 points.
    Statement True False
    In Python, functions, modules, classes and loops create scope.
    Programs written in C tend to run faster than programs written in Python.
    Any for loop can be re-written as while loop.
    C is still one of the most commonly used programming languages.
  2. Each of the following multiple choice questions will be awarded with 1 point if all correct statements are checked and all incorrect statements are not checked.
    1. Mark all code snippets that are valid Python 3.8
    2. Mark all correct statements
  3. What is the output of the following code snippets? Write exactly what the output of each snippet is if the snippet is the sole content of a Python file. If the output is an error message, it is enough to write “ERROR”. If there is no output, write "-".
    1. if __name__ == "__main__":
          import sys
          print(sys.exit(0))
      -
    2. def decode(text):
          return encode(text)
      print(decode('apt-build moo'))
      ERROR
    3. def grmpfzotz():
          print('Hooray')
      -
    4. s = "Everyone wants to become a great programmer."
      print(s[:3], s[-1])
      Eve .
  4. Write a function sum_to(num) that takes an integer num >= 0 as sole argument. The function should return the sum of all positive integers <= num. In the function, assert that the input is ≥ 0 by using assert.
    Grading: 1 point for correct function signature, 1 point for proper docstring, 1 for correct assertion, 2 for returning the correct result.
    def sum_to(num):
        """
        Sum all numbers from 0 up to a given integer.
    
        Assert that no negative numbers are entered.
        """
        assert num >= 0, "argument must be >= 0"
        return int(num / 2.0 * (num + 1))
  5. Write a function sumproduct(first iterable, second iterable) in Python that takes two sequences of numbers with equal length and returns the sum of the product of the numbers.
    [e.g. sumproduct((1, 3, 4), (9, 2, 3)) ⇒ 1*9 + 3*2 + 4*3 = 27; sumproduct([3, 5], [2, 4]) ⇒ 26]. Add a proper docstring to receive full points.
    Grading: 1 point for correct function signature, 1 point for proper docstring, 1 point for a doctest, 2 for returning the correct result.
    def sumproduct(first, second):
        """
        Return the sum of the products of `zip(first, second)`.
    
        >>> sumproduct((1, 3, 4), (9, 2, 3))
        27
        """
        sum = 0
        for i, j in zip(first, second):
            sum += i * j
        return sum