Python Online Test

40 Questions

Settings
Please wait...
Python Programming Quizzes & Trivia

Created by Guido Van Rossum and initially released to the market in 1991, Python is a high level-programming tool that is for general programming use. How good are you with it? The Python online test below has the answer.


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.5y = 2print 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 = Truey = Falsez = Falseif x or y and z:print "yes"else:print "no"
    • A. 

      Yes

    • B. 

      No

    • C. 

      Fails to compile

  • 6. 
    • 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 accessSecureSystem 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 = paramclass child(parent):def __init__(self, param):self.v2 = paramobj = 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 ** 2for 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.pyand the program is run as: python /usr/lib/python/person.py What gets printed?class Person:def __init__(self):passdef 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 + ca = 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. 
    • 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 itby default when executed in shell.
    • A. 

      Int

    • B. 

      Bool

    • C. 

      Void

    • D. 

      None

  • 20. 
    • 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(Multiple answers possible) ?
    • 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 somethingexcept:    # Do somethingfinally:    # 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

  • 26. 
     Let A and B be objects of class Foo. Which functions are called when print(A + B) is executed?
    • A. 

      __add__(), __str__()

    • B. 

      __str__(), __add__()

    • C. 

      __sum__(), __str__()

    • D. 

      __str__(), __sum__()

  • 27. 
    • A. 

      (math.e ** p) – 1

    • B. 

      math.e ** (p – 1)

    • C. 

      Error

    • D. 

      None of the mentioned

  • 28. 
    • A. 

      9

    • B. 

      9.0

    • C. 

      None

    • D. 

      None of the mentioned

  • 29. 
    • A. 

      No difference

    • B. 

      In r+ the pointer is initially placed at the beginning of the file and the pointer is at the end for w+

    • C. 

      In w+ the pointer is initially placed at the beginning of the file and the pointer is at the end for r+

    • D. 

      Depends on the operating system

  • 30. 
    • A. 

      Fp.seek()

    • B. 

      Fp.tell()

    • C. 

      Fp.loc

    • D. 

      Fp.pos

  • 31. 
    • A. 

      File position is set to the start of file

    • B. 

      File position is set to the end of file

    • C. 

      File position remains unchanged

    • D. 

      Error

  • 32. 
    • A. 

      os.getcwd()

    • B. 

      Os.cwd()

    • C. 

      os.getpwd()

    • D. 

      Os.pwd()

  • 33. 
    Python supports the creation of anonymous functions at runtime, using a construct called __________?
    • A. 

      Lambda

    • B. 

      Pi

    • C. 

      Anonymous

    • D. 

      None of the mentioned

  • 34. 
     What is the output of this program? y = 6 z = lambda x: x * yprint z(8)
    • A. 

      48

    • B. 

      14

    • C. 

      64

    • D. 

      None of mentioned

  • 35. 
    What is the output of below program? def writer():    title = 'Sir'    name = (lambda x:title + ' ' + x)    return name who = writer()who('Arthur')
    • A. 

      Arthur Sir

    • B. 

      Sir Arthur

    • C. 

      Arthur

    • D. 

      None of mentioned

  • 36. 
    What is the output of the below program? def C2F(c):    return c * 9/5 + 32print C2F(100)print C2F(0)
    • A. 

      212 32

    • B. 

      314 24

    • C. 

      567 98

    • D. 

      None of the mentioned

  • 37. 
    What is the output of the below program? def power(x, y=2):    r = 1    for i in range(y):       r = r * x    return rprint power(3)print power(3, 3)
    • A. 

      212 32

    • B. 

      9 27

    • C. 

      Error

    • D. 

      567 98

    • E. 

      None of the mentioned

  • 38. 
    3. What is the order of precedence in python?i) Parenthesesii) Exponentialiii) Divisioniv) Multiplicationv) Additionvi) Subtraction
    • A. 

      i,ii,iii,iv,v,vi

    • B. 

      Ii,i,iii,iv,v,vi

    • C. 

      Ii,i,iv,iii,v,vi

    • D. 

      I,ii,iii,iv,vi,v

    • E. 

      I,ii,iv,iii,v,vi

  • 39. 
     Operators with the same precedence are evaluated in which manner?
    • A. 

      Left to Right

    • B. 

      Right to Left

  • 40. 
    Which of the following is incorrect?
    • A. 

      x = 0b101

    • B. 

      X = 0x4f5

    • C. 

      x = 19023

    • D. 

      X = 03964