Python quiz questions

Python interview questions

  • 1.

    What gets printed?

    names = ['Amir', 'Barry', 'Chales', 'Dao']
    print names[-1][-1]
    1. A

    2. r

    3. Amir

    4. Dao

    5. o

    Answer
  • 2.

    What gets printed?

    foo = {1:'1', 2:'2', 3:'3'}
    del foo[1]
    foo[1] = '10'
    del foo[2]
    print len(foo)
    1. 1

    2. 2

    3. 3

    4. 4

    5. An exception is thrown

    Answer
  • 3.
    foo = {1:'1', 2:'2', 3:'3'}
    foo = {}
    print len(foo)
    1. 0

    2. 1

    3. 2

    4. 3

    5. An exception is thrown

    Answer
  • 4.

    What gets printed?

    numberGames = {}
    numberGames[(1,2,4)] = 8
    numberGames[(4,2,1)] = 10
    numberGames[(1,2)] = 12
    
    sum = 0
    for k in numberGames:
        sum += numberGames[k]
    
    print len(numberGames) + sum
    1. 8

    2. 12

    3. 24

    4. 30

    5. 33

    Answer
  • 5.

    What gets printed?

    boxes = {}
    jars = {}
    crates = {}
    
    boxes['cereal'] = 1
    boxes['candy'] = 2
    jars['honey'] = 4
    crates['boxes'] = boxes
    crates['jars'] = jars
    
    print len(crates[boxes])
    1. 1

    2. 2

    3. 4

    4. 7

    5. An exception is thrown

    Answer
  • 6.

    What gets printed?

    confusion = {}
    confusion[1] = 1
    confusion['1'] = 2
    confusion[1.0] = 4
    
    sum = 0
    for k in confusion:
        sum += confusion[k]
    
    print sum
    1. 2

    2. 4

    3. 6

    4. 7

    5. An exception is thrown

    Answer
  • 7.

    What gets printed?

    confusion = {}
    confusion[1] = 1
    confusion['1'] = 2
    confusion[1] += 1
    
    sum = 0
    for k in confusion:
        sum += confusion[k]
    
    print sum
    1. 1

    2. 2

    3. 3

    4. 4

    5. 5

    Answer
  • 8.

    What gets printed?

    country_counter = {}
    
    def addone(country):
        if country in country_counter:
            country_counter[country] += 1
        else:
            country_counter[country] = 1
    
    addone('China')
    addone('Japan')
    addone('china')
    
    print len(country_counter)
    1. 0

    2. 1

    3. 2

    4. 3

    5. 4

    Answer
  • 9.

    What gets printed?

    foo = (3, 4, 5)
    print type(foo)
    1. int

    2. list

    3. tuple

    4. dict

    5. set

    Answer
  • 10.

    What gets printed

    foo = {}
    print type(foo)
    1. set

    2. dict

    3. list

    4. tuple

    5. object

    Answer
  • 11.

    Assuming the filename for the code below is /usr/lib/python/person.py
    and the program is run as:

    python /usr/lib/python/person.py 

    What gets printed?

    class Person:
        def __init__(self):
            pass
    
        def getAge(self):
            print __name__
    
    p = Person()
    p.getAge()
    1. Person

    2. getAge

    3. usr.lib.python.person

    4. __main__

    5. An exception is thrown

    Answer
  • 12.

    Code indentation must be 4 spaces when creating a code block?

    if error:
        # four spaces of indent are used to create the block
        print "%s" % msg
    1. True

    2. False

    Answer
  • 13.

    Which of the following print statements will print all the names in the list on a seperate line

    names = ['Ramesh', 'Rajesh', 'Roger', 'Ivan', 'Nico']
    1. print "\n".join(names)

    2. print names.join("\n")

    3. print names.concatenate("\n")

    4. print names.append("\n")

    5. print names.join("%s\n", names)

    Answer
  • 14.

    What gets printed?

    import re
    sum = 0
    
    pattern = 'back'
    if re.match(pattern, 'backup.txt'):
        sum += 1
    if re.match(pattern, 'text.back'):
        sum += 2
    if re.search(pattern, 'backup.txt'):
        sum += 4
    if re.search(pattern, 'text.back'):
        sum += 8
    
    print sum
    1. 3

    2. 7

    3. 13

    4. 14

    5. 15

    Answer
  • 15.

    What does the code below do?

    sys.path.append('/root/mods')
    1. Changes the location that the python executable is run from

    2. Changes the current working directory

    3. Adds a new directory to seach for python modules that are imported

    4. Removes all directories for mods

    5. Changes the location where sub-processes are searched for after they are launched

    Answer
  • 16.

    What gets printed?

    def simpleFunction():
        "This is a cool simple function that returns 1"
        return 1
    
    print simpleFunction.__doc__[10:14]
    1. simpleFunction

    2. simple

    3. func

    4. funtion

    5. cool

    Answer
  • 17.

    What gets printed by the code below?

    class Person:
        def __init__(self, id):
            self.id = id
    
    obama = Person(100)
    
    obama.__dict__['age'] = 49
    
    print obama.age + len(obama.__dict__)
    1. 1

    2. 2

    3. 49

    4. 50

    5. 51

    Answer
  • 18.

    What gets printed by the code snippet below?

    import math
    
    print math.floor(5.5)
    1. 5.0

    2. 5.4

    3. 5.6

    4. 6

    Answer
  • 19.

    What numbers get printed

    import pickle
    
    class account:
    	def __init__(self, id, balance):
    		self.id = id
    		self.balance = balance
    	def deposit(self, amount):
    		self.balance += amount
    	def withdraw(self, amount):
    		self.balance -= amount
    
    myac = account('123', 100)
    myac.deposit(800)
    myac.withdraw(500)
    
    fd = open( "archive", "w" ) 
    pickle.dump( myac, fd)
    fd.close()
    
    myac.deposit(200)
    print myac.balance
    
    fd = open( "archive", "r" ) 
    myac = pickle.load( fd )
    fd.close()
    
    print myac.balance
    1. 500 300

    2. 500 500

    3. 600 400

    4. 600 600

    5. 300 500

    Answer
  • 20.

    What sequence of numbers is printed?

    values = [2, 3, 2, 4]
    
    def my_transformation(num):
        return num ** 2
    
    for i in  map(my_transformation, values):
        print i
    1. 2 3 2 4

    2. 4 6 4 8

    3. 1 1.5 1 2

    4. 1 1 1 2

    5. 4 9 4 16

    Answer

© 2017 QuizBucket.org