Python quiz questions

Python interview questions

  • 1.

    Which of the following is the output of the below Python code?

    def myfunc():
     try:
      print('Monday')
     finally:
      print('Tuesday')
    myfunc()
    1. Tuesday

    2. Monday Tuesday

    3. undefined exception

    4. Monday

    5. None of these

    6. Tuesday Monday

    Answer
  • 2.

    Which of the following is the correct output of the call to below line of code?

    print(list("hello"))
    1. ['h', 'e', 'l', 'l', 'o']

    2. [h,e,l,l,o]

    3. hello

    4. ['h' 'e' 'l' 'l' 'o']

    5. None of these

    Answer
  • 3.

    Determine the output of the below Python code fragment?

    var1 = True
    var2 = False
    var3 = False
    if var1 or var2 and var3:
     print("True")
    else:
     print("False")
    1. False

    2. Compile time error

    3. True

    4. Runtime error

    5. None of these

    Answer
  • 4.

    Which of the following is the output of the instructions mentioned below?

    def test1(param):
     return str(param)
    def test2(param):
     return str(2 * param)
    result = test1(1) + test2(2)
    print(result)
    1. 3

    2. 5

    3. Compile time error

    4. 14

    Answer
  • 5.

    Which of the following is the output of the below Python code?

    def test1(param):
     return param
    def test2(param):
     return param * 2
    def test3(param):
     return param + 3
    result = test1(test2(test3(1)))
    print(result)
    1. 1

    2. 6

    3. 3

    4. 8

    Answer
  • 6.

    Which of the following is the output of the below Python code?

    def test():
     try:
      return 1
     finally:
      return 2
    result = test()
    print(result)
    1. Runtime error.

    2. 1

    3. 2

    4. Compile error, there is more than one return statement in a single try-finally block.

    5. None of these

    Answer
  • 7.

    Which of the following statements would create an instance of Ubuntu class correctly?

    class Ubuntu:
     def __init__(self, ramsize):
      self.ram = ramsize
      self.type = 'server'
    1. Ubuntu = Ubuntu('server', 2000)

    2. Ubuntu = Ubuntu(2000)

    3. Ubuntu = Ubuntu('client', 2000)

    4. Ubuntu = Ubuntu()

    5. None of these

    Answer
  • 8.

    Which of the following is the output of the below Python code?

    test_list = [1, 5, 5, 5, 5, 1]
    max = test_list[0]
    indexOfMax = 0
    for i in range(1, len(test_list)):
        if test_list[i] > max:
            max = test_list[i]
            indexOfMax = i
    print(indexOfMax)
    1. 1

    2. 4

    3. 0

    4. 2

    5. 3

    Answer
  • 9.

    What is the output of the following code snippet?

    print type([1,2])
    1. <type 'tuple'>

    2. <type 'complex'>

    3. <type 'int'>

    4. <type 'set'>

    5. <type 'list'>

    Answer
  • 10.

    Which of the following is the output of the below Python code?

    def f(): pass
    print(type(f()))
    1. <type 'type'>

    2. <type 'function'>

    3. <type 'tuple'>

    4. <type 'NoneType'>

    5. <type 'str'>

    Answer
  • 11.

    Which of the following is the output of the below Python code?

    def Test():
        try:
            print(20)
        finally:
            print(30)
    Test()
    1. 20
      30

    2. 20

    3. 30

    4. 30
      20

    5. None

    Answer
  • 12.

    Which of the following is the output of the following Python code?

    print(type(lambda:None))
    1. <type 'type'>

    2. <type 'tuple'>

    3. <type 'NoneType'>

    4. <type 'function'>

    5. <type 'bool'>

    Answer
  • 13.

    Say test_list is [3, 4, 5, 20, 5, 25, 1, 3] then what would be the value of test_list after test_list.pop(1)?

    1. [1, 3, 3, 4, 5, 5, 20, 25]

    2. [3, 4, 5, 20, 5, 25, 1, 3]

    3. [3, 5, 20, 5, 25, 1, 3]

    4. [3, 1, 25, 5, 20, 5, 4]

    5. [1, 3, 4, 5, 20, 5, 25]

    Answer
  • 14.

    The x % y operator can be used to check for even or odd numbers.

    Here, we need to verify an even number so complete the below condition?

    if _______:
       print("x is an even number")
    1. X % 1 == 2

    2. X % 2 == 1

    3. X % x == 0

    4. X % "even" == True

    5. X % 2 == 0

    Answer
  • 15.

    Which of the following is the output of the below Python code?

    try:
        print("throw")
    except:
        print("except")
    finally:
        print("finally")
    1. finally
      throw
    2. finally
      except
    3. except
      finally
    4. throw
      finally
    5. Syntax error

    Answer
  • 16.

    Fill in the missing part of the code to print the following patterns:

    5 5 5 5 5
    4 4 4 4 4
    3 3 3 3 3
    2 2 2 2 2
    1 1 1 1 1

    for i in range(5, 0, ____ ):
       print(str(i) * 5)
    1. 0

    2. None

    3. 1

    4. -1

    Answer
  • 17.

    What does the following code do?

    def a(b, c, d): pass
    1. defines a list and initializes it.

    2. None of these.

    3. defines a function, which does nothing.

    4. defines a function, which passes its parameters through.

    5. defines an empty class.

    Answer
  • 18.

    What is the output of the below code fragment?
    [Python version 2.x]

    print type(1/2)
    1. <type 'number'>

    2. <type 'double'>

    3. <type 'tuple'>

    4. <type 'int'>

    5. <type 'float'>

    Answer
  • 19.

    Which of the following literals is not valid to be a function name?

    1. Function_1

    2. Function1

    3. Func_1_tion

    4. _function1

    5. 1function

    Answer
  • 20.

    Which of the following is the output of the below Python code fragment?

    d = lambda p: p * 2
    t = lambda p: p * 3
    x = 2
    x = d(x)
    x = t(x)
    x = d(x)
    print(x)
    1. 36

    2. 24

    3. 12

    4. 7

    5. 48

    Answer

© 2017 QuizBucket.org