1AKIFT POS Test (Group B)

2021-11-04

Max. 25 points

Name:

Task Max. Achieved
1 6
2 2
3 2
4 5
5 10
Sum 25
Grading: 23-25: 1, 20-22: 2, 17-19: 3, 13-16: 4, <13: 5
  1. 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. x = 5
      y = 2 * x**2 + 2 * x - 6
      print(y)
      54
    2. n = 10
      d = 6
      work = True
      while work:
          if n / d == 0:
              print (d)
              work = False
          d -= 1
      ERROR
    3. s = "Programming is great fun"
      print (s[3], s[-2])
      g u
  2. What is the difference between a comment and a docstring?
    Comments are ignored by the interpreter and programming tools. Docstrings are used to generate documentation, make information available in development tools and can be accessed programmatically.
  3. What is scope in computer programming?
    Scope determines where a name can be accessed (eg. function scope ).
  4. Rewrite the following for loop as while loop.
    for i in range(0, 10, 3):
        print(f'{i} * {i} = {i * i}')
    i = 0
    while i < 10:
        print(f'{i} * {i} = {i * i}')
        i += 3
  5. Create a program that reads the integer `n` from standard input and prints the sum of all even integers between 1 and `n` (inclusive) to standard output. For example, if the number 7 is given, the value 12 should be printed. If the number 8 is given, the value 20 should be printed.
    n = int(input('Enter an integer: '))
    total = 0
    for i in range(1, n // 2 + 1):
        total += i * 2