Python的面试题/测试题汇总,带答案

*来源于网络整理

1.what does the following code do?(B

def a(b, c, d): pass

 

A.defines a list and initializes it

B.defines a function, which does nothing

C.defines a function, which passes its parameters through

D.defines an empty class

 

2.what gets printed? Assuming python version 2.x(A)

print type(1/2)

 

A.<type 'int'>

B.<type 'number'>

C.<type 'float'>

D.<type 'double'>

E.<type 'tuple'>

 

3. what is the output of the following code?(E

print type([1,2])

 

A.<type 'tuple'>

B.<type 'int'>

C.<type 'set'>

D.<type 'complex'>

E.<type 'list'>

 

4. what gets printed?(C

def f(): pass

print type(f())

 

A.<type 'function'>

B.<type 'tuple'>

C.<type 'NoneType'>

D.<type 'str'>

E.<type 'type'>

 

5. what should the below code print?(A)

print type(1J)

 

A.<type 'complex'>

B.<type 'unicode'>

C.<type 'int'>

D.<type 'float'>

E.<type 'dict'>

 

6. what is the output of the following code?(D)

print type(lambda:None)

 

A.<type 'NoneType'>

B.<type 'tuple'>

C.<type 'type'>

D.<type 'function'>

E.<type 'bool'>

 

7. what is the output of the below program?(D)

a = [1,2,3,None,(),[],]

print len(a)

 

A.syntax error

B.4

C.5

D.6

E.7

 

8.what gets printed? Assuming python version 3.x(C)

print (type(1/2))

 

A.<type 'int'>

B.<type 'number'>

C.<type 'float'>

D.<type 'double'>

E.<type 'tuple'>

 

9. What gets printed?(C)

d = lambda p: p * 2

t = lambda p: p * 3

x = 2

x = d(x)

x = t(x)

x = d(x)

print x

 

A.7

B.12

C.24

D.36

E.48

 

10. What gets printed?(A)

x = 4.5

y = 2

print x//y

 

A.2.0

B.2.25

C.9.0

D.20.25

E.21

 

11. What gets printed?(C)

nums = set([1,1,2,3,3,3,4])

print len(nums)

 

A.1

B.2

C.4

D.5

E.7

 

12. What gets printed?(A)

x = True

y = False

z = False

 

if x or y and z:

    print "yes"

else:

    print "no"

 

A.yes

B.no

C.fails to compile

 

13. What gets printed?(C)

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

 

A.1

B.2

C.3

D.4

 

14. If PYTHONPATH is set in the environment, which directories are searched for modules?(D)

 

A) PYTHONPATH directory

 

B) current directory

 

C) home directory

 

D) installation dependent default path

 

A.A only

B.A and D

C.A, B, and C

D.A, B, and D

E.A, B, C, and D

 

15. In python 2.6 or earlier, the code will print error type 1 if accessSecureSystem raises an exception of either AccessError type or SecurityError type(B)

 

try:

  accessSecureSystem()

except AccessError, SecurityError:

  print "error type 1"

 

continueWork()

 

A.true

B.false

 

16. The following code will successfully print the days and then the months(B)

 

daysOfWeek = ['Monday',

              'Tuesday',

              'Wednesday',

              'Thursday',

              'Friday',

              'Saturday',

              'Sunday']

 

months =             ['Jan', \

                      'Feb', \

                      'Mar', \

                      'Apr', \

                      'May', \

                      'Jun', \

                      'Jul', \

                      'Aug', \

                      'Sep', \

                      'Oct', \

                      'Nov', \

                      'Dec']

 

print "DAYS: %s, MONTHS %s" %

    (daysOfWeek, months)

 

A.true

B.false

 

17. Assuming python 2.6 what gets printed?(A)

 

f = None

 

for i in range (5):

    with open("data.txt", "w") as f:

        if i > 2:

            break

 

print f.closed

 

A.True

B.False

C.None

 

18. What gets printed?(C)

 

counter = 1

 

def doLotsOfStuff():

    

    global counter

 

    for i in (1, 2, 3):

        counter += 1

 

doLotsOfStuff()

 

print counter

 

A.1

B.3

C.4

D.7

E.none of the above

 

19. What gets printed?(C)

 

print r"\nwoow"

 

A.new line then the string: woow

B.the text exactly like this: r"\nwoow"

C.the text like exactly like this: \nwoow

D.the letter r and then newline then the text: woow

E.the letter r then the text like this: nwoow

 

20.What gets printed?(B)

 

print "hello" 'world'

 

A.on one line the text: hello world

B.on one line the text: helloworld

C.hello on one line and world on the next line

D.syntax error, this python program will not run

 

21.What gets printed?(E)

 

print "\x48\x49!"

 

A.\x48\x49!

B.4849

C.4849!

D.      48      49!

E.HI!

 

22. What gets printed?(D)

 

print 0xA + 0xa

 

A.0xA + 0xa

B.0xA 0xa

C.14

D.20

E.0x20

 

23. What gets printed?(E)

 

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)

 

A.None None

B.None 11

C.11 None

D.11 11

E.Error is generated by program

 

24. What gets printed?(E)

 

kvps  = {"user","bill", "password","hillary"}

 

print kvps['password']

 

A.user

B.bill

C.password

D.hillary

E.Nothing. Python syntax error

 

25. What gets printed?(B)

66% on 1871 times asked

 

class Account:

    def __init__(self, id):

        self.id = id

        id = 666

 

acc = Account(123)

print acc.id

 

A.None

B.123

C.666

D.SyntaxError, this program will not run

 

26. What gets printed?(C)

 

name = "snow storm"

 

print "%s" % name[6:8]

 

A.st

B.sto

C.to

D.tor

E.Syntax Error

 

27. What gets printed?(D)

 

name = "snow storm"

 

name[5] = 'X'

 

print name

 

A.snow storm

B.snowXstorm

C.snow Xtorm

D.ERROR, this code will not run

 

28. Which numbers are printed?(C)

 

for i in  range(2):

    print i

 

for i in range(4,6):

    print i

 

A.2, 4, 6

B.0, 1, 2, 4, 5, 6

C.0, 1, 4, 5

D.0, 1, 4, 5, 6, 7, 8, 9

E.1, 2, 4, 5, 6

 

29. What sequence of numbers is printed?(B)

 

values = [1, 2, 1, 3]

nums = set(values)

 

def checkit(num):

    if num in nums:

        return True

    else:

        return False

 

for i in  filter(checkit, values):

    print i

 

A.1 2 3

B.1 2 1 3

C.1 2 1 3 1 2 1 3

D.1 1 1 1 2 2 3 3

E.Syntax Error

 

30. What sequence of numbers is printed?(E)

 

values = [2, 3, 2, 4]

 

def my_transformation(num):

    return num ** 2

 

for i in  map(my_transformation, values):

    print i

 

A.2 3 2 4

B.4 6 4 8

C.1 1.5 1 2

D.1 1 1 2

E.4 9 4 16

 

31. What numbers get printed(C)

 

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

 

A.500 300

B.500 500

C.600 400

D.600 600

E.300 500

 

32. What gets printed by the code snippet below?(B)

 

import math

 

print math.floor(5.5)

 

A.5

B.5.0

C.5.5

D.6

E.6.0

 

33. What gets printed by the code below?(E)

 

class Person:

    def __init__(self, id):

        self.id = id

 

obama = Person(100)

 

obama.__dict__['age'] = 49

 

print obama.age + len(obama.__dict__)

 

A.1

B.2

C.49

D.50

E.51

 

 

34. What gets printed?(E)

 

x = "foo "

y = 2

print x + y

 

A.foo

B.foo foo

C.foo 2

D.2

E.An exception is thrown

 

35. What gets printed?(E)

 

def simpleFunction():

    "This is a cool simple function that returns 1"

    return 1

 

print simpleFunction.__doc__[10:14]

A.simpleFunction

B.simple

C.func

D.funtion

E.cool

 

36. What does the code below do?(C)

 

sys.path.append('/root/mods')

 

A.Changes the location that the python executable is run from

B.Changes the current working directory

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

D.Removes all directories for mods

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

 

37. What gets printed?(C)

 

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

 

A.3

B.7

C.13

D.14

E.15

 

38. Which of the following print statements will print all the names in the list on a seperate line(A)

 

names = ['Ramesh', 'Rajesh', 'Roger', 'Ivan', 'Nico']

 

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

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

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

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

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

 

39. True or false? Code indentation must be 4 spaces when creating a code block?(B)

 

if error:

    # four spaces of indent are used to create the block

    print "%s" % msg

 

A.True

B.False

 

40. 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?(D)

 

class Person:

    def __init__(self):

        pass

 

    def getAge(self):

        print __name__

 

p = Person()

p.getAge()

 

A.Person

B.getAge

C.usr.lib.python.person

D.__main__

E.An exception is thrown

 

41. What gets printed(B)

 

foo = {}

print type(foo)

 

A.set

B.dict

C.list

D.tuple

E.object

 

42. What gets printed?(C)

 

foo = (3, 4, 5)

print type(foo)

 

A.int

B.list

C.tuple

D.dict

E.set

 

43. What gets printed?(D)

 

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)

 

A.0

B.1

C.2

D.3

E.4

 

44. What gets printed?(D)

 

confusion = {}

confusion[1] = 1

confusion['1'] = 2

confusion[1] += 1

 

sum = 0

for k in confusion:

    sum += confusion[k]

 

print sum

 

A.1

B.2

C.3

D.4

E.5

 

45. What gets printed?(C)

 

confusion = {}

confusion[1] = 1

confusion['1'] = 2

confusion[1.0] = 4

 

sum = 0

for k in confusion:

    sum += confusion[k]

 

print sum

 

A.2

B.4

C.6

D.7

E.An exception is thrown

 

46.What gets printed?(E)

 

boxes = {}

jars = {}

crates = {}

 

boxes['cereal'] = 1

boxes['candy'] = 2

jars['honey'] = 4

crates['boxes'] = boxes

crates['jars'] = jars

 

print len(crates[boxes])

A.1

B.2

C.4

D.7

E.An exception is thrown

 

47. What gets printed?(E)

 

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

 

A.8

B.12

C.24

D.30

E.33

 

48. What gets printed?(A)

 

foo = {1:'1', 2:'2', 3:'3'}

foo = {}

print len(foo)

 

A.0

B.1

C.2

D.3

E.An exception is thrown

 

49. What gets printed?(B)

 

foo = {1:'1', 2:'2', 3:'3'}

del foo[1]

foo[1] = '10'

del foo[2]

print len(foo)

A.1

B.2

C.3

D.4

E.An exception is thrown

 

50. What gets printed?(E)

 

names = ['Amir', 'Barry', 'Chales', 'Dao']

print names[-1][-1]

 

A.A

B.r

C.Amir

D.Dao

E.o

 

51. What gets printed?(B)

 

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

 

A.11

B.12

C.21

D.22

E.33

 

52. What gets printed?(E)

 

names1 = ['Amir', 'Barry', 'Chales', 'Dao']

 

loc = names1.index("Edward")

 

print loc

 

A.-1

B.0

C.4

D.Edward

E.An exception is thrown

 

53. What gets printed?(B)

 

names1 = ['Amir', 'Barry', 'Chales', 'Dao']

 

if 'amir' in names1:

    print 1

else:

    print 2

 

A.1

B.2

C.An exception is thrown

 

54. What gets printed?(C)

 

names1 = ['Amir', 'Barry', 'Chales', 'Dao']

names2 = [name.lower() for name in names1]

 

print names2[2][0]

 

A.i

B.a

C.c

D.C

E.An exception is thrown

 

55. What gets printed?(B)

 

numbers = [1, 2, 3, 4]

 

numbers.append([5,6,7,8])

 

print len(numbers)

A.4

B.5

C.8

D.12

E.An exception is thrown

 

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

 

A.list

B.set

C.dictionary

D.None of the above

E.All of the above

 

57. Wat gets printed?(D)

 

list1 = [1, 2, 3, 4]

list2 = [5, 6, 7, 8]

 

print len(list1 + list2)

 

A.2

B.4

C.5

D.8

E.An exception is thrown

 

58. What gets printed?(C)

 

def addItem(listParam):

    listParam += [1]

 

mylist = [1, 2, 3, 4]

addItem(mylist)

print len(mylist)

 

A.1

B.4

C.5

D.8

E.An exception is thrown

 

59. What gets printed?(E)

 

my_tuple = (1, 2, 3, 4)

my_tuple.append( (5, 6, 7) )

print len(my_tuple)

 

A.1

B.2

C.5

D.7

E.An exception is thrown

 

60. What gets printed?(B)

 

a = 1

b = 2

a,b = b,a

 

print "%d %d" % (a,b)

 

A.1 2

B.2 1

C.An exception is thrown

D.This program has undefined behavior

 

61. What gets printed?(A)

 

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))

 

A.+++1 some info+++

B.+++%s+++

C.1

D.some info

 

62. What gets printed?(C)

 

def dostuff(param1, *param2):

   print type(param2)

 

dostuff('apples', 'bananas', 'cherry', 'dates')

 

A.str

B.int

C.tuple

D.list

E.dict

 

63. What gets printed?(E

 

def dostuff(param1, **param2):

   print type(param2)

 

 

dostuff('capitals', Arizona='Phoenix',

California='Sacramento', Texas='Austin')

 

A.in

B.str

C.tuple

D.list

E.dict

 

64. What gets printed?(B)

 

def myfunc(x, y, z, a):

    print x + y

 

nums = [1, 2, 3, 4]

 

myfunc(*nums)


A.1

B.3

C.6

D.10

E.An exception is thrown

 

65. How do you create a package so that the following reference will work?(C)

 

p = mytools.myparser.MyParser()

 

A.Declare the myparser package in mytools.py

B.Create an __init__.py in the home dir

C.Inside the mytools dir create a __init__.py

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

E.This can not be done

 

66. What gets printed?(E)

 

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

 

A.1

B.2

C.3

D.6

E.7

 

67. What gets printed?(E)

 

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

 

A.2

B.4

C.6

D.8

E.An exception is thrown

 

68. What gets printed?(A)

 

one = chr(104)

two = chr(105)

print "%s%s" % (one, two)

 

A.hi

B.h

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

D.104105

E.104

 

69. What gets printed?(A)

 

x = 0

y = 1

 

a = cmp(x,y)

if a < x:

    print "a"

elif a == x:

    print "b"

else:

    print "c"

 

A.a

B.b

C.c

 

70. What gets printed?(C)

 

x = 1

y = "2"

z = 3

 

sum = 0

for i in (x,y,z):

    if isinstance(i, int):

        sum += i

print sum

 

A.2

B.3

C.4

D.6

E.An exception is thrown

 

71. What gets printed (with python version 2.X) assuming the user enters the following at the prompt?(D)

#: foo

 

a = input("#: ")

 

print a

 

A.f

B.foo

C.#: foo

D.An exception is thrown

 

72. What gets printed?(C)

 

x = sum(range(5))

print x

 

A.4

B.5

C.10

D.15

E.An exception is thrown

 

73. If the user types '0' at the prompt what gets printed?(B)

 

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"

 

A.valid

B.invalid

C.An exception is thrown

 

74. What gets printed?(D)

 

kvps = { '1' : 1, '2' : 2 }

theCopy = kvps

 

kvps['1'] = 5

 

sum = kvps['1'] + theCopy['1']

print sum

 

A.1

B.2

C.7

D.10

E.An exception is thrown

 

75. What gets printed?(C)

 

kvps = { '1' : 1, '2' : 2 }

theCopy = kvps.copy()

 

kvps['1'] = 5

 

sum = kvps['1'] + theCopy['1']

print sum

 

A.1

B.2

C.6

D.10

E.An exception is thrown

 

76. What gets printed(D)

 

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

 

A.1

B.2

C.6

D.10

E.An exception is thrown

 

77. What gets printed?(C)

 

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

 

A.1

B.2

C.6

D.10

E.An exception is thrown

 

78. What gets printed?(C)

 

kvps = { '1' : 1, '2' : 2 }

theCopy = dict(kvps)

 

kvps['1'] = 5

 

sum = kvps['1'] + theCopy['1']

print sum

 

A.1

B.2

C.6

D.10

E.An exception is thrown

 

79. What gets printed?(B)

 

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

 

A.15

B.51

C.150

D.An exception is thrown

 

80. What gets printed (with python version 3.X) assuming the user enters the following at the prompt?(B)

#: foo

 

a = input("#: ")

 

print a

 

A.f

B.foo

C.Not a number

D.An exception is thrown

 

 

 

 

 

 

 

 

猜你喜欢

转载自blog.csdn.net/jlhx123456/article/details/107570587