1AKIFT POS Test (Group B)

2024-12-19

Max. 100 points

Name:

Task Max. Achieved
1 20
2 30
3 25
4 25
Sum 100
Grading: >= 88: 1, >=76: 2, >=64 : 3, >50: 4, <=50: 5
  1. Answer the following statements indicating whether they are True or False.
    0-4 correct: 0 points, 5 correct: 5 points, 6 correct: 10 points, 7 correct: 15 points, 8 correct: 20 points.
    Statement True False
    Python sets are mutable.
    sys.exit(.) reports to the operating system whether execution was successful or not.
    The "ifmain" pattern improves program performance when executed.
    The ipython shell allows interactive execution of Python code.
    In Python, int is mutable.
    for loops are used for repeated execution as long as an expression is true.
    Python modules are essentially files with Python code.
    PYTHONPATH is used to tune performance of Python programs.
  2. 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 visible output, write "-".
    5 points for each correct answer.
    1. t = ('a', 'b', 'c', )
      print(t.append('d'))
      ERROR
    2. def sum(a, b, c):
          return a + b
      sum(4, 2, 3)
      -
    3. def area(length, width):
          return length + width
      print(area(5, 2))
      7
    4. l = [4, 3, 2]
      print(l.sort())
      None
    5. s = 'Hello World!'
      s = s.lower()
      print(s)
      hello world!
    6. print({1, 1, 1})
      {1}
  3. Implement the function is_leap(year: int) -> bool. Given a year as a number, report if it is a leap year. A leap year occurs: For example, is_leap(1981) returns False, is_leap(1984) returns True, is_leap(1900) returns False while is_leap(2000) returns True. Document your function with a docstring. (25 points)
    5 points for the correct function signature
    5 points for returning `True` for years evenly divisible by 400
    5 points for returning `False` for years evenly divisible by 100
    5 points for returning `True` for years evenly divisible by 4
    5 points for returning `False` for all other years
    def is_leap(year):
        """Return True if the given year is a leap year, otherwise false."""
        if year % 400 == 0:
            return True
        if year % 100 == 0:
            return False
        if year % 4 == 0:
            return True
        return False

  4. 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 and a doctest 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. (25 points)
    5 points for the function signature
    5 points for the docstring
    10 points for the correct calculation of the sumproduct
    5 points for returning the sum
    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