PCAP Programming Essentials in Python Essentials 2 PE2 Part 2 Summary Test Exam Answers

  1. The following code:

    x = "\\\\"
    print(len(x))
    • will print 1
    • will print 2
    • will cause an error
    • will print 3
  2. What information can be read using the uname function provided by the os module? (Select two answers)

    import os
    os.mkdir('pictures')
    os.chdir('pictures')
    print(os.getcwd())
    • Last login date.
    • Current path
    • Operating system name
    • Hardware identifier
  3.  The following statement:

    assert var != 0
    • is erroneous
    • has no effect
    • will stop the program when var == 0
    • will stop the program when var !=0
  4. What is the except output of the following code?

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

    from datetime import timedelta
    delta = timedelta(weeks = 1, days = 7, hours = 11)
    print(delta * 2)
    • 28 days, 22:00:00
    • 2 weeks, 14 days, 22 hours
    • 7 days, 22:00:00
    • The code will raise an exception
  6. What is the excepted output of the following code?

    class A:
        def __init__(self, v=2)
        def set(self, v=1):
           self.v +=v
           return self.v
    a = A()
    b = a
    b.set()
    print(a.v)
    • 1
    • 0
    • 2
    • 3
  7. 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 except
    • The code will print 2
    • The code will print 0
    • The code will print 1
  8. Knowing that a function named fun() resides in a module named mod , and was imported using the following statement:

    from mod import fun

    choose the right to invoke the fun() function:

    • mod:fun()
    • mod::fun()
    • fun()
    • mod.fun()
  9. What output will appear after running the following snippet?

    • The number of all the entities residing in the math module
    • A string containing the fully qualified name of the module
    • An error message
    • A list of all the entities residing in the math module
  10. Look at the code below:

    import random
    #
    # Insert lines of code here.
    #
    print(a, b,

    Which lines of code would you insert so that it is possible for the program to output the following result:

    6 82 0
    • a = random.randint(0, 100)
      b = random.randrange(10, 100, 3)
      c = random.choice((0, 100, 3))
    • a = random.choice((0, 100, 3))
      b  = random.randrange(10, 100, 3)
      c = random.randint(0, 100)
    • a  = random.randrange(10, 100, 3)
      b = random.randint(0, 100)
      c  = random.choice((0, 100, 3))
    • a  = random.randint(0, 100)
      b = random.choice((0, 100, 3))
      c  = random.randrange(10, 100, 3)
  11. What is the expected result of the following code?

    from datetime import datetime
    datetime_1 = datetime(2019, 11, 27, 11, 27, 22)
    datetime_2 = datetime(2019, 11, 27, 0, 0, 0)
    print(datetime_1 - datetime_2)
    • o days
    • 0 days, 11:27:22
    • 11:27:22
    • 11 hours, 27 minutes, 22 seconds
  12. What is the expected result of the following code?

    import calendar
    calendar.setfirstweekday(calendar.SUNDAY)
    print(calendar.weekheader(3))
    • Su Mo Tu We Th We Fr Sa
    • Sun Mon Tue Wed Thu Fri Sat
    • Tu
    • Tue
  13. 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(B, A):
         def c(self):
             self.a()
    
    o = C()
    o.c()
    • The code will print c
    • The code will raise an exception
    • The code will print b
    • The code will print a
  14. Look at the following code:

    numbers  [0, 2, 7, 9, 10]
    # Insert line of code here.
    print(list(foo))

    Which line would you insert in order for the program to produce the expected output?

    [0, 4, 49, 81, 100]
    • foo  = lambda num: num ** 2, numbers
    • foo = lambda num: num * 2, numbers)
    • foo = filter(lambda num: num ** 2, numbers)
    • foo = map(lambda num : num ** 2, numbers)
  15. What is the expected result of executing the following code?

    class I:
        def __init__(self):
            self.s = 'abc'
            self.i = 0
            def __init__(self):
            return self
        def __next__(self):
            if self.i == len(self.s):
               raise StopIteration
             v = self.s[self.i]
             self.i +=1
             return v
    for x in I():
        print(x, end='')
    • The code will print 210
    • The code will print abc
    • The code will print 012
    • The code will print cba
  16. The compiled Python bytecode is stored in files which have their names ending with:

    • py
    • pyb
    • pc
    • pyc
  17. Which pip command would you use to uninstall a previously install package?

    • pip delete packagename
    • pip –uninstall packagename
    • pip –remove packagename
    • pip uninstall packagename
  18. What is the excepted result of executing the following code?

    try:
        raise Exception(1, 2, 3)
    except Exception as e:
        print(len(e.args))
    • The code will raise an unhandled exception
    • The code will print 2
    •  The code will print 3
    • The code will print 1
  19. What is the excepted result of the following snippet?

    try:
        raise Exception
    except BaseException:
        print("a")
    except Exception:
        print("b")
    except:
        print("c")
    • b
    • a
    • An error message
    • 1
  20. What is the expected result of executing the following code?

    class A:
          pass
    class B(A):
          pass
    class C(B):
          pass
    print(issubclass(A, C))
    • The code will print Ture
    • The code will print 1
    • The code will print False
    • The code will raise an exception
  21. If you want to fill a byte array with data read in from a stream, which method you can use?

    • The read() method
    • The readbytes() method
    • The readfrom() method
    • The readinto() method
  22. The following code:

    print(float("1.3"))
    • will print 1.3
    • will print 13
    • will print 1,3
    • will raise a ValueError exception
  23. The following code:

    print(chr(ord('p) + 2))

    will print:

    • q
    • s
    • r
    • t
  24. If there are more than one except: branch after the try: clause, we can say that:

    •  exactly one except: block will be executed
    • one or more except: blocks will be executed
    • not more than one except: block will be executed
    • none of the except: blocks will be executed
  25. If the class constructor is declared in the following way:

    class Class:
         def __init__(self, vla = 0):
              pass

    which one of the assignments is invalid?

    • object = Class(1)
    • object = Class(None)
    • object = Class(1, 2)
    • object = Class()
  26. What is the expected result of the following snippet?

    try:
       raise Exception
    except:
       print("c")
    except BaseException:
       print("a")
    except Exception:
       print("b")
    • The code will cause a syntax error 
    • 1
    • b
    • a
  27. What is the expected result of the following code?

    def my_fun(n):
        s = '+'
        for i in range(n):
            s += s
            yield s
    for x in my_fun(2):
        print(x, end='')
    • The code will print +
    • The code will print +++
    • The code will print ++
    • The code will print ++++++
  28. Look at the following code:

    numbers = [i*i for i in range(5)]
    # Insert line of code here.
    print(foo)

    Which line would you insert in order for the program to produce the expected output?

    [1, 9]
    • foo = list(filter(lambda x: x % 2, numbers))
    • foo = list(filter(lambda x: x / 2, numbers))
    • foo = list(map(lambda x: x % 2, numbers))
    • foo = list(map(lambda x: x // 2, numbers))
  29. What is the expected result of executing the following code?

    def o(p):
        def q():
            return '*' * p
        return q
    r = o(1)
    s = o(2)
    print(r() + s())
    • The code will print ***
    • The code will print ****
    • The code will print *
    • The code will print **
  30. The following statement:

    from a.b import c

    causes the import of:

    • entity a from module b from package c
    • entity b from module a from package c
    • entity c from module a from package b
    • entity c from module b from package a
  31. The sys.stderr stream is normally associated with:

    • the keyboard
    • the printer
    • a null device
    • the screen
  32. What will be the output of the following code, located in the p.py file?

    print(__name__)
    • main
    • p.py
    • __main__
    • __p.py__
  33. Assuming that the​ open() invocation has gone successfully, the following snippet:

    for x in open('file', 'rt')) 
        print(x)

    will:

    • read the file character by character
    • read the file line by line
    • read the whole file at once
    • cause an exception
  34. If a is a stream opened in read mod, the following line:

    q = s.read(1)

    will read:

    • one line from the stream
    • one kilobyte from the stream
    • one buffer from the stream
    • one character from the stream 
  35. The following line of code:

    for line in open('text.txt', 'rt'):
    • in invalid because open returns nothing
    • is invalid because open returns a non-iterable object
    • is invalid because open returns an iterable object
    • may be valid if line is a list
  36. What is the expected result of the following code?

    import os
    os.mkdir('pictures')
    os.chdir('pictures')
    print(os.getcwd())
    • The code will print the owner of the created directory
    • The code will print the content of the created directory
    • The code will print the name of the created directory
    • The code will print the path to the created directory
  37. Assuming that the following three files a.py , and c.py reside in the same directory, what will be the output produced after running the c.py file?

    # file a.py
    print("a", end='')
    # file b.py
    import a
    print("b", end='')
    # file c.py
    print("c", end='')
    import a
    import b
    • cab
    • cab
    • abc
    • bac
  38. What is the expected result of executing the following code?

    class A:
         def __init__(self):
             pass
    a = A(1)
    print(hasattr(a, 'A'))
    • The code will print 1
    • The code will raise an exception
    • The code will print False
    • The code will print True
  39. The following code:

    x = " \\"
    print(len(x))
    • will print 1
    • will print 3
    • will print 2
    • will cause an error
  40. Which of the following commands would you use to check pip ‘s version? (Select two answers)

    • pip version
    • pip --version
    • pip–version
    • pip3 --version
5 3 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments