Python quiz questions

Python interview questions

  • 1.

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

    class A:
        def __init__(self, i = 2, j = 3):
            self.i = i
            self.j = j
        def __str__(self):
            return"A"
        def __eq__(self, other):
            return self.i * self.j == other.i * other.j
    def main():
        x = A(1, 2)
        y = A(2, 1)
        print(x == y)
    main()
    1. False

    2. 2

    3. True

    4. 1

    5. None

    Answer
  • 2.

    Analyze the following code fragments that assign a boolean value to the variable even?

    number = 202
    #Code1 
    if number % 2 == 0:
        even = True
    else: 
        even = False
    print ("Code1: even := {}".format(even))
    #Code2
    even = True if number % 2 == 0 else False
    print ("Code2: even := {}".format(even))
    #Code3
    even = number % 2 == 0
    print ("Code3: even := {}".format(even))
    1. Code3 has a syntax error because you attempt to assign a number to 'even'.

    2. Code2 has a syntax error because you cannot have True and False literals in the conditional expression.

    3. All three are correct, but Code1 is preferred.

    4. All three are correct, but Code3 is preferred.

    5. All three are correct, but Code2 is preferred.

    Answer
  • 3.

    What is the value of x after the following statements?

    x = 2
    y = 1
    x *= y + 1
    print(x)
    1. x is 2.

    2. x is 1.

    3. x is 3.

    4. x is 4.

    5. None

    Answer
  • 4.

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

    class A:
        def __init__(self):
            self.i = 1
        def m(self):
            self.i = 10
    class B(A):
        def m(self):
            self.i += 1
            return self.i
    def main():
        b = B()
        print(b.m())
    main()
    1. 10

    2. i is not accessible from b.

    3. 2

    4. 1

    Answer
  • 5.

    Which is the output of the following Python code fragment?

    x = True
    y=False
    z= False
    if not x or y:
          print (1)
    elif not x or not y and z:
          print (2)
    elif not x or y or not y and x:
          print (3)
    else:
          print (4)
    1. 2

    2. None

    3. 1

    4. 3

    5. 4

    Answer
  • 6.

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

    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))
    1. 11 None

    2. None None

    3. 11 11

    4. AttributeError: 'child' object has no attribute 'v1'

    5. None 11

    Answer
  • 7.

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

    list1 = [1, 3]
    list2 = list1
    list1[0] = 4
    print(list2)
    1. [4, 3]

    2. [1, 3]

    3. [1, 4]

    4. [1, 3, 4]

    Answer
  • 8.

    What is the output when following statement is executed?

    print (R'Tech\nBeamers')
    1. 'RTech' then 'Beamers' in a New line

    2. 'Tech' then 'Beamers' in a New line

    3. Tech\nBeamers

    4. Tech Beamers

    Answer
  • 9.

    What is the output when following statement is executed?

    print ('Tech' + 'Beamers')
    1. Beamers

    2. Tech

    3. TechBeamers

    4. Tech Beamers

    Answer
  • 10.

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

    str='Tech Beamers'
    str[4]='-'
    print (str)
    1. Tech Beamers

    2. Tech-Beamers

    3. TypeError

    4. Tec- Beamers

    Answer
  • 11.

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

    str='Hello World'
    print  (str.replace('l','n',2))
    1. Henno Wornd

    2. Hello World

    3. Helno World

    4. Henno World

    Answer
  • 12.

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

    str='Tech Beamers'
    print (str[4:9])
    1. Beamer

    2. Beam

    3. Beamers

    4. Beame

    Answer
  • 13.

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

    str='example'
    print  (str.center(2,'*'))
    1. m

    2. example

    3. Error will be generated

    4. *example*

    Answer
  • 14.

    What is the output when following statement is executed?

    print ('Tech'  'Beamers')
    1. Tech Beamers

    2. TechBeamers

    3. Tech

    4. Beamers

    Answer
  • 15.

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

    class  Assign:
        def __init__(self, value= 0):
           self.__value =value
    obj1 = Assign(2)
    obj2 = Assign(2)
    print(id(obj1) == id(obj2))
    str1='Good'
    str2='Good'
    print(id(str1) == id(str2))
    1. True
      True
    2. False
      False
    3. True
      False
    4. False
      True
    Answer
  • 16.

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

    str='Hello World'
    print (str.find('o'))
    1. 7

    2. 2

    3. 4

    4. 4, 7

    Answer
  • 17.

    What is the output when following code will be executed?

    str='Recurssion'
    print  (str.rfind('s'))
    1. 5

    2. 2

    3. 6

    4. 4

    Answer
  • 18.

    Which of the following statements can be used to return the length of the given String, str?

    1. str._len_()

    2. len(str)

    3. size(str)

    4. str.size()

    Answer
  • 19.

    Which of the following operators can be used with Strings?

    a) +
    b) *
    c) -
    d) in
    1. a, b, c, d

    2. a, b

    3. a, b, d

    4. a, b, c

    Answer
  • 20.

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

    str='Tech Beamers'
    print (str[::-1])
    1. Tech

    2. Tech Beamers

    3. sremaeB hceT

    4. Beamers

    Answer

© 2017 QuizBucket.org