|
1 |
| -# Python-Interview-Questions |
| 1 | +# Python-Interview-Questions |
| 2 | +- Capgemini |
| 3 | + |
| 4 | +#### What is a method? |
| 5 | + |
| 6 | +A method is a function on some object x that you normally call as x.name(arguments...). Methods are defined as functions inside the class definition: class C: def meth (self, arg): return arg*2 + self.attribute. |
| 7 | + |
| 8 | +#### How can I find the methods or attributes of an object? |
| 9 | + |
| 10 | +For an instance x of a user-defined class, dir(x) returns an alphabetized list of the names containing the instance attributes and methods and attributes defined by its class. |
| 11 | +Capgemini Python Technical Interview Questions And Answers |
| 12 | + |
| 13 | +#### How do I call a method defined in a base class from a derived class that overrides it? |
| 14 | + |
| 15 | +If you're using new-style classes, use the built-in super() function: class Derived(Base): def meth (self): super(Derived, self).meth() If you're using classic classes: For a class definition such as class Derived(Base): ... you can call method meth() defined in Base (or one of Base's base classes) as Base.meth(self, arguments...). Here, Base.meth is an unbound method, so you need to provide the self argument. |
| 16 | + |
| 17 | +#### How can I organize my code to make it easier to change the base class? |
| 18 | + |
| 19 | +You could define an alias for the base class, assign the real base class to it before your class definition, and use the alias throughout your class. Then all you have to change is the value assigned to the alias. Incidentally, this trick is also handy if you want to decide dynamically (e.g. depending on availability of resources) which base class to use. Example: BaseAlias = class Derived(BaseAlias): def meth(self): BaseAlias.meth(self) |
| 20 | + |
| 21 | +#### How do I find the current module name? |
| 22 | + |
| 23 | +A module can find out its own module name by looking at the predefined global variable __name__. If this has the value '__main__', the program is running as a script. Many modules that are usually used by importing them also provide a command-line interface or a self-test, and only execute this code after checking __name__: def main(): print 'Running test...' ... if __name__ == '__main__': main() __import__('x.y.z') returns Try: __import__('x.y.z').y.z For more realistic situations, you may have to do something like m = __import__(s) for i in s.split(".")[1:]: m = getattr(m, i) |
| 24 | + |
| 25 | +#### How do I copy a file? |
| 26 | + |
| 27 | +The shutil module contains a copyfile() function. |
| 28 | + |
| 29 | +#### How do I access a module written in Python from C? |
| 30 | + |
| 31 | +You can get a pointer to the module object as follows: module = PyImport_ImportModule(""); If the module hasn't been imported yet (i.e. it is not yet present in sys.modules), this initializes the module; otherwise it simply returns the value of sys.modules[""]. Note that it doesn't enter the module into any namespace -- it only ensures it has been initialized and is stored in sys.modules. You can then access the module's attributes (i.e. any name defined in the module) as follows: attr = PyObject_GetAttrString(module, ""); Calling PyObject_SetAttrString() to assign to variables in the module also works. |
| 32 | + |
| 33 | +#### How do I interface to C++ objects from Python? |
| 34 | + |
| 35 | +Depending on your requirements, there are many approaches. To do this manually, begin by reading the "Extending and Embedding" document. Realize that for the Python run-time system, there isn't a whole lot of difference between C and C++ -- so the strategy of building a new Python type around a C structure (pointer) type will also work for C++ objects. |
| 36 | + |
| 37 | +#### How can I pass optional or keyword parameters from one function to another? |
| 38 | + |
| 39 | +Collect the arguments using the * and ** specifier in the function's parameter list; this gives you the positional arguments as a tuple and the keyword arguments as a dictionary. You can then pass these arguments when calling another function by using * and **: def f(x, *tup, **kwargs): ... kwargs['width']='14.3c' ... g(x, *tup, **kwargs) In the unlikely case that you care about Python versions older than 2.0, use 'apply': def f(x, *tup, **kwargs): ... kwargs['width']='14.3c' ... apply(g, (x,)+tup, kwargs) |
| 40 | + |
| 41 | +#### How do you make a higher order function in Python? |
| 42 | + |
| 43 | +You have two choices: you can use nested scopes or you can use callable objects. For example, suppose you wanted to define linear(a,b) which returns a function f(x) that computes the value a*x+b. Using nested scopes: def linear(a,b): def result(x): return a*x + b return result Or using a callable object: class linear: def __init__(self, a, b): self.a, self.b = a,b def __call__(self, x): return self.a * x + self.b In both cases: taxes = linear(0.3,2) gives a callable object where taxes(10e6) == 0.3 * 10e6 + 2. The callable object approach has the disadvantage that it is a bit slower and results in slightly longer code. However, note that a collection of callables can share their signature via inheritance: class exponential(linear): # __init__ inherited def __call__(self, x): return self.a * (x ** self.b) Object can encapsulate state for several methods: class counter: value = 0 def set(self, x): self.value = x def up(self): self.value=self.value+1 def down(self): self.value=self.value-1 count = counter() inc, dec, reset = count.up, count.down, count.set Here inc(), dec() and reset() act like functions which share the same counting variable. |
| 44 | + |
| 45 | +#### How do I convert a number to a string? |
| 46 | + |
| 47 | +To convert, e.g., the number 144 to the string '144', use the built-in function str(). If you want a hexadecimal or octal representation, use the built-in functions hex() or oct(). For fancy formatting, use the % operator on strings, e.g. "%04d" % 144 yields '0144' and "%.3f" % (1/3.0) yields '0.333'. See the library reference manual for details. |
| 48 | + |
| 49 | +#### What are the disadvantages of the Python programming language? |
| 50 | + |
| 51 | +It is not best for mobile application |
| 52 | +Due to interpreter its execution speed is not up to mark as compared to compiler |
| 53 | +#### How is the Implementation of Python's dictionaries done? |
| 54 | + |
| 55 | +Python dictionary needs to be declared first: |
| 56 | +dict = { } |
| 57 | + |
| 58 | +Key value pair can be added as: |
| 59 | +dict[key] = value |
| 60 | + or |
| 61 | +objDict.update({key:value}) |
| 62 | + |
| 63 | +Remove element by: |
| 64 | +dict.pop(key) |
| 65 | + |
| 66 | +Remove all: |
| 67 | +objDict.clear() |
| 68 | +A hash value of the key is computed using a hash function, The hash value addresses a location in an array of "buckets" or "collision lists" which contains the (key , value) pa... |
| 69 | + |
| 70 | +#### What is used to create Unicode string in Python? |
| 71 | +"u" should be added before the string |
| 72 | +`` |
| 73 | +a=u"Python" |
| 74 | +type(a) # will give you unicode |
| 75 | +`` |
| 76 | +Add unicode before the string. Ex: unicode(text) resulting in text. |
| 77 | + |
| 78 | +#### What is the built-in function used in Python to iterate over a sequence of numbers? |
| 79 | + |
| 80 | +Syntax : range(start,end,step count) |
| 81 | +Ex: a = range(1,10,2) |
| 82 | + print a |
| 83 | + |
| 84 | +Output: [1, 3, 5, 7, 9] |
| 85 | + |
| 86 | +If using to iterate |
| 87 | +`` |
| 88 | +for i in range(1,10): |
| 89 | + print i |
| 90 | +`` |
| 91 | +o/p: |
| 92 | +`` |
| 93 | + 1 |
| 94 | + 2 |
| 95 | + 3 |
| 96 | + 4 |
| 97 | + 5 |
| 98 | + 6 |
| 99 | + 7 |
| 100 | + 8 |
| 101 | + 9 |
| 102 | +`` |
| 103 | +Range() |
| 104 | +`` |
| 105 | +for i in range(10): |
| 106 | + print i |
| 107 | +`` |
| 108 | +#### What is the method does join() in python belong to? |
| 109 | + |
| 110 | +``.join([]).`` It takes any iterables into this method. |
| 111 | +Join method is used to concatenate the elements of any list. |
| 112 | + |
| 113 | +#### Does python support switch or case statement in Python?If not what is the reason for the same? |
| 114 | + |
| 115 | +Dictionary can be used as case/switch . |
| 116 | +Actually there is no switch statement in the Python programming language but the is a similar construct that can do justice to switch that is the exception handling.using try and except1,except2,except3.... and so on. |
| 117 | + |
| 118 | +#### What is the statement that can be used in Python if a statement is required syntactically but the program requires no action? |
| 119 | + |
| 120 | +pass |
| 121 | + |
| 122 | +Code: |
| 123 | +`` |
| 124 | +try x[10]: |
| 125 | + print(x) |
| 126 | +except: |
| 127 | + pass |
| 128 | +`` |
| 129 | +Use pass keyword over there like: |
| 130 | +`` |
| 131 | +if a>0: |
| 132 | + print "Hello" |
| 133 | +else: |
| 134 | + pass |
| 135 | +`` |
| 136 | +pass keyword is used to do nothing but it fulfill the syntactical requirements. |
| 137 | + |
| 138 | +#### What is used to represent Strings in Python.Is double quotes used for String representation or single quotes used for String representation in Python? |
| 139 | + |
| 140 | +Both works in Python |
| 141 | + |
| 142 | +Most preferable way by PEP 8 is in double quotes |
| 143 | + |
| 144 | + |
| 145 | +#### Does Python support strongly for regular expressions? What are the other languages that support strongly for regular expressions? |
| 146 | + |
| 147 | +Yes, Python Supports Regular Expressions Well. |
| 148 | +There is a lot of other languages that have good support to RegEx- |
| 149 | + Perl |
| 150 | + Awk |
| 151 | + Sed |
| 152 | + Java |
| 153 | + etc. |
| 154 | + |
| 155 | +Yes python strongly support regular expression. Other languages supporting regular expressions are: Delphi, Java, Java script, .NET, Perl, Php, Posix, python, Ruby, Tcl, Visual Basic, XML schema, VB script, Visual Basic 6. |
| 156 | + |
| 157 | +#### How is memory managed in Python? |
| 158 | + |
| 159 | +Python memory is managed by Python private heap space. All Python objects and data structures are located in a private heap. The programmer does not have an access to this private heap and interpreter... |
| 160 | + |
| 161 | +Like other programming language python also has garbage collector which will take care of memory management in python. |
| 162 | + |
| 163 | +#### Why can't lambda forms in Python contain statements? |
| 164 | + |
| 165 | +Lambdas evaluates at run time and these do not need statements |
| 166 | +Lambda is a anonymous function, which does not have a name and no fixed number of arguments. Represented by keyword lambda followed by statement. |
| 167 | +Ex: |
| 168 | + sum = lambda a,b: a+b |
| 169 | + sum(2,3) |
| 170 | + output: 5 |
| 171 | + Answer Question |
| 172 | + |
| 173 | + Select Best Answer |
| 174 | + |
| 175 | +May 29 2015 08:23 AM |
| 176 | +3412 |
| 177 | +Views |
| 178 | +0 |
| 179 | +Ans |
| 180 | + |
| 181 | +Encryption with AES/CBC/PKCS5 |
| 182 | + |
| 183 | + radha |
| 184 | + Python Interview Questions |
| 185 | + |
| 186 | +AES Encryption using CBC/PKCS5 padding in python while decrypting in java it is given below error |
| 187 | + |
| 188 | +javax.crypto.BadPaddingException: Decryption error at sun.security.rsa.RSAPadding.unpadV15(Unknown Source) at sun.security.rsa.RSAPadding.unpad(Unknown Source) at com.sun.crypto.provider.RSACipher.doFinal(RSACipher.java:363) at com.sun.crypto.provider.RSACipher.engineDoFinal(RSACipher.java:389)... |
| 189 | +Answer Question |
| 190 | +Jan 04 2009 01:41 AM |
| 191 | +7306 |
| 192 | +Views |
| 193 | +1 |
| 194 | +Ans |
| 195 | + |
| 196 | +Python Popularity |
| 197 | + |
| 198 | + ushanamballa |
| 199 | + Python Interview Questions |
| 200 | + |
| 201 | +Why Python is not that much of popular, when compared to other programming languages? |
| 202 | + |
| 203 | + suji |
| 204 | + |
| 205 | + Mar 2nd, 2012 |
| 206 | + |
| 207 | + Python is not so useful in web environment where the user interface is the browser. Hence, lot of people are using PHP for different design goals. Google is tightly connected with python as they make ... |
| 208 | + Answer Question |
| 209 | + |
| 210 | + Select Best Answer |
| 211 | + |
| 212 | +Aug 02 2010 03:16 AM |
| 213 | +7916 |
| 214 | +Views |
| 215 | +1 |
| 216 | +Ans |
| 217 | + |
| 218 | +Phyton Advantages and Dis-Advantages |
| 219 | + |
| 220 | + torch_619 |
| 221 | + Python Interview Questions |
| 222 | + |
| 223 | +What are the advantage and dis-advantage of Phyton programming language? |
| 224 | + |
| 225 | + suji |
| 226 | + |
| 227 | + Mar 2nd, 2012 |
| 228 | + |
| 229 | + Python is more like java and bit cumbersome, but it leads to a better design. Python is an interpreted language, high level programming, pure object-oriented, high performance server side scripting la... |
| 230 | + Answer Question |
| 231 | + |
| 232 | + Select Best Answer |
| 233 | + |
| 234 | +Jan 08 2007 11:23 AM |
| 235 | +4501 |
| 236 | +Views |
| 237 | +2 |
| 238 | +Ans |
| 239 | + |
| 240 | +What is the optional statement used in a try ... except statement in Python? |
| 241 | + |
| 242 | + norman |
| 243 | + Python Interview Questions |
| 244 | + |
| 245 | + Priya Patel |
| 246 | + |
| 247 | + Oct 20th, 2007 |
| 248 | + |
| 249 | + There are 2 optional clauses used in try...except statements: 1. else clause: It is useful for code that must be executed when the try block doesn't create any exception2. finally clause:Its is useful for code that must be executed irrespective of whether an exception is generated or not. |
| 250 | + xeio |
| 251 | + |
| 252 | + Sep 25th, 2007 |
| 253 | + |
| 254 | + Try ... except (...) ... [finally] |
| 255 | + Answer Question |
| 256 | + |
| 257 | + Select Best Answer |
| 258 | + |
| 259 | +Jan 08 2007 11:18 AM |
| 260 | +4134 |
| 261 | +Views |
| 262 | +1 |
| 263 | +Ans |
| 264 | + |
| 265 | +What is List Comprehensions feature of Python used for? |
| 266 | + |
| 267 | + SachinDeo |
| 268 | + Python Interview Questions |
| 269 | + |
| 270 | + Priya Patel |
| 271 | + |
| 272 | + Oct 19th, 2007 |
| 273 | + |
| 274 | + List comprehensions help to create and manage lists in a simpler and |
| 275 | + clearer way than using map(), filter() and lambda. Each list comprehension |
| 276 | + consists of an expression followed by a for clause, then zero or more for or |
| 277 | + if clauses. |
| 278 | + Answer Question |
| 279 | + |
| 280 | + Select Best Answer |
| 281 | + |
| 282 | +Oct 10 2007 11:37 AM |
| 283 | +3956 |
| 284 | +Views |
| 285 | +0 |
| 286 | +Ans |
| 287 | + |
| 288 | +What is __slots__ and when is it useful? |
| 289 | + |
| 290 | + Vince Gatto |
| 291 | + Python Interview Questions |
| 292 | + |
| 293 | +Normally, all class objects have an __dict__ which allows new attributes to be bound to a class instance at runtime. When a class is defined with __slots__, only attributes whose names are present in the __slots__ sequence are allowed. This results in instances of this class not having an __dict__ attribute and not being able to bind new attributes at run time.__slots__ is useful because it eliminates... |
| 294 | +Answer Question |
| 295 | +Jan 08 2007 11:08 AM |
| 296 | +5921 |
| 297 | +Views |
| 298 | +1 |
| 299 | +Ans |
| 300 | + |
| 301 | +Why isn't all memory freed when Python exits? |
| 302 | + |
| 303 | + Robert |
| 304 | + Python Interview Questions |
| 305 | + |
| 306 | + stranger1 |
| 307 | + |
| 308 | + Sep 28th, 2007 |
| 309 | + |
| 310 | + Objects referenced from the global namespaces of Python modules are not always deallocated when Python exits. This may happen if there are circular references. There are also certain bits of memory ... |
| 311 | + Answer Question |
| 312 | + |
| 313 | + Select Best Answer |
| 314 | + |
| 315 | +Jan 08 2007 11:06 AM |
| 316 | +3926 |
| 317 | +Views |
| 318 | +1 |
| 319 | +Ans |
| 320 | + |
| 321 | +Why was the language called as Python? |
| 322 | + |
| 323 | + nancyphilips |
| 324 | + Python Interview Questions |
| 325 | + |
| 326 | + stranger1 |
| 327 | + |
| 328 | + Sep 28th, 2007 |
| 329 | + |
| 330 | + At the same time he began implementing Python, Guido van Rossum was also reading the published scripts from "Monty Python's Flying Circus" (a BBC comedy series from the seventies, in the. |
0 commit comments