PE1 & PE2 : Python Essentials — Final Test

  1. What is the expected output of the following snippet?

    a = True
    b = False
    a = a or b
    b = a and b
    a = a or b
    print(a, b)
    • False False
    • True False
    • False True
    • True False
  2. What is the expected output of the following snippet?

    print(len([i for i in range(0, -2)]))
    • 3
    • 1
    • 2
    • 0
  3. How many stars * will the following snippet send to the console?

    i = 4
    while i > 0 :
          i -= 2
          print("*")
          if i == 2:
              break
    else:
          print("*")
    • two
    • one
    • zero
    • The snippet will enter an infinite loop, constantly printing one * per line
  4. What is the sys.stdout stream normally associated with?

    • The printer
    • The keyboard
    • null device
    • The screen
  5. What is the excepted output of the following snippet?

    class X: 
         pass
    class Y(X):
         pass
    class Z(Y):
         pass
    x = X()
    z = Z()
    print(isinstance(x, z), isinstance(z, X))
    • True True
    • True False
    • False True
    • False False
  6. What is the excepted output of the following snippet?

    class A:
        def __init__(self,name):
             self.name = name
    a = A("class")
    print(a)
    • class
    • name
    • A number
    • A string ending with a long hexadecimal number
  7. What is the excepted result of executing the following code?

    class A:
          pass
    class B:
          pass
    class C(A, B):
          pass
    print(issubclass(C, A) and issubclass(C, B))
    • The code will print True
    • The code will raise an exception
    • The code will print an empty line
    • The code will print False
  8. What is the excepted output of the following code?

    from datetime import datetime
    
    datetime = datatime(2019, 11, 27, 11, 27, 22)]
    print(datetime.strftime('%Y/%m/%d %H:%M:%S'))
    • 19/11/27 11:27:22
    • 2019/Nov/27  11:27:22
    • 2019/11/27  11:27:22       
    • 2019/November/27 11:27:22
  9. What is the excepted output of the following code?

    my_string_1 = 'Bond'
    my_string_2 = 'James Bond'
    
    print(my_string_1.isalpha(), my_string_2.isalpha())
    • False True
    • True True
    • False False
    • True False
  10. The meaning of a Keyword argument is determined by its:

    • both name and value assigned to it
    • position within the argument list
    • connection with existing variables
    • value only
  11. Knowing that the function named m, and the code contains the following import statement:

    from f import m

    Choose the right way to invoke the function:

    • mod.f()
    • The function cannot be invoked because the import statement is invalid
    • f()
    • mod:f()
  12. The Exception class contains a property named args -what is it?

    • A dictionary 
    • A list
    • A string 
    • A tuple
  13. What is PEP 8?

    • A document that provides coding conventions and style guide for the C code computing the C implementation of Python
    • A document that describes the development and release schedule for Python versions
    • A document that describes an extension to Python’s import mechanism which improves sharing of Python source code files
    • A document that provides coding conventions and style guide for Python code
  14. Which is the expected behavior of the following snippet?

    def fun(x):
        return 1 if x % 2 != else 2
    print(fun(fun(1)))
    • The program will output None
    • The code will cause a runtime error
    • The program will output 2
    • The program will output 1
  15. Which operator would you use to check whether two values are equal?

    • ===
    • is
    • ==
    • =
  16. What is the name of the directory/folder created by Python used to store pyc files?

    • __pycfiles
    • __pyc__
    • __pycache__
    • __cache__
  17. What can you do if you want tell your module users that a particular variable should not be accessed directly?

    • Start its name with  __or__
    • Start its name with a capital letter
    • Use its number instead of its name
    • Build its name with lowercase letters only
  18. What is the expected output of the following snippet?

    d = ('one': 1, 'three': 3, 'two':2)
    for k in sorted(d.values()):
    print(k, end=' ')
    • 1 2 3
    • 3 1 2
    • 3 2 1
    • 2 3 1
  19. Select the true statements.(Select two answers)

    • The first parameter of a class method dose not have to be named self
    • The first parameter of a class method must be named self
    • If a class contains the __init__ method, it can return a value
    • If a class contains the __init__ method, it cannot return any value
  20. Select the true statements. (Select two answers)

    • PyPI is short for Python Package Index
    • PyPI is the only existing Python repository
    • PyPI is one of many existing Python repository 
    • PyPI is short for Python Package Installer
  21. What is the expected output of the following snippet?

    t = (1, 2, 3, 4)
    t = t[-2:-1]
    t = t[-1]
    print(t)
    • (3)
    • (3,)
    • 3
    • 33
  22. What is the expected effect of running the following code?

    class A:
         def __init__(self, v):
             self._a = v + 1
    a = A(0)
    print(a._a)
    • The code will raise an AttributeError exception
    • The code will output 1
    • The code will output 2
    • The code will output 0
  23. Which of the following functions provide by the os module are available in both Windows and Unix? (Select two answers)

    • chdir()
    • getgid()
    • getgroups()
    • mkdir()
  24. What is the expected output of the following piece of code?

    v = 1 + 1 // 2 + 1 / 2 + 2
    • 4.0
    • 3
    • 3.5
    • 4
  25. If s is a stream opened in read mode, the following line:

    q = s.readlines()

    will assign q as :

    • dictionary 
    • tuple
    • list
    • string
  26. Which of the following sentences is true about the snippet below?

    str_1 = 'string'
    str_2 = str_1[:]
    • str_1 is longer than str_2
    • str_1 and  str_2  are different (but equal) strings
    • str_1 and str_2 are different names of the same string
    • str_2 is longer than str_1
  27. What is the expected result of executed the following code?

    class A:
         def __init__(self):
             pass
         def f(self):
             return 1
         def g():
             return self.f()
    a = A()
    print(a.g())
    • The code will output True
    • The code will output 0
    • The code will output 1
    • The code will raise an exception 
  28. What is the expected output of the following code, located in the file module.py ?

    print(__name__)
    • main
    • modle.py
    • __main__
    • __module.py__
  29. What is the excepted output of the following code?

    def a(x):
         def b():
            return x + x
         return b
    x = a('x)
    y = a('')
    print(x() + y())
    • xx
    • xxxx
    • xxxxxx
    • x
  30. What is the excepted behavior of the following piece of code?

    x = 16
    while x > 0:
         print('*', end='')
         x //= 2
    • The code will output *****
    • The code will output ***
    • The code will output *
    • The code will error an infinite loop
  31. A package directory/folder may contain a file intended to initialize the package. What is its name?

    • init.py
    • __init.py__
    • __init__.
    • __init__.py
  32. If there is a finally: branch inside the try: block, we can say that:

    • the finally: branch will always be executed
    • the finally: branch won’t be executed if any of the except: branch is executed
    • the finally: branch won’t be executed if no exception is raised
    • the finally: branch will be executed when there is no else: branch
  33. What value will be assigned to the x variable?

    z = 2
    y = 1
    x = y < z or z > y and y > z or z < y
    • True
    • False
    • 0
    • 1
  34. If you want to write a byte array’s content to a stream, which method can you use?

    • writeto()
    • writefrom()
    • write()
    • writebytearray()
  35. What is the expected behavior of the following snippet?

    try:
       raise Exception
    except:
       print("c")
    except BaseException:
       print("a")
    except Exception:
       print("b")
    • The code will cause an error
    • The code will output b
    • The code will output c
    • The code will output a
  36. What is true about the following code snippet?

    def fun(par2, par1):
        return par2 + par1
    print(fun(par2 = 1, 2))
    • The code is erroneous
    • The code will output 3
    • The code will output 2
    • The code will output 1
  37. What is the expected output of the following piece of code?

    x, y, z = 3, 2, 1
    z, y, x = x, y, z
    print(x, y, z)
    • 2 1 3
    • 1 2 2
    • 3 2 1
    • 1 2 3
  38. What is the expected output of the following snippet?

    d = {}
    d['2'] = [1, 2]
    d['1'] = [3, 4]
    for x in d.keys():
        print(d[x][1], end="")
    • 24
    • 31
    • 13
    • 42
  39. What is the expected output of the following snippet?

    try:
       raise Exception
    except BaseException:
       print("a", end='')
    else:
       print("b", end='')
    finnaly:
       print("c")
    • bc
    • a
    • ab
    • ac
  40. If the class constructor is declare as below:

    class Class:
         def __init__(self):
             pass

    which one of the assignment is valid?

    • object = Class()
    • object = Class(1,2)
    • object = Class(None)
    • object = Class(1)
  41. What is the expected output of the following code?

    from datetime import timedelta
    delta = timedelta(weeks = 1, days = 7, hours = 11)
    print(delta)
    • 7 days, 11:00:00
    • 2 weeks, 11:00:00
    • 1 week, 7 days, 11 hours
    • 14 days, 11:00:00
  42. What is the expected output of the following piece of code if the user enters two lines containing 1 and 2 respectively?

    y = input()
    x = input()
    print(x + y)
    • 2
    • 21
    • 12
    • 3
  43. What is the expected behavior of the following snippet?

    my_string = 'abcdef'
    def fun(s):
       del s[2]
       return s
    print(fun(my_string))
    • The program will cause an error 
    • The program will output abdef
    • The program will output acdef
    • The program will output abcef
  44. What is true about the following snippet?

    def fun(d, k, v):
        d[k] = v
    my_dictionary = {}
    print(fun(my_dictionary, '1', 'v'))
    • The code is erroneous 
    • The code will output None
    • The code will output v
    • The code will output 1
  45. What is the expected output of the following code?

    class A:
         A = 1
         def __init__(self):
           self.a = 0
    print(hasattr(A, 'A'))
    • False
    • 1
    • 0
    • True
  46. What is the expected behavior of the following code?

    x = """
    """
    print(len(x))
    • The code will output 2
    • The code will output 3
    • The code will cause an error
    • The code will output 1
  47. What is the expected output of the following code?

    import calendar
    c = calendar.Calendar(calendar.SUNDAY)
    for weekday in c.iterweekdays():
         print(weekday, end=" ")
    • 7 1 2 3 4 5 6
    • 6 0 1 2 3 4 5
    • Su Mo Tu We Th Fr Sa
    • Su
  48. What is the expected output of the following code?

    def fun(n):
        s = ' '
        for i in range(n):
             s += '*'
             yield s
    for x in fun(3):
         print(x, end='')
    • ****
    • ******
    • 2***
    • *
  49. What is the expected output of the following code?

    t = (1, )
    t = t[0] + t[0]
    print(t)
    • 2
    • (1, )
    • (1, 1)
    • 1
  50. What is the expected result of executing the following code?

    class A:
        def a(self):
            print('a')
    class B:
        def a(self):
            print('b')
    class C(A, B):
        def c(self):
            self.a()
    o = C()
    o.c()
    • The code will print c
    • The code will print  a
    • The code will raise an exception
    • The code will print b
  51. What is the expected behavior of the following code snippet?

    my_list = [1, 2, 3, 4]
    my_list = list(map(lambda x: 2*x, my_list))
    print(my_list)
    • The code will cause a runtime error
    • The code will output 1 2 3 4
    • the code will output 10
    • The code will output 2 4 6 8
  52. What is the expected behavior of the following piece of code?

    d = {1: 0, 2: 1, 3: 2, 0: 1}
    x = 0
    for y in range(len(d)):
        x = d[x]
    print(x)
    • The code will output  0
    • The code will output 1
    • The code will cause a runtime error
    • The code will output 2
  53. What pip operation would you use to check what Python package have been installed so far?

    • show
    • list
    • help
    • dir
  54. What is true about the following line of code?

    print(len((1, )))
    • The code will output 0
    • The code is erroneous 
    • The code will output 1
    • The code will output 2
  55. Which line properly invokes the function defined as below?

    def fun(a, b, c=0):
        # function body
    • fun(b=0, b=0)
    • fun(a=1, b=0, c=0)
    • fun(1, c=2)
    • fun(0)
  56. What is the expected behavior of the following code?

    import os
    os.makedirs('pictures/thumbnails')
    os.rmdir('pictures')
    • The code will delete both the pictures and thumbnails directories 
    • The code will delete the pictures directory only
    • The code will raise an error
    • the code will delete the thumbnails directory only
  57. What is true about the following piece of code?

    print("a", "b", "c", sep=" ' ")
    • The code is erroneous 
    • The code will output abc
    • The code will output a'b'c
    • The code will output a b c
  58. What is the expected behavior of the following code?

    x = "\"
    print(len(x))
    • The code will output 3
    • The code will cause an error
    • The code will output a2
    • The code will output 1
  59. What is the expected output of the following code?

    class A:
         A = 1
         def __init__(self, v=2):
             self.v = v +A.A
             A.A += 1
         def set(self, v):
             self.v +=v
             A.A += 1
             return
    a = A()
    a.set(2)
    print(a.v)
    • 7
    • 1
    • 3
    • 5
  60. How many empty lines will the following snippet send to the console?

    my_list = [[c for c in range(r)] for r in range(3)]
    for element in my_list:
        if len(element) < 2:
           print()
    • two
    • three
    • zero
    • one
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments