1AKIFT POS Test (Group A)

2021-11-04

Max. 25 points

Name:

Task Max. Achieved
1 2
2 6
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 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.
  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 output, write "-".
    1. x = 5
      y = 2 * x**2 + 2 * x - 7
      print(y)
      53
    2. n = 10
      d = 6
      work = True
      while work:
          if n / d == 0:
              print (d)
              work = False
          d -= 1
      ERROR
    3. s = "Everyone wants to become a great programmer."
      print (s[3], s[-2])
      r r
  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. Finish the program below. The given search term needle should be searched in all of the files in the list of relative filenames. The program should count the non-overlapping occurrences of needle in all the given files and print it to standard output. The comparison must not be case sensitive. Use the string class' S.count(sub[, start[, end]]) -> int method which returns the number of non-overlapping occurrences of substring sub in string S[start:end].
    #!/usr/bin/env python
    filenames = input('Files: ')
    needle = input('Needle: ').lower()
    matches = 0
    filenames = filenames.split(',')  # create list of filenames
    for filename in filenames:
        with open(filename) as f:
            content = f.read().lower()
            matches += content.count(needle)
    print(matches)