Python -- interview questions Flashcards
Q: How Python is interpreted?
A: Python language is an interpreted language. Python program runs directly from the source code. It converts the source code that is written by the programmer into an intermediate language, which is again translated into machine language that has to be executed.
Q: What is the difference between list and tuple?
A: The difference between list and tuple is that the list is mutable while tuple is not. Tuple can be hashed for e.g as a key for dictionaries.
Q: How are arguments passed by value or by reference?
A: Everything in Python is an object and all variables hold references to the objects. The references values are according to the functions; as a result, you cannot change the value of the references. However, you can change the objects if it is mutable.
Q: What are Dict and List comprehension?
They are syntax constructions to ease the creation of a Dictionary or List based on existing iterable.
Q: What are the built-in type does python provides?
A: There are mutable and Immutable types of Pythons built-in types Mutable built-in types List Sets Dictionaries Immutable built-in types Strings Tuples
Q: What is namespace in Python?
A: In Python, every name introduced has a place where it lives and can be hooked for. This is known as a namespace. It is like a box where a variable name is mapped to the object placed. Whenever the variable is searched out, this box will be searched, to get the corresponding object.
Q: What is lambda in Python?
A: It is a single expression anonymous function often used as an inline function.
Q: Why lambda forms in python do not have statements?
A: A lambda form in python does not have statements as it is used to make a new function object and then return them at runtime.
Q: What are the generators in Python?
A: The way of implementing iterators are known as generators. It is a normal function except that it yields expression in the function.
Q: How can you copy an object in Python?
A: To copy an object in Python, you can try copy.copy () or copy.deepcopy() for the general case. You cannot copy all objects but most of them.
Q: What is a negative index in Python?
A: Python sequences can be index in positive and negative numbers. For positive index, 0 is the first index, 1 is the second index and so forth. For negative index, (-1) is the last index and (-2) is the second last index and so forth.
Q: How you can convert a number to a string?
A: In order to convert a number into a string, use the inbuilt function str(). If you want an octal or hexadecimal representation, use the inbuilt function oct() or hex().
Q: What is the difference between Xrange and range?
A: Xrange returns the xrange object while range returns the list, and uses the same memory and no matter what the range size is.
Q: What is the module and package in Python?
A: In Python, the module is the way to structure the program. Each Python program file is a module, which imports other modules like objects and attributes.
Q: What are the rules for local and global variables in Python?
Local variables: If a variable is assigned a new value anywhere within the function’s body, it’s assumed to be local.
Global variables: Those variables that are only referenced inside a function are implicitly global.