Python Programming Trivia: MCQ Quiz!

40 Questions | Total Attempts: 1838

SettingsSettingsSettings
Please wait...
Python Programming Trivia: MCQ Quiz!

.


Questions and Answers
  • 1. 
    What does the following code do?   def a(b, c, d): pass
    • A. 

      Defines a list and initializes it

    • B. 

      Defines a function, which does nothing

    • C. 

      Defines a function, which passes its parameters through

    • D. 

      Defines an empty class

  • 2. 
    All keywords in Python are in:
    • A. 

      Lower case

    • B. 

      UPPER CASE

    • C. 

      Capitalized

    • D. 

      None

  • 3. 
     What gets printed? x = 4.5 y = 2 print x//y
    • A. 

      2.0

    • B. 

      2.25

    • C. 

      9.0

    • D. 

      20.25

    • E. 

      21

  • 4. 
    What is the output of the below program? a = [1,2,3,None,(),[],] print len(a)
    • A. 

      Syntax error

    • B. 

      4

    • C. 

      5

    • D. 

      6

    • E. 

      7

  • 5. 
    What gets printed? x = True y = False z = False if x or y and z: print "yes" else: print "no"
    • A. 

      Yes

    • B. 

      No

    • C. 

      Fails to compile

  • 6. 
    If PYTHONPATH is set in the environment, which directories are searched for modules? A) PYTHONPATH directory B) current directory C) home directory D) installation dependent default path
    • A. 

      A only

    • B. 

      A and D

    • C. 

      A, B, and C

    • D. 

      A, B, and D

    • E. 

      A, B, C, and D

  • 7. 
    In python 2.6 or earlier, the code will print error type 1 if access secure system raises an exception of either AccessError type or SecurityError type try:   accessSecureSystem() except AccessError, SecurityError:   print "error type 1" continueWork()
    • A. 

      True

    • B. 

      False

  • 8. 
    What gets printed? print r"\nwoow"
    • A. 

      New line then the string: woow

    • B. 

      The text exactly like this: r"\nwoow"

    • C. 

      The text like exactly like this: \nwoow

    • D. 

      The letter r and then newline then the text: woow

    • E. 

      The letter r then the text like this: nwoow

  • 9. 
    What gets printed? print "\x48\x49!"
    • A. 

      \x48\x49!

    • B. 

      4849

    • C. 

      48 49!

    • D. 

      4849!

    • E. 

      HI!

  • 10. 
    What gets printed? class parent: def __init__(self, param): self.v1 = param class child(parent): def __init__(self, param): self.v2 = param obj = child(11) print "%d %d" % (obj.v1, obj.v2)
    • A. 

      None None

    • B. 

      11 None

    • C. 

      Error is generated by program

    • D. 

      11 11

    • E. 

      None 11

  • 11. 
     What sequence of numbers is printed? values = [2, 3, 2, 4] def my_transformation(num): return num ** 2 for i in map(my_transformation, values): print i
    • A. 

      2 3 2 4

    • B. 

      4 9 4 16

    • C. 

      1 1 1 2

    • D. 

      1 1.5 1 2

    • E. 

      4 6 4 8

  • 12. 
    What does the code below do? sys.path.append('/root/mods')
    • A. 

      Changes the location that the python executable is run from

    • B. 

      Changes the location where sub-processes are searched for after they are launched

    • C. 

      Removes all directories for mods

    • D. 

      Adds a new directory to seach for python modules that are imported

    • E. 

      Changes the current working directory

  • 13. 
    Assuming the filename for the code below is /usr/lib/python/person.py and the program is run as:  python /usr/lib/python/person.py  What gets printed? class Person: def __init__(self): pass def getAge(self): print __name__ p = Person() p.getAge()
    • A. 

      __main__

    • B. 

      Usr.lib.python.person

    • C. 

      GetAge

    • D. 

      An exception is thrown

    • E. 

      Person

  • 14. 
    Which of the following data structures can be used with the "in" operator to check if an item is in the data structure?
    • A. 

      List

    • B. 

      Set

    • C. 

      Dictionary

    • D. 

      All of the above

    • E. 

      None of the above

  • 15. 
    What gets printed? class A: def __init__(self, a, b, c): self.x = a + b + c a = A(1,2,3) b = getattr(a, 'x') setattr(a, 'x', b+1) print a.x
    • A. 

      1

    • B. 

      2

    • C. 

      6

    • D. 

      7

    • E. 

      3

  • 16. 
    Which of the following statements is NOT true about Python?
    • A. 

      Python's syntax is much like PHP

    • B. 

      Python can be used for web development

    • C. 

      Python can run on any type of platform

    • D. 

      Python can be used to generate dynamic web pages

  • 17. 
    If you have a variable "example", how do you check to see what type of variable you are working with?
    • A. 

      GetType(example)

    • B. 

      Type(example)

    • C. 

      type(example)

    • D. 

      Example.type

  • 18. 
    If you had a statement like, "f = open("test.txt","w")", what would happen to the file as soon as that statement is executed?
    • A. 

      Nothing, unless the code following it writes to the file

    • B. 

      The file's contents will be erased

    • C. 

      Nothing

    • D. 

      Python will save the file's contents and append whatever the code following says to write.

  • 19. 
    Given a function that does not return any value, What value is thrown by it by default when executed in a shell.
    • A. 

      Int

    • B. 

      Bool

    • C. 

      Void

    • D. 

      None

  • 20. 
    Which of the following will run without errors?
    • A. 

      Round(45.8)

    • B. 

      Round(6352.894,2)

    • C. 

      Round()

    • D. 

      Round(7463.123,2,1)

  • 21. 
    Which of the following results in a SyntaxError?
    • A. 

      '"Once upon a time…", she said.'

    • B. 

      '3\'

    • C. 

      "’That's okay"'

    • D. 

      "He said, "Yes!""

  • 22. 
    Is the following code valid?   try:     # Do something except:     # Do something finally:     # Do something
    • A. 

      no, there is no such thing as finally

    • B. 

      no, finally cannot be used with except

    • C. 

      no, finally must come before except

    • D. 

      yes

  • 23. 
    How many except statements can a try-except block have?
    • A. 

      More than zero

    • B. 

      More than one

    • C. 

      One

    • D. 

      zero

  • 24. 
    Can one block of except statements handle multiple exception?
    • A. 

      Yes, like except TypeError, SyntaxError [,…]

    • B. 

      yes, like except [TypeError, SyntaxError

    • C. 

      no

    • D. 

      None of the mentioned

  • 25. 
    Which of the following will print True?a = foo(2) b = foo(3) print(a < b)
    • A. 

      Class foo: def __init__(self, x): self.x = x def __lt__(self, other): if self.x < other.x: return False else: return True

    • B. 

      Class foo: def __init__(self, x): self.x = x def __less__(self, other): if self.x > other.x: return False else: return True

    • C. 

      Class foo: def __init__(self, x): self.x = x def __lt__(self, other): if self.x < other.x: return True else: return False

    • D. 

      Class foo: def __init__(self, x): self.x = x def __less__(self, other): if self.x < other.x: return False else: return True

Related Topics
Back to Top Back to top