0% found this document useful (0 votes)
98 views

Knowing That A Function Named F

The document contains 30 multiple choice questions about Python concepts like modules, functions, exceptions, classes, files and streams. The questions cover topics such as importing modules and functions, using the dir() function, bytecode file extensions, exception handling, classes and inheritance, file I/O, iterators, generators and reading from streams.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views

Knowing That A Function Named F

The document contains 30 multiple choice questions about Python concepts like modules, functions, exceptions, classes, files and streams. The questions cover topics such as importing modules and functions, using the dir() function, bytecode file extensions, exception handling, classes and inheritance, file I/O, iterators, generators and reading from streams.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

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()
2. 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
3. The compiled Python bytecode is
stored in files having names ending
with:
 py
 pyb
 pc
 pyc
4. 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
5. What will be the output of the
following code, located in file p.py?
 
   print(__name__)
 p.py

 main

 __p.py__

 __main__

6. 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
7. 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
8. 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

9. 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

10. 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

11. 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

12. The following code prints:


 
   x = “\\\\”
   print(len(x))
 2

 1

 3

 the code will cause an error

13. The following code prints:


 
   x = “\\\”
   print(len(x))
 3
 the code will cause an error
 1
 2
14. The following code prints:
   print(chr(ord(‘p’) + 2))
 s

 t

 q

 r

15. The following code:


    print((“1.3”))
 raises a ValueError exception

 prints 13

 prints 1,3

 1.3

16. 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)

17. 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
18. 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
19. 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
20. The sys.stderr stream is normally
associated with:
 the keyboard
 a null device
 the screen
 the printer
21. 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
22. 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
23. 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
24. 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
25. 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 +++
26. 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
27. 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 *
28. 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

29. 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

30. 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

You might also like