Python quiz questions

Python interview questions

  • 1.

    What is the type of b?

    a = "bay"
    b = a[0]

     

    1. chr

    2. ord

    3. int

    4. str

    Answer
  • 2.

    What is the result of this code?

    a=[1,2,3,4,5]
    a[3:1:-1]

     

    1. SyntaxError

    2. ValueError

    3. [3, 2]

    4. [4, 3]

    5. [4, 3, 2]

    Answer
  • 3.

    What is the result of this code?

    a=[1,2,3,4,5,6,7,8,9]
    a[::2]=10,20,30,40,50,60

     

    1. SyntaxError

    2. ValueError

    3. [10, 2, 20, 4, 30, 6, 40, 8, 50, 9, 60]

    Answer
  • 4.

    What is the result of this code?

    a=[1,2,3,4,5,6,7,8,9]
    a[::2]

     

    1. [1,2]

    2. [1,3]

    3. [8,9]

    4. [1,3,5,7,9]

    5. SyntaxError

    Answer
  • 5.

    What will be placed in a?

    a = [1,2,3]
    a[-3:-1] = 10,20,30,40

     

    1. IndexError

    2. TypeError

    3. [10, 20, 30, 40, 3]

    4. [10, 20, 30, 40, 2, 3]

    5. [10, 20, 30, 40]

    6. [[10, 20, 30, 40],3]

    Answer
  • 6.

    What will be placed in a?

    a = {'a': 1,'b':2, 'a':3}

     

    1. It causes SyntaxError

    2. {'a': 1,'b': 2, 'a': 3}

    3. The behavior is undefined

    4. {'a': 1,'b': 2}

    5. {'a': 3,'b': 2}

    Answer
  • 7.

    What is the result of this code?

    a = {(1,2):1,(2,3):2}
    a[1,2]

     

    1. KeyError

    2. 1

    Answer
  • 8.

    What is the result of this code?

    a = {'a':1,'b':2,'c':3}
    a['a','b']

     

    1. KeyError

    2. [1,2]

    3. {'a':1,'b':2}

    Answer
  • 9.

    How to assign a tuple of length 1 to a?

    1. a = 1

    2. a = (1,)

    3. a = (1)

    4. a = tuple(1)

    Answer
  • 10.

    What will be placed in a?

    a = 2,3

     

    1. 2

    2. 3

    3. (2,3)

    Answer
  • 11.

    What gets printed (with python version 3.X) assuming the user enters 'foo' at the prompt?

    a = input("#: ")
    
    print (a)
    1. f

    2. foo

    3. Not a number

    4. An exception is thrown

    Answer
  • 12.

    What gets printed?

    kvps = { '1' : 1, '2' : 2 , '3' : 3, '4' : 4, '5' : 5}
    newData = { '1' : 10, '3' : 30 }
    
    kvps.update(newData)
    
    x = sum(kvps.values())
    
    print x
    1. 15

    2. 51

    3. 150

    4. An exception is thrown

    Answer
  • 13.

    What gets printed?

    kvps = { '1' : 1, '2' : 2 }
    theCopy = dict(kvps)
    
    kvps['1'] = 5
    
    sum = kvps['1'] + theCopy['1']
    print sum
    1. 0

    2. 2

    3. 6

    4. 9

    5. An exception is thrown

    Answer
  • 14.

    What gets printed?

    import copy
    
    aList = [1,2]
    bList = [3,4]
    
    kvps = { '1' : aList, '2' : bList }
    theCopy = copy.deepcopy(kvps)
    
    kvps['1'][0] = 5
    
    sum = kvps['1'][0] + theCopy['1'][0]
    print sum
    1. 1

    2. 2

    3. 6

    4. 10

    5. An exception is thrown

    Answer
  • 15.

    What gets printed
     

    aList = [1,2]
    bList = [3,4]
    
    kvps = { '1' : aList, '2' : bList }
    theCopy = kvps.copy()
    
    kvps['1'][0] = 5
    
    sum = kvps['1'][0] + theCopy['1'][0]
    print sum
    1. 1

    2. 2

    3. 6

    4. 10

    5. An exception is thrown

    Answer
  • 16.

    What gets printed?

    kvps = { '1' : 1, '2' : 2 }
    theCopy = kvps.copy()
    
    kvps['1'] = 5
    
    sum = kvps['1'] + theCopy['1']
    print sum
    1. 1

    2. 2

    3. 6

    4. 10

    5. An exception is thrown

    Answer
  • 17.

    What gets printed?

    kvps = { '1' : 1, '2' : 2 }
    theCopy = kvps
    
    kvps['1'] = 5
    
    sum = kvps['1'] + theCopy['1']
    print sum
    1. 1

    2. 2

    3. 7

    4. 10

    5. An exception is thrown

    Answer
  • 18.

    If the user types '0' at the prompt what gets printed?

    def getinput():
        print "0: start"
        print "1: stop"
        print "2: reset"
        x = raw_input("selection: ")
        try:
            num = int(x)
            if num > 2 or num < 0:
                return None
            return num
        except:
            return None
    
    num = getinput()
    if not num:
        print "invalid"
    else:
        print "valid"
    1. valid

    2. invalid

    3. An exception is thrown

    Answer
  • 19.

    What gets printed?

    x = sum(range(5))
    print x
    1. 4

    2. 5

    3. 10

    4. 15

    5. An exception is thrown

    Answer
  • 20.

    What gets printed (with python version 2.X) assuming the user enters the following at the prompt?
    #: foo

    a = input("#: ")
    
    print a
    1. f

    2. foo

    3. #: foo

    4. An exception is thrown

    Answer

© 2017 QuizBucket.org