Monday, October 23, 2017

Python Interview Questions for freshers

In this post we will learn frequently asked Python Interview Questions and answers for freshers and also for experienced developers.In previous post we had discussed about why Python so powerful for Data Science, one of the frequently raised question that one so go through that post as well.

1) What is Python?

Ans: Python is a high level ,Object oriented,most successful interpreted language. It is a portable language,since it can run on different platforms such as Windows,Linux,Unix,Mac...It is a interpreted language that means when write a Python script it doesn't need to get compiled before execution.

2) What are the Benefits of Python?

Ans:

  1. Python is a Object Oriented Programming language as you can define classes along with the composition and Inheritance. It doesn't use any access specifier like Public or private
  2. It provides very high level dynamic data types and supports dynamic type checking
  3. It can be use as Scripting language or can be compiled to byte-code for building large applications.
  4. We can use Python for developing Web applications,automation,Scientific Modeling,Big Data applications etc..
  5. It can be easily integrated with C,C++,JAVA,COBRA,COM .
  6. It is extensible
3) What are the Data types in Python?

Ans: Python has 5 Standard Data types:

  1. Numbers
  2. String
  3. List
  4. Tuple
  5. Dictiornary
4) Is Multithreading supports in Python?

Ans: YES,Python has multi-threading package. It has a Construct called Global Interpreter Lock(GIL). This GIL makes you feeling only one thread is 'executing' at any one time.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.This means that if you want to make your code run faster then using the threading package often isn’t a good idea.

5)What is the output of print str if str = ‘Hello World!’?


Ans: Hello World!.

6) How to overload methods in Python?

Ans: Python's constructor: 
_init__ () is a first method of a class. Whenever we try to instantiate a object __init__() is automatically invoked by python to initialize members of an object.

7)How To Find Bugs Or Perform Static Analysis In A Python Application?

Ans: You can use PyChecker, which is a static analyzer. It identifies the bugs in Python project and also reveals the style and complexity related bugs.Another tool is Pylint, which checks whether the Python module satisfies the coding standard.

8) How to achieve Inheritance in Python?

Ans: Inheritance allows One class to gain all the members(say attributes and methods) of another class. Inheritance provides code re-usability, 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.

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

10)How is memory managed in Python?

Ans: 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.

11)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, State & Captain. Their corresponding values are India, Andhra and Kohili respectively.


dict={'Country':'India','State':'Andhra','Captain':'kohli'}
print dict[Country]
India
print dict[State]
Andhra
print dict[Captian]
Kohli

12)What is pickling and unpickling in Python?

Ans: Pickling is a process in which a pickle module accepts any Python object, converts it into a string representation and dumps it into a file by using dump() function.
Unpickling is a process of retrieving original Python object from the stored string representation for use.

13) How Python does Compile-time and Run-time code checking?

Ans: In Python some amount of coding is done at compile time, but most of the checking such as type, name, etc. are postponed until code execution. Consequently, if the Python code references a user -defined function that does not exist, the code will compile successfully. The Python code will fail only with an exception when the code execution path does not exist.

14)Which command is used to exit help window or help command prompt?

Ans: "quit" command is used to exit help window or help command prompt.

15)What Is The Key Difference Between A List And The Tuple?
Ans: 
The major difference between a list and the tuple is that the list is mutable while tuple is not. A tuple is allowed to be hashed, for example, using it as a key for dictionaries.

16)Is There A Switch Or Case Statement In Python? If Not Then What Is The Reason For The Same?

Ans:
No, Python does not have a Switch statement, but you can write a Switch function and then use it.

 17)What Is A Built-In Function That Python Uses To Iterate Over A Number Sequence?

Ans: 
range() generates a list of numbers, which is used to iterate over for loops.
Example:
for i in range(5):
    print(i)

The range() function accompanies two sets of parameters.

range(stop)
stop: It is the no. of integers to generate and starts from zero. eg. range(4) == [0, 1, 2,3].
range([start], stop[, step])
start: It is the starting no. of the sequence.
stop: It specifies the upper limit of the sequence.
step: It is the incrementing factor for generating the sequence.

Points to note:
Only integer arguments are allowed.
Parameters can be positive or negative.
The <range()> function in Python starts from the zeroth index.