python interview exam

1. The following code is output:

list1 = {'1':1,'2':2}
list2 = list1
list1['1'] = 5
sum = list1['1'] + list2['1']
print(sum)

Analysis: 10

b = a: assignment references, a and b are the same object.
L IST1 and list2 points to the same memory space
list1 [ '1'] = 5  ------>     change this value the value of a memory space of '1'
This step is performed after the data stored in the memory space becomes: { '1': 5 '2': 2}
Accordingly sum = [ '1'] + list2 [ '1'] = 5 + 5 = 10 list1
 
2. codes known print_func.py follows:
print('HelloWorld!')
print('__name__value: ', __name__)

def main():
    print('This message is from main function')
  
if __name__ =='__main__':
    main()

print_module.py code is as follows:

import print_func
print("Done!")

Print_module.py run the program, the result is:

Hello World!  __name__ value: print_module  Done!

Resolution:

When the operation of the module, __ name__ equal "__main__"; if the import into the other modules, the module name is equal __name__ (no suffix .py)
 
3. Python _foo single underscore and double underscore __foo and __foo__ member of the following statements is correct?
A _foo not be used directly 'from Module1 Import *' 
B __foo parser _classname__foo name instead, and to distinguish between other types same naming 
C __foo__ Representative python in a dedicated method for identifying particular 
D __foo may be used directly 'from module import * '
The answer: ABC
Resolution:
There are four main python named:
  a: object public methods
  b: _object semi-protected
                  Is seen as "protect", meaning that only the class and subclass object objects they can access these variables,
                  Can not be used outside the module or class is not used 'from module import *' import.
                  __object method is to avoid the subclass name conflicts, the identifier for the method described in the parent class method can not be easily covered by subclasses, their names are actually _classname__methodname.
                 
  c: _ _ object wholly private, full protection
                        Private members "private", meaning that only he can access the object class, subclass object can not even visit
                          Asked this data, you can not use 'from module import *' import.
  d: _ _ object_ _ built-in methods, so that users do not defined
 
4. There was a python coding procedure is as follows: urllib.quote (. Line.decode ( "gbk ") encode ( "utf16")), to ask the decoded sequence is encoded by the string (url decoding utf16 gbk)
Resolution:
It is a title code encoding process:
encoding: decode () 
decoding: encode () 
URL encoding: urllib.quote () 
line.decode ( "gbk") shows line is gbk coding

The encoding process:
line -> decoding gbk -> encoding utf-16 -> encoded URL

(opposite to the encoding process) decoding process:
decoding url -> utf-16 -> gbk
 
5. Python is an interpreted language, what characteristics are interpreted languages? 
Non-independent and inefficiency
 
6. The following python socket on the operating statements is correct (CD)
A use recvfrom () receives TCP data 
B using getsockname () Gets connector socket remote address 
C using the connect () to initialize the server TCP connection 
D server using listen () starts listening TCP

Resolution:

sk.recv (bufsize [, flag]): receive data socket. Data is returned as a string, bufsize can receive up to a specified number. flag provides additional information about the message can usually be ignored.

sk.recvfrom (bufsize [.flag]): the recv () is similar, but the return value is (data, address). Wherein the received data is a string containing the data, address data is the address of the sending socket.
sk.getsockname (): Returns the socket's own address. Usually a tuple (ipaddr, port)

sk.connect (address): address to the socket connector at. In general, the format of the address tuple (hostname, port), if the connection error, an error return socket.error.

sk.listen (backlog): start listening for incoming connections. backlog specified before connection rejection, the maximum number of connections may be suspended.

 
7. After the operation code below, the value of a, b, c, d of the four variables that describes the error is? d
import copy
a = [1, 2, 3, 4, ['a', 'b']]
b = a
c = copy.copy(a)
d = copy.deepcopy(a)
a.append(5)
a[4].append('c')

a ==  [1,2, 3, 4, ['a', 'b', 'c'], 5]
b ==  [1,2, 3, 4, ['a', 'b', 'c'], 5]
c ==  [1,2, 3, 4, ['a', 'b', 'c']]
d ==  [1,2, 3, 4, ['a', 'b', ‘c’]]

Resolution:

b = a, just a change of name, a change b on how how to change,
c is a shallow copy, copy only the value of a portion of still share some value, so when a child object is operated may change c
d is a deep copy, a complete copy of all values, has nothing to do with a, any operation will not affect a d

 

 

 First, we look to see the situation of b, b and a point of fact, is the same value, like man's name and nickname, just different names, but still the same person

 

Then look at the case of c, c situation and the situation a.copy () is the same, we are the so-called shallow copy (shallow copy), shallow copy copy only the parent object, the child object will not copy, popular to say that only copied to the second layer

 

 

If the parent object changes, c does not change, because all of its parent objects that have been copied, if the child object is changed becomes c
D look at the situation, and this is what we call deep copy, no matter what action a, d does not change, they have a different point values ​​(here refers to a different location in the memory storage).

 

 

 

 

 

 

 

 
 
 
 
 

Guess you like

Origin www.cnblogs.com/xiuercui/p/11838441.html