PCAP- Programming Essentials in Python Part 2 Summary Test Answer!

  1. Knowing that a function named f() resides in a module named m, and was imported using the following statement


  from mod import fun

choose the right way to invoke it:

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

      import math
      print(dir(math))
    • an error message
    • a string containing the fully qualified name of the module
    • a list of all the entities residing in the math module
    • the number of all the entities residing in the math module
  1. The compiled Python bytecode is stored in files having names ending with:
    • py
    • pyb
    • pc
    • pyc
  1. Assuming that all three files, a.py, b.py, and c.py reside in the same folder, what will be the output produced by 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
    • cba
    • abc
    • bac
    • cab
  1. What will be the output of the following code, located in file p.py?

      print(__name__)
    • p.py
    • main
    • __p.py__
    • __main__
  1. 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 b from package a
    • entity c from module a from package b
  1. If there are more than one except: branches after the try:, we can say that:
    • one or more of the try: blocks will be executed
    • none of the try: blocks will be executed
    • not more than one try: block will be executed
    • exactly one of the try: blocks will be executed
  1. What will be the output of the following snippet?  
    try:     
      raise Exception
      except BaseException:
          print(“a”)
      except Exception:
          print(“b”)
      except:
          print(“c”)
    • c
    • b
    • it will cause an error
    • a
  1. The following line of code:

        for line in open(‘text.txt’,’rt’):
    • is valid as open returns an iterable object
    • is invalid as open returns a non-iterable object
    • is invalid as open returns nothing
    • may be valid if line is a list
  1. What will be the output of the following snippet?
      try:
          raise Exception
      except:
          print(“c”)
      except BaseException:
          print(“a”)
      except Exception:
          print(“b”)
    • a
    • c
    • b
    • it will cause an error
  1. The following statement:

    assert var != 0
    • will stop the program when var == 0
    • is erroneous
    • has no effect
    • will stop the program when var != 0
  1. The following code prints:

      x = “\\\\”
      print(len(x))
    • 2
    • 1
    • 3
    • the code will cause an error
  1. The following code prints:

      x = “\\\”
      print(len(x))
    • 3
    • the code will cause an error
    • 1
    • 2
  1. The following code prints:
      print(chr(ord(‘p’) + 2))
    • s
    • t
    • q
    • r
  1. The following code:
        print(float(“1.3”))
    • raises a ValueError exception
    • prints 13
    • prints 1,3
    • 1.3
  1. If the class’s constructor is declared as below, which one of the assignments is invalid?
        class Class:
          def __init__(self,val=0):
              pass
    • object = Class(None)
    • object = Class(1)
    • object = Class()
    • object = Class(1,2)
  1. What will be output of the following code?
      class A:
          def __init__(self,v = 2):
            self.v = v
          def set(self,v = 1):
            self.v += v
              return self.v
      a = A()
      b = a
      b.set()
      print(a.v)
    • 0
    • 3
    • 1
    • 2
  1. What will be output of the following code?
        class A:
            A = 1
            def __init__(self):
              self.a = 0
        print(hasattr(A,’a’))
    • 1
    • False
    • 0
    • True
  1. What will be the result of executing the following code?
      class A:
          pass
      class B(A):
          pass
      class C(B):
          pass
      print(issubclass(A,C))
    • it will print False
    • it will print True
    • it will print 1
    • it will raise an exception
  1. The sys.stderr stream is normally associated with:
    • the keyboard
    • a null device
    • the screen
    • the printer
  1. What will be the effect of running the following code?
      class A:
          def __init__(self,v):
              self.__a = v + 1
      a = A(0)
      print(a.__a)
    • it will print 1
    • it will print 2
    • it will raise an AttributeError exception
    • it will print 0
  1. What will be the result of executing the following code?
      class A:
          def __init__(self):
            pass
      a = A(1)
      print(hasattr(a,’A’))
    • it will print True
    • it will raise an exception
    • it will print False
    • it will print 1
  1. What will be the 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()
    • it will print c
    • it will raise an exception
    • it will print b
    • it will print a
  1. What will be the result of executing the following code?
      try:
          raise Exception(1,2,3)
      except Exception as e:
          print(len(e.args))
    • it will print 2
    • it will print 1
    • it will raise an unhandled exception
    • it will print 3
  1. What will be the result of executing the following code?
      def I(n):
          s = ‘+’
          for i in range(n):
            s += s
            yield s
      for x in I(2):
          print(x,end=”
    )
    • it will print ++
    • it will print ++++++
    • it will print +
    • it will print +++
  1. What will be the result of executing the following code?
      class I:
          def __init__(self):
            self.s = ‘abc’
            self.i = 0
          def __iter__(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=”)
    • 012
    • abc
    • cba
    • 210
  1. What will be the result of executing the following code?
      def o(p):
          def q():
            return ‘*’ * p
          return q
      r = o(1)
      s = o(2)
      print(r() + s())

     
    • it will print ****
    • it will print **
    • it will print ***
    • it will print *
  1. If s is a stream opened in read mode, the following line will:
      q = s.read(1)
    • read 1 character from the stream
    • read 1 kilobyte from the stream
    • read 1 line from the stream
    • read 1 buffer from the stream
  1. Assuming that the open() invocation has gone successfully, the following snippet will:
      for x in open(‘file’,’rt’):
          print(x)
    • read the file character by character
    • cause an exception
    • read the whole file at once
    • read the file line by line
  1. If you want to fill a byte array with data read in from a stream, you’d use:
    • the readinto() method
    • the read() method
    • the readbytes() method
    • the readfrom() method

PCAP- Programming Essentials in Python Part 1 Summary Test Answer!: Here

Manoj Kumar

I Love Technology and like to Explore new Technologies

Leave a Reply