As The last ship sailed towards the distant horizon I sat there watching on a rock My mind slowly drifting away Forming into my... Dreamtale
Python InterView Questions
2018-03-30 / 11 min read
  • What are the key features of Python?
    Ans: These are the few key features of Python:

    • Python is an interpreted language. That means that, unlike languages like C and its variants, Python does not need to be compiled before it is run. Other interpreted languages include PHP and Ruby.
    • Python is dynamically typed, this means that you don’t need to state the types of variables when you declare them or anything like that. You can do things like x=111 and then x="I'm a string" without error
    • Python is well suited to object orientated programming in that it allows the definition of classes along with composition and inheritance. Python does not have access specifiers (like C++’s public, private), the justification for this point is given as “we are all adults here”
    • In Python, functions are first-class objects. This means that they can be assigned to variables, returned from other functions and passed into functions. Classes are also first class objects
    • Writing Python code is quick but running it is often slower than compiled languages. Fortunately,Python allows the inclusion of C based extensions so bottlenecks can be optimized away and often are. The numpy package is a good example of this, it’s really quite quick because a lot of the number crunching it does isn’t actually done by Python
    • Python finds use in many spheres – web applications, automation, scientific modelling, big data applications and many more. It’s also often used as “glue” code to get other languages and components to play nice.
  • What is the difference between deep and shallow copy?
    Ans: Shallow copy is used when a new instance type gets created and it keeps the values that are copied in the new instance. Shallow copy is used to copy the reference pointers just like it copies the values. These references point to the original objects and the changes made in any member of the class will also affect the original copy of it. Shallow copy allows faster execution of the program and it depends on the size of the data that is used.

    Deep copy is used to store the values that are already copied. Deep copy doesn’t copy the reference pointers to the objects. It makes the reference to an object and the new object that is pointed by some other object gets stored. The changes made in the original copy won’t affect any other copy that uses the object. Deep copy makes execution of the program slower due to making certain copies for each object that is been called.

  • What is the difference between list and tuples?
    Ans: Lists are mutable i.e they can be edited. Syntax: list_1 = [10, ‘Chelsea’, 20]

    Tuples are immutable (tuples are lists which can’t be edited). Syntax: tup_1 = (10, ‘Chelsea’ , 20)

  • How is Multithreading achieved in Python?

    • Python has a multi-threading package but if you want to multi-thread to speed your code up.
    • Python has a construct called the Global Interpreter Lock (GIL). The GIL makes sure that only one of your ‘threads’ can execute at any one time. A thread acquires the GIL, does a little work, then passes the GIL onto the next thread.
    • This happens very quickly so to the human eye it may seem like your threads are executing in parallel, but they are really just taking turns using the same CPU core.
    • All this GIL passing adds overhead to execution. This means that if you want to make your code run faster then using the threading package often isn’t a good idea.
  • How is memory managed in Python?

    • 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 takes care of this Python private heap.
    • The allocation of Python heap space for Python objects is done by Python memory manager. The core API gives access to some tools for the programmer to code.
    • Python also have an inbuilt garbage collector, which recycle all the unused memory and frees the memory and makes it available to the heap space.
  • Explain Inheritance in Python with an example.
    Ans: Inheritance allows One class to gain all the members(say attributes and methods) of another class. Inheritance provides code reusability, makes it easier to create and maintain an application. The class from which we are inheriting is called super-class and the class that is inherited is called a derived / child class.

    They are different types of inheritance supported by Python:

    • Single Inheritance – where a derived class acquires the members of a single super class.
    • Multi-level inheritance – a derived class d1 in inherited from base class base1, and d2 is inherited from base2.
    • Hierarchical inheritance – from one base class you can inherit any number of child classes
    • Multiple inheritance – a derived class is inherited from more than one base class.
  • Explain what Flask is and its benefits?
    Ans: Flask is a web micro framework for Python based on “Werkzeug, Jinja2 and good intentions” BSD license. Werkzeug and Jinja2 are two of its dependencies. This means it will have little to no dependencies on external libraries. It makes the framework light while there is little dependency to update and less security bugs.

    A session basically allows you to remember information from one request to another. In a flask, a session uses a signed cookie so the user can look at the session contents and modify. The user can modify the session if only it has the secret key Flask.secret_key.

  • What is the usage of help() and dir() function in Python?
    Ans: Help() and dir() both functions are accessible from the Python interpreter and used for viewing a consolidated dump of built-in functions.

    Help() function: The help() function is used to display the documentation string and also facilitates you to see the help related to modules, keywords, attributes, etc.
    Dir() function: The dir() function is used to display the defined symbols.

  • Whenever Python exits, why isn’t all the memory de-allocated?

    • Whenever Python exits, especially those Python modules which are having circular references to other objects or the objects that are referenced from the global namespaces are not always de-allocated or freed.
    • It is impossible to de-allocate those portions of memory that are reserved by the C library.
    • On exit, because of having its own efficient clean up mechanism, Python would try to de-allocate/destroy every other object.
  • What is dictionary in Python?
    Ans: The built-in datatypes in Python is called dictionary. It defines one-to-one relationship between keys and values. Dictionaries contain pair of keys and their corresponding values. Dictionaries are indexed by keys.

    Let’s take an example:

    The following example contains some keys. Country, Capital & PM. Their corresponding values are India, Delhi and Modi respectively.

    print dict[Country]
    print dict[Capital]
    print dict[PM]

  • What are negative indexes and why are they used?
    Ans: The sequences in Python are indexed and it consists of the positive as well as negative numbers. The numbers that are positive uses ‘0’ that is uses as first index and ‘1’ as the second index and the process goes on like that.

    The index for the negative number starts from ‘-1’ that represents the last index in the sequence and ‘-2’ as the penultimate index and the sequence carries forward like the positive number.

    The negative index is used to remove any new-line spaces from the string and allow the string to except the last character that is given as S[:-1]. The negative index is also used to show the index to represent the string in correct order.

  • What is the process of compilation and linking in python?
    Ans: The compiling and linking allows the new extensions to be compiled properly without any error and the linking can be done only when it passes the compiled procedure. If the dynamic loading is used then it depends on the style that is being provided with the system. The python interpreter can be used to provide the dynamic loading of the configuration setup files and will rebuild the interpreter.

    The steps that is required in this as:

    • Create a file with any name and in any language that is supported by the compiler of your system. For example file.c or file.cpp
    • Place this file in the Modules/ directory of the distribution which is getting used.
    • Add a line in the file Setup.local that is present in the Modules/ directory.
    • Run the file using spam file.o
    • After successful run of this rebuild the interpreter by using the make command on the top-level directory.
    • If the file is changed then run rebuildMakefile by using the command as ‘make Makefile’.
  • Explain split(), sub(), subn() methods of “re” module in Python.
    Ans: To modify the strings, Python’s “re” module is providing 3 methods. They are:

    • split() – uses a regex pattern to “split” a given string into a list.
    • sub() – finds all substrings where the regex pattern matches and then replace them with a different string
    • subn() – it is similar to sub() and also returns the new string along with the no. of replacements.
  • Explain the use of session in Django framework?
    Ans: Django provides session that lets you store and retrieve data on a per-site-visitor basis. Django abstracts the process of sending and receiving cookies, by placing a session ID cookie on the client side, and storing all the related data on the server side.