Python quiz questions

Python interview questions

  • 1.

    What gets printed?

    x = 1
    y = "2"
    z = 3
    
    sum = 0
    for i in (x,y,z):
        if isinstance(i, int):
            sum += i
    print sum
    1. 2

    2. 3

    3. 4

    4. 6

    5. An exception is thrown

    Answer
  • 2.

    Assuming python 2.x, what gets printed?

    x = 0
    y = 1
    
    a = cmp(x,y)
    if a < x:
        print "a"
    elif a == x:
        print "b"
    else:
        print "c"
    1. a

    2. b

    3. c

    Answer
  • 3.

    What gets printed?

    one = chr(104)
    two = chr(105)
    print "%s%s" % (one, two)
    1. hi

    2. h

    3. None

    4. 104105

    5. 104

    Answer
  • 4.

    What gets printed?

    class NumFactory:
        def __init__(self, n):
            self.val = n
        def timesTwo(self):
            self.val *= 2
        def plusTwo(self):
            self.val += 2
    
    f = NumFactory(2)
    for m in dir(f):
        mthd = getattr(f,m)
        if callable(mthd):
            mthd()
    
    print f.val
    1. 2

    2. 4

    3. 6

    4. 8

    5. An exception is thrown

    Answer
  • 5.

    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
    1. 1

    2. 2

    3. 3

    4. 6

    5. 7

    Answer
  • 6.

    Asume Python 2
    How do you create a package so that the following reference will work?

    p = mytools.myparser.MyParser()
    1. Declare the myparser package in mytools.py

    2. Create an __init__.py in the home dir

    3. Inside the mytools dir create a __init__.py and myparser.py

    4. Create a myparser.py directory inside the mytools directory

    5. This can not be done

    Answer
  • 7.

    What gets printed?

    def myfunc(x, y, z, a):
        print x + y
    
    nums = [1, 2, 3, 4]
    
    myfunc(*nums)
    1. 1

    2. 3

    3. 6

    4. 10

    5. An exception is thrown

    Answer
  • 8.

    What gets printed?

    def dostuff(param1, **param2):
       print type(param2)
    
    
    dostuff('capitals', Arizona='Phoenix',
    California='Sacramento', Texas='Austin')
    1. in

    2. str

    3. tuple

    4. list

    5. dict

    Answer
  • 9.

    What gets printed?

    def dostuff(param1, *param2):
       print type(param2)
    
    dostuff('apples', 'bananas', 'cherry', 'dates')
    1. str

    2. int

    3. tuple

    4. list

    5. dict

    Answer
  • 10.

    What gets printed?

    def print_header(str):
        print "+++%s+++" % str
    
    
    print_header.category = 1
    print_header.text = "some info"
    
    print_header("%d %s" %  \
    (print_header.category, print_header.text))
    1. +++1 some info+++

    2. +++%s+++

    3. 1

    4. 1

    5. some info

    Answer
  • 11.

    What gets printed?

    a = 1
    b = 2
    a,b = b,a
    
    print "%d %d" % (a,b)
    1. 1 2

    2. 2 1

    3. An exception is thrown

    4. This program has undefined behavior

    Answer
  • 12.

    What gets printed?

    my_tuple = (1, 2, 3, 4)
    my_tuple.append( (5, 6, 7) )
    print len(my_tuple)
    1. 1

    2. 2

    3. 5

    4. 7

    5. An exception is thrown

    Answer
  • 13.

    What gets printed?

    def addItem(listParam):
        listParam += [1]
    
    mylist = [1, 2, 3, 4]
    addItem(mylist)
    print len(mylist)
    1. 1

    2. 4

    3. 5

    4. 8

    5. An exception is thrown

    Answer
  • 14.

    What gets printed?

    list1 = [1, 2, 3, 4]
    list2 = [5, 6, 7, 8]
    
    print len(list1 + list2)
    1. 2

    2. 4

    3. 5

    4. 8

    5. An exception is thrown

    Answer
  • 15.

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

    1. list

    2. set

    3. dictionary

    4. All of the above

    5. None of the above

    Answer
  • 16.

    What gets printed?

    numbers = [1, 2, 3, 4]
    
    numbers.append([5,6,7,8])
    
    print len(numbers)
    1. 4

    2. 5

    3. 8

    4. 12

    5. An exception is thrown

    Answer
  • 17.

    What gets printed?

    names1 = ['Amir', 'Barry', 'Chales', 'Dao']
    names2 = [name.lower() for name in names1]
    
    print names2[2][0]
    1. i

    2. a

    3. c

    4. C

    5. An exception is thrown

    Answer
  • 18.

    What gets printed?

    names1 = ['Amir', 'Barry', 'Chales', 'Dao']
    
    if 'amir' in names1:
        print 1
    else:
        print 2
    1. 1

    2. 2

    3. An exception is thrown

    Answer
  • 19.

    What gets printed?

    names1 = ['Amir', 'Barry', 'Chales', 'Dao']
    
    loc = names1.index("Edward")
    
    print loc
    1. -1

    2. 0

    3. 4

    4. Edward

    5. An exception is thrown

    Answer
  • 20.

    What gets printed?

    names1 = ['Amir', 'Barry', 'Chales', 'Dao']
    names2 = names1
    names3 = names1[:]
    
    names2[0] = 'Alice'
    names3[1] = 'Bob'
    
    sum = 0
    for ls in (names1, names2, names3):
        if ls[0] == 'Alice':
            sum += 1
        if ls[1] == 'Bob':
            sum += 10
    
    print sum
    1. 11

    2. 12

    3. 21

    4. 22

    5. 33

    Answer

© 2017 QuizBucket.org